OpenCV学习笔记-形态学

OpenCV提供了通用的形态学函数cvMorphologyEx,该函数能够实现开运算,闭运算,形态梯度,礼帽操作,黑帽操作,例题如下:

#include "StdAfx.h"
#include "cv.h"  
#include "highgui.h"  
#include "highgui.h"  

int main(int argc, char ** argv)  
{  
	cvNamedWindow("sourceImage");  
	cvNamedWindow("open");  
	cvNamedWindow("close");  
	cvNamedWindow("gradient"); 
	cvNamedWindow("topHat"); 
	cvNamedWindow("blackHat"); 
	IplImage * src = cvLoadImage("test.bmp");  
	cvShowImage("sourceImage",src);
	IplImage * temp = cvCreateImage(cvGetSize(src), 8,3);  
	IplImage * img=cvCreateImage(cvGetSize(src), 8, 3);  
	cvCopyImage(src,temp);  
	cvCopyImage(src, img);  
	//开运算  
	cvMorphologyEx(  
		src,  
		img,  
		temp,  
		NULL, //default 3*3  
		CV_MOP_OPEN,  
		4);  
	cvShowImage("open", img);  
	//闭运算  
	cvMorphologyEx(  
		src,  
		img,  
		temp,  
		NULL, //default 3*3  
		CV_MOP_CLOSE,  
		4);  
	cvShowImage("close", img);  
	//形态梯度  
	cvMorphologyEx(  
		src,  
		img,  
		temp,  
		NULL, //default 3*3  
		CV_MOP_GRADIENT,  
		3);  
	cvShowImage("gradient", img);  
	//cvWaitKey(0);  

	//"礼帽"  
	cvMorphologyEx(  
		src,  
		img,  
		temp,  
		NULL, //default 3*3  
		CV_MOP_TOPHAT,  
		3);  
	cvShowImage("topHat", img);  
	//cvWaitKey(0);  
	//“黑帽”  
	cvMorphologyEx(  
		src,  
		img,  
		temp,  
		NULL, //default 3*3  
		CV_MOP_BLACKHAT,  
		3);  
	cvShowImage("blackHat", img);  
	cvWaitKey(0);  
	cvReleaseImage(&temp);  
	cvReleaseImage(&src);  
	cvReleaseImage(&img);  
	cvDestroyAllWindows();  
	return 0;  
} 

运行结果为:
OpenCV学习笔记-形态学_第1张图片


参考文献:

1.学习OpenCV,于仕祺,刘瑞祯,清华大学出版社,pp.139-145.

2.http://www710.univ-lyon1.fr/~bouakaz/OpenCV-0.9.5/docs/ref/OpenCVRef_ImageProcessing.htm

3.http://www710.univ-lyon1.fr/~bouakaz/OpenCV-0.9.5/docs/ref/OpenCVRef_ImageProcessing.htm

你可能感兴趣的:(null,出版)