1边缘检测
#include<opencv2/opencv.hpp>
#include<opencv2/imgproc.hpp>
using namespace cv;
int main()
{
Mat img = imread("pp.jpg");
imshow("zairu", img);
Mat edge, grayImage;//定义参数
cvtColor(img, grayImage, CV_BGR2GRAY);//转换灰度图
blur(grayImage, edge, Size(3, 3));
Canny(edge, edge, 3, 9, 3);
imshow("效果图",edge);
waitKey(6000);
return 0;
}
2
#include<opencv2/opencv.hpp>
#include<opencv2/imgproc.hpp>
using namespace cv;
int main()
{
VideoCapture capFromFile("mu.mp4");
//VideoCapture capFromFile;
//capFromFile.open("E:\\video\\1.avi");//为0则读入摄像头
while (1)
{
Mat frame;
capFromFile >> frame;
imshow("读取视频",frame);
waitKey(30);
}
return 0;
}
图形绘制
//--------------------------------------【程序说明】-------------------------------------------
// 程序说明:《OpenCV3编程入门》OpenCV3版书本配套示例程序20
// 程序描述:使用OpenCV进行基本的绘图操作
// 开发测试所用操作系统: Windows 7 64bit
// 开发测试所用IDE版本:Visual Studio 2010
// 开发测试所用OpenCV版本: 3.0 beta
// 2014年11月 Created by @浅墨_毛星云
// 2014年12月 Revised by @浅墨_毛星云
//------------------------------------------------------------------------------------------------
//---------------------------------【头文件、命名空间包含部分】----------------------------
// 描述:包含程序所使用的头文件和命名空间
//------------------------------------------------------------------------------------------------
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
//此程序对于OpenCV3版需要额外包含头文件:
#include <opencv2/imgproc/imgproc.hpp>
//-----------------------------------【宏定义部分】--------------------------------------------
// 描述:定义一些辅助宏
//------------------------------------------------------------------------------------------------
#define WINDOW_NAME1 "【绘制图1】" //为窗口标题定义的宏
#define WINDOW_NAME2 "【绘制图2】" //为窗口标题定义的宏
#define WINDOW_WIDTH 600//定义窗口大小的宏
//--------------------------------【全局函数声明部分】-------------------------------------
// 描述:全局函数声明
//-----------------------------------------------------------------------------------------------
void DrawEllipse(Mat img, double angle);//绘制椭圆
void DrawFilledCircle(Mat img, Point center);//绘制圆
void DrawPolygon(Mat img);//绘制多边形
void DrawLine(Mat img, Point start, Point end);//绘制线段
//-----------------------------------【ShowHelpText( )函数】----------------------------------
// 描述:输出一些帮助信息
//----------------------------------------------------------------------------------------------
void ShowHelpText()
{
//输出欢迎信息和OpenCV版本
printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n");
printf("\n\n\t\t\t此为本书OpenCV3版的第20个配套示例程序\n");
printf("\n\n\t\t\t 当前使用的OpenCV版本为:" CV_VERSION);
printf("\n\n ----------------------------------------------------------------------------\n");
}
//---------------------------------------【main( )函数】--------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始执行
//-----------------------------------------------------------------------------------------------
int main(void)
{
// 创建空白的Mat图像
Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
Mat rookImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
ShowHelpText();
// ---------------------<1>绘制化学中的原子示例图------------------------
//【1.1】先绘制出椭圆
DrawEllipse(atomImage, 90);
DrawEllipse(atomImage, 0);
DrawEllipse(atomImage, 45);
DrawEllipse(atomImage, -45);
//【1.2】再绘制圆心
DrawFilledCircle(atomImage, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2));
// ----------------------------<2>绘制组合图-----------------------------
//【2.1】先绘制出椭圆
DrawPolygon(rookImage);
// 【2.2】绘制矩形
rectangle(rookImage,
Point(0, 7 * WINDOW_WIDTH / 8),
Point(WINDOW_WIDTH, WINDOW_WIDTH),
Scalar(0, 255, 255),
-1,
8);
// 【2.3】绘制一些线段
DrawLine(rookImage, Point(0, 15 * WINDOW_WIDTH / 16), Point(WINDOW_WIDTH, 15 * WINDOW_WIDTH / 16));
DrawLine(rookImage, Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8), Point(WINDOW_WIDTH / 4, WINDOW_WIDTH));
DrawLine(rookImage, Point(WINDOW_WIDTH / 2, 7 * WINDOW_WIDTH / 8), Point(WINDOW_WIDTH / 2, WINDOW_WIDTH));
DrawLine(rookImage, Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8), Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH));
// ---------------------------<3>显示绘制出的图像------------------------
imshow(WINDOW_NAME1, atomImage);
moveWindow(WINDOW_NAME1, 0, 200);
imshow(WINDOW_NAME2, rookImage);
moveWindow(WINDOW_NAME2, WINDOW_WIDTH, 200);
waitKey(0);
return(0);
}
//-------------------------------【DrawEllipse( )函数】--------------------------------
// 描述:自定义的绘制函数,实现了绘制不同角度、相同尺寸的椭圆
//-----------------------------------------------------------------------------------------
void DrawEllipse(Mat img, double angle)
{
int thickness = 2;
int lineType = 8;
ellipse(img,
Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2),
Size(WINDOW_WIDTH / 4, WINDOW_WIDTH / 16),
angle,
0,
360,
Scalar(255, 129, 0),
thickness,
lineType);
}
//-----------------------------------【DrawFilledCircle( )函数】---------------------------
// 描述:自定义的绘制函数,实现了实心圆的绘制
//-----------------------------------------------------------------------------------------
void DrawFilledCircle(Mat img, Point center)
{
int thickness = -1;
int lineType = 8;
circle(img,
center,
WINDOW_WIDTH / 32,
Scalar(0, 0, 255),
thickness,
lineType);
}
//-----------------------------------【DrawPolygon( )函数】--------------------------
// 描述:自定义的绘制函数,实现了凹多边形的绘制
//--------------------------------------------------------------------------------------
void DrawPolygon(Mat img)
{
int lineType = 8;
//创建一些点
Point rookPoints[1][20];
rookPoints[0][0] = Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
rookPoints[0][1] = Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
rookPoints[0][2] = Point(3 * WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
rookPoints[0][3] = Point(11 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
rookPoints[0][4] = Point(19 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
rookPoints[0][5] = Point(3 * WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
rookPoints[0][6] = Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
rookPoints[0][7] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rookPoints[0][8] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rookPoints[0][9] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rookPoints[0][10] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rookPoints[0][11] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rookPoints[0][12] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rookPoints[0][13] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rookPoints[0][14] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rookPoints[0][15] = Point(WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
rookPoints[0][16] = Point(WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
rookPoints[0][17] = Point(13 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
rookPoints[0][18] = Point(5 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
rookPoints[0][19] = Point(WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
const Point* ppt[1] = { rookPoints[0] };
int npt[] = { 20 };
fillPoly(img,
ppt,
npt,
1,
Scalar(255, 255, 255),
lineType);
}
//-----------------------------------【DrawLine( )函数】--------------------------
// 描述:自定义的绘制函数,实现了线的绘制
//---------------------------------------------------------------------------------
void DrawLine(Mat img, Point start, Point end)
{
int thickness = 2;
int lineType = 8;
line(img,
start,
end,
Scalar(0, 0, 0),
thickness,
lineType);
}
打开视频
int main()
{
//【1】读取视频文件
VideoCapture capture("D:\\vslianxi\\video\\1.mp4");
//【2】循环显示每一帧
while (true)
{
Mat frameImg; //定义一个Mat变量,用于存储每一帧的图像
capture >> frameImg; //读取当前帧
/*若视频播放完成,退出循环*/
if (frameImg.empty())
{
break;
}
imshow("读取视频", frameImg); //显示当前帧
waitKey(40); //每帧画面存在40ms,即1秒25帧
}
return 0;
}
颜色
https://www.jianshu.com/p/4222b144310b
转换码 code
解释
CV_BGR2RGBCV_RGB2BGRCV_RGBA2BGRACV_BGRA2RGBA
在RGB或BGR色彩空间之间转换(包括或者不包括alpha 通道)
CV_RGB2RGBACV_BGR2BGRA
在 RGB或BGR图像中加入alpha 通道
CV_RGBA2RGBCV_BGRA2BGR
从 RGB或BGR图像中删除alpha 通道
CV_RGB2BGRACV_RGBA2BGRCV_BGRA2RGBCV_BGR2RGBA
加入或者移除alpha通道时,转换RGB到BGR 色彩空间
CV_RGB2GRAYCV_BGR2GRAY
转换RGB或者BGR色彩空间为灰度空间
CV_GRAY2RGBCV_GRAY2BGRCV_RGBA2GRAYCV_BGRA2GRAY
转换灰度为RGB或者BGR色彩空间(在进程中选择移除alpha通道)
CV_GRAY2RGBACV_GRAY2BGRA
转换灰度为RGB或者BGR色彩空间并且加入alpha通道
CV_RGB2BGR565CV_BGR2BGR565CV_BGR5652RGBCV_BGR5652BGRCV_RGBA2BGR565CV_BGRA2BGR565CV_BGR5652RGBACV_BGR5652BGRA
在从RGB或者BGR色彩空间转换到BGR565彩色图画时,选择加入或者移除alpha通道 (16位图)
CV_GRAY2BGR565CV_BGR5652GRAY
转换灰度为BGR565彩色图像或者反变换(16位图)
CV_RGB2BGR555CV_BGR2BGR555CV_BGR5552RGBCV_BGR5552BGRCV_RGBA2BGR555CV_BGRA2BGR555
在从RGB或者BGR色彩空间转换到BGR555色彩空间时,选择加入或者移除alpha通道(16位图)
CV_GRAY2BGR555CV_BGR5552GRAY
转换灰度到BGR555色彩空间或者反变换(16位图)
CV_RGB2XYZCV_BGR2XYZCV_XYZ2RGBCV_XYZ2BGR
转换RGB或者BGR色彩空间到CIE XYZ色彩空间或者反变换(Rec 709和D65 白点)
CV_RGB2YCrCbCV_BGR2YCrCbCV_YCrCb2RGBCV_YCrCb2BGR
转换RGB 或者BGR色彩空间到luma-chroma (aka YCC)色彩空间
CV_RGB2HSVCV_BGR2HSVCV_HSV2RGBCV_HSV2BGR
转换RGB或者BGR色彩空间到HSV(hue,saturation,value)色彩空间或反变换
CV_RGB2HLSCV_BGR2HLSCV_HLS2RGBCV_HLS2BGR
转换RGB或者BGR色彩空间到HLS(hue,Lightness,saturation)色彩空间或反变换
CV_RGB2LabCV_BGR2LabCV_Lab2RGBCV_Lab2BGR
转换RGB或者BGR色彩空间到CIE LAB色彩空间或反变换
CV_RGB2LuvCV_BGR2LuvCV_Luv2RGBCV_Luv2BGR
转换RGB或者BGR色彩空间到CIE Luv色彩空间
CV_BayerBG2RGBCV_BayerGB2RGB
转换Bayer模式(单通道)到RGB或者BGR色彩空间
CV_BayerRG2RGBCV_BayerGR2RGBCV_BayerBG2BGRCV_BayerGB2BGRCV_BayerRG2BGRCV_BayerGR2BGR…
… …