OpenCV学习笔记-形态学操作-腐蚀膨胀-开闭运算
一个可以调节参数的形态学例题,用到的自定义核函数为:
IplConvKernel * cvCreateStructuringElementEx(int cols,int rows,int anchor_x,int anchor_y,int shape,int *value=NULL);
函数中参数cols,rows确定了构造的矩形大小。
anchor_x,anchor_y确定了封闭矩形内参考点的横纵坐标。
shape是自定义核的形状,具体为
1.CV_SHAPE_RECT 核是矩形
2.CV_SHAPE_CROSS 核是勺子交叉形
3.CV_SHAPE_ELLIPSE 核是椭圆形
4.CV_SHAPE_CUSTOM 核是用户自定义类型
其释放函数为
void cvReleaseStructuringElement( IplConvKernel **element);
实例代码如下:
#include #include #include #include IplImage* src = 0;IplImage* dst = 0;IplConvKernel* element = 0;int element_shape = CV_SHAPE_RECT;//the address of variable which receives trackbar position update int max_iters = 10;int open_close_pos = 0;int erode_dilate_pos = 0;// callback function for open/close trackbarvoid OpenClose(int pos) { int n = open_close_pos - max_iters; int an = n > 0 ? n : -n; element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 ); if( n < 0 ) { cvErode(src,dst,element,1); cvDilate(dst,dst,element,1); } else { cvDilate(src,dst,element,1); cvErode(dst,dst,element,1); } cvReleaseStructuringElement(&element); cvShowImage("Open/Close",dst);} // callback function for erode/dilate trackbarvoid ErodeDilate(int pos) { int n = erode_dilate_pos - max_iters; int an = n > 0 ? n : -n; element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 ); if( n < 0 ) { cvErode(src,dst,element,1); } else { cvDilate(src,dst,element,1); } cvReleaseStructuringElement(&element); cvShowImage("Erode/Dilate",dst);} int main( int argc, char** argv ){ char* filename = argc == 2 ? argv[1] : (char*)"baboon.jpg"; if( (src = cvLoadImage(filename,1)) == 0 ) return -1; printf( "Hot keys: \n" "\tESC - quit the program\n" "\tr - use rectangle structuring element\n" "\te - use elliptic structuring element\n" "\tc - use cross-shaped structuring element\n" "\tENTER - loop through all the options\n" ); dst = cvCloneImage(src); //create windows for output images cvNamedWindow("Open/Close",1); cvNamedWindow("Erode/Dilate",1); open_close_pos = erode_dilate_pos = max_iters; cvCreateTrackbar("iterations", "Open/Close",&open_close_pos,max_iters*2+1,OpenClose); cvCreateTrackbar("iterations", "Erode/Dilate",&erode_dilate_pos,max_iters*2+1,ErodeDilate); for(;;) { int c; OpenClose(open_close_pos); ErodeDilate(erode_dilate_pos); c = cvWaitKey(0); if( (char)c == 27 ) break; if( (char)c == 'e' ) element_shape = CV_SHAPE_ELLIPSE; else if( (char)c == 'r' ) element_shape = CV_SHAPE_RECT; else if( (char)c == 'c' ) element_shape = CV_SHAPE_CROSS; else if( (char)c == '\n' ) element_shape = (element_shape + 1) % 3; } //release images cvReleaseImage(&src); cvReleaseImage(&dst); //destroy windows cvDestroyWindow("Open/Close"); cvDestroyWindow("Erode/Dilate"); return 0;}
运行结果图为:
说明:
在此例题中开运算和闭运算都是通过腐蚀-膨胀来实现的,而在OpenCV中有封装好的开闭运算可以直接调用。该函数为
void cvMorphologyEx(const CvArr * src,CvArr *dst,CvArr *temp,IplConvKernel *element,int operation,int iterations=1)
参考资料:
2.学习OpenCV,于仕祺,刘瑞祯,清华大学出版社,134-139
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~