标签:style blog http io ar color os 使用 sp
制作自己的filter,
主要使用filter2D函数:
先上代码:
1 #include "opencv2/opencv.hpp" 2 #include<iostream> 3 #include<string> 4 5 using namespace std; 6 using namespace cv; 7 8 int main(void) 9 { 10 Mat src,dst; 11 12 Mat kernel; 13 Point anthor; 14 double delta; 15 int kernel_size; 16 int ddepth; 17 string win_name="filter2D_DEMO"; 18 19 int c; 20 21 src=imread("lena.jpg"); 22 23 if(!src.data) 24 return -1; 25 namedWindow(win_name,CV_WINDOW_AUTOSIZE); 26 27 anthor=Point(-1,-1); 28 delta=0; 29 ddepth=-1; 30 31 int ind=0; 32 while(true) 33 { 34 c=waitKey(500); 35 if((char)c==27) 36 break; 37 kernel_size=3+2*(ind%5); 38 kernel=Mat::ones(kernel_size,kernel_size,CV_32F)/(float)(kernel_size*kernel_size); 39 40 filter2D(src,dst,ddepth,kernel,anthor,delta,BORDER_DEFAULT); 41 imshow(win_name,dst); 42 ++ind; 43 } 44 45 return 0; 46 }
然后是CMakeLists.txt
1 cmake_minimum_required(VERSION 2.8) 2 project( filter2D_demo1.cpp ) 3 find_package( OpenCV REQUIRED ) 4 add_executable( Main filter2D_demo1.cpp ) 5 target_link_libraries( Main ${OpenCV_LIBS} )
运行结果:
说实话这是动态的,所以看不到各个结果了。
说明以下各个变量作用:
anthor是锚点定义,(-1,-1)是默认在kernel中央。
kernel在这里使用的是奇数矩阵,并且需要除去他本身行列数的乘积,类似这样
ddepth:结果矩阵大小,默认-1就是与原矩阵一样(文档这样说
ddepth: The depth of dst. A negative value (such as −1) indicates that the depth is the same as the source.)
delta:在每个被卷积的像素上附加的值,默认为0;
BORDER_DEFAULT:这个参数是默认值,具体没说,文档后面可能会提到。
重新写了一个分开的图像:
1 #include "opencv2/opencv.hpp" 2 #include<iostream> 3 #include<string> 4 5 using namespace std; 6 using namespace cv; 7 8 void showImg(const string &win_name,const Mat &img) 9 { 10 namedWindow(win_name,CV_WINDOW_AUTOSIZE); 11 imshow(win_name,img); 12 } 13 14 int main(void) 15 { 16 Mat src,dst; 17 18 Mat kernel; 19 Point anthor; 20 double delta; 21 int kernel_size; 22 int ddepth; 23 24 src=imread("lena.jpg"); 25 showImg("Src",src); 26 27 if(!src.data) 28 return -1; 29 30 anthor=Point(-1,-1); 31 delta=0; 32 ddepth=-1; 33 34 int ind=0; 35 36 //#Img1 37 kernel_size=3+2*(ind%5); 38 kernel=Mat::ones(kernel_size,kernel_size,CV_32F)/(float)(kernel_size*kernel_size); 39 filter2D(src,dst,ddepth,kernel,anthor,delta,BORDER_DEFAULT); 40 showImg("Dst1",dst); 41 42 ++ind; 43 //#Img2 44 kernel_size=3+2*(ind%5); 45 kernel=Mat::ones(kernel_size,kernel_size,CV_32F)/(float)(kernel_size*kernel_size); 46 filter2D(src,dst,ddepth,kernel,anthor,delta,BORDER_DEFAULT); 47 showImg("Dst2",dst); 48 49 ++ind; 50 //#Img3 51 kernel_size=3+2*(ind%5); 52 kernel=Mat::ones(kernel_size,kernel_size,CV_32F)/(float)(kernel_size*kernel_size); 53 filter2D(src,dst,ddepth,kernel,anthor,delta,BORDER_DEFAULT); 54 showImg("Dst3",dst); 55 56 waitKey(); 57 58 return 0; 59 }
结果:
以上。
标签:style blog http io ar color os 使用 sp
原文地址:http://www.cnblogs.com/lhyz/p/4158743.html