1 #include<iostream> 2 #include <opencv2/core/core.hpp> 3 #include <opencv2/highgui/highgui.hpp> 4 using namespace cv;
1 Mat img4 = imread("LinuxLogo.jpg"); 2 // 创建画"窗口 3 namedWindow("4"); 4 // 在窗口中显示游戏原画 5 imshow("4", img4); 6 // 等待6000 ms后窗口自动关闭 7 waitKey(0);
ROI : region of interest
嵌入(覆盖图像),mask法
1 Mat console = imread("console.jpg"); //载入图像到Mat 2 Mat sight1 = imread("sight1.jpg"); 3 namedWindow("sight1.jpg"); 4 namedWindow("控制台"); 5 imshow("控制台",console); 6 imshow("sight1.jpg",sight1); 7 8 //设定区域 9 Mat ROI = console(Rect(100,100,sight1.cols,sight1.rows)); 10 11 //读入mask 12 Mat mask = imread("sight1.jpg"); 13 14 //将目标拷贝到ROI 15 sight1.copyTo(ROI,mask); 16 17 //显示 18 namedWindow("add"); 19 imshow("add",console); 20 21 waitKey(); 22 return 0;
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow
图片路径问题
1 //【0】定义一些局部变量 2 double alphaValue = 0.5; 3 double betaValue; 4 Mat srcImage2,srcImage3, dstImage; 5 6 //【1】读取图像 ( 两幅图片需为同样的类型和尺寸 ) 7 srcImage2 = imread("moon.jpg"); 8 srcImage3 = imread("road.jpg"); 9 10 if (!srcImage2.data) { printf("读取srcImage2错误~! \n"); return false; } 11 if (!srcImage3.data) { printf("读取srcImage3错误~! \n"); return false; } 12 13 //【2】做图像混合加权操作 14 betaValue = (1.0 - alphaValue); 15 addWeighted(srcImage2, alphaValue, srcImage3, betaValue, 0.0, dstImage); 16 17 //【3】创建并显示原图窗口 18 namedWindow("原图 by 强哥"); 19 imshow("原图 by 强哥", srcImage2); 20 21 namedWindow("线性混合图 by强哥"); 22 imshow("线性混合图 by强哥", dstImage); 23 waitKey(0); 24 25 return 0;
1 void addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1);
dst = src1[I]*alpha+ src2[I]*beta + gamma;
src1与src2的通道数需要相同,否则会出现以下问题
1 OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array') in arithm_op
1 split(srcImage,channels);//分离色彩通道 2 3 //【3】将原图的红色通道引用返回给imageBlueChannel,注意是引用,相当于两者等价,修改其中一个另一个跟着变 4 imageBlueChannel=channels.at(2); 5 6 //【4】将原图的蓝色通道的(500,250)坐标处右下方的一块区域和logo图进行加权操作,将得到的混合结果存到imageBlueChannel中 7 addWeighted(imageBlueChannel(Range(0,0 + logoImage.rows),Range(0,0 + logoImage.cols)),0.9, logoImage,0.5,0,imageBlueChannel(Range(0,0 + logoImage.rows),Range(0,0 + logoImage.cols)));
codeblock乱码问题解决
图像的深度,位深
线性滤波:方框滤波,均值滤波,高斯滤波
非线性滤波:中值滤波,双边滤波
均值滤波:不能很好地保护图像细节,在图像去噪的同时也破坏了图像的细节部分,从而使图像变得模糊,不能很好地去除噪声点。特殊的boxfilter
1 #include<iostream> 2 #include<opencv2/highgui/highgui.hpp> 3 #include<opencv2/core/core.hpp> 4 #include<opencv2/imgproc/imgproc.hpp> 5 6 using namespace std; 7 using namespace cv; 8 9 Mat g_src,g_dst1,g_dst2,g_dst3,g_dst4,g_dst5; 10 int g_box_core = 3; 11 int g_blur_core = 3; 12 int g_gau_core = 3; 13 int g_median_core = 3; 14 int g_bila_core = 10; 15 16 static void on_box(int,void*); 17 static void on_blur(int,void*); 18 static void on_gau(int,void*); 19 static void on_median(int,void*); 20 static void on_bila(int,void*); 21 22 int main() 23 { 24 //读原图 25 g_src = imread("pool.jpg"); 26 27 //克隆三个图到dest 28 g_dst1 = g_src.clone(); 29 g_dst2 = g_src.clone(); 30 g_dst3 = g_src.clone(); 31 g_dst4 = g_src.clone(); 32 g_dst5 = g_src.clone(); 33 34 namedWindow("原图"); 35 imshow("原图",g_src); 36 37 38 //boxFiter 39 namedWindow("box"); 40 createTrackbar("core","box",&g_box_core,50,on_box); 41 on_box(g_box_core,0); 42 // imshow("box",g_dst1); 43 44 //blur 45 namedWindow("blur"); 46 createTrackbar("core","blur",&g_blur_core,50,on_blur); 47 on_blur(g_blur_core,0); 48 // imshow("blur",g_dst2); 49 50 //GaussionBlur 51 namedWindow("gau"); 52 createTrackbar("core","gau",&g_gau_core,60,on_gau); 53 on_gau(g_gau_core,0); 54 // imshow("gau",g_dst3); 55 56 namedWindow("median"); 57 createTrackbar("core","median",&g_median_core,50,on_median); 58 on_median(g_median_core,0); 59 60 namedWindow("bila"); 61 createTrackbar("core","bila",&g_bila_core,50,on_bila); 62 on_bila(g_bila_core,0); 63 // imshow("bila",g_dst5); 64 65 waitKey(0); 66 return 0; 67 } 68 69 static void on_box(int,void*) 70 { 71 //trackbar最小值为1,此处保证模板存在 72 boxFilter(g_src,g_dst1,-1,Size(g_box_core + 1,g_box_core + 1)); 73 74 imshow("box",g_dst1); 75 } 76 77 static void on_blur(int,void*) 78 { 79 80 blur(g_src,g_dst2,Size(g_blur_core + 1,g_blur_core + 1)); 81 82 imshow("blur",g_dst2); 83 } 84 85 static void on_gau(int,void*) 86 { 87 //参数必须为奇数 88 GaussianBlur(g_src,g_dst3,Size(g_gau_core * 2 + 1,g_gau_core * 2 + 1 ),0,0); 89 90 imshow("gau",g_dst3); 91 } 92 93 static void on_median(int,void*) 94 { 95 medianBlur(g_src,g_dst4,g_median_core * 2 + 1); 96 97 imshow("median",g_dst4); 98 } 99 100 static void on_bila(int,void*) 101 { 102 bilateralFilter(g_src,g_dst5,g_bila_core,g_bila_core*2,g_bila_core/2); 103 104 imshow("blia",g_dst5); 105 }
1 Mat srcImage; 2 g_srcImage.copyTo(srcImage); 3 int resultImage_cols = g_srcImage.cols - g_tempalteImage.cols + 1; //需要处理参数 4 int resultImage_rows = g_srcImage.rows - g_tempalteImage.rows + 1; //需要处理参数 5 g_resultImage.create(resultImage_cols, resultImage_rows, CV_32FC1); 6 7 matchTemplate(g_srcImage, g_tempalteImage, g_resultImage, g_nMatchMethod); //注意参数 8 normalize(g_resultImage, g_resultImage, 0, 2, NORM_MINMAX, -1, Mat()); 9 double minValue, maxValue; 10 Point minLocation, maxLocation, matchLocation; 11 minMaxLoc(g_resultImage, &minValue, &maxValue, &minLocation, &maxLocation); 12 13 if (g_nMatchMethod == TM_SQDIFF || g_nMatchMethod == CV_TM_SQDIFF_NORMED) //寻找匹配点 14 { 15 matchLocation = minLocation; 16 } 17 else 18 { 19 matchLocation = maxLocation; 20 } 21 22 rectangle(srcImage, matchLocation, Point(matchLocation.x + g_tempalteImage.cols, matchLocation.y + g_tempalteImage.rows), Scalar(0, 0, 255), 2, 8, 0); 23 rectangle(g_resultImage, matchLocation, Point(matchLocation.x + g_tempalteImage.cols, matchLocation.y + g_tempalteImage.rows), Scalar(0, 0, 255), 2, 8, 0); 24 25 imshow("原始图", srcImage); 26 imshow("效果图", g_resultImage);