1.开运算:CV_MOP_OPEN,先腐蚀(cvErode)再膨胀(cvDilate),去除亮点,可以将做了阀值化细胞图像的细胞分开
2.闭运算:CV_MOP_CLOSE,先膨胀(cvDilate)再cvErode,去除暗点
3.形态学梯度:cvDilate-cvErode,用于求边界
4.礼帽:src-开运算,突出亮点
5.黑帽:闭运算-src,突出暗点
程序:
代码:
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include
int morphology(int argc,char** argv)
{
IplImage* src=cvLoadImage("e:\\picture\\123.bmp",0); //不一定要是黑白的
IplImage* dst1=cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
IplImage* dst2=cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
IplImage* dst3=cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
IplImage* dst4=cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
IplImage* dst5=cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
IplImage* temp=cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
cvZero(dst1);
cvZero(dst2);
cvZero(dst3);
cvZero(dst4);
cvZero(dst5);
cvZero(temp);
cvNamedWindow("src");
cvNamedWindow("open");
cvNamedWindow("close");
cvNamedWindow("gradient");
cvNamedWindow("tophat");
cvNamedWindow("blackhat");
int value[]={1,1,1,
1,1,1,
1,1,1
};
IplConvKernel* kernel=cvCreateStructuringElementEx(3,3,1,1,CV_SHAPE_CUSTOM,value); //构建3*3的核
cvMorphologyEx(src,dst1,NULL,kernel,CV_MOP_OPEN,1); //开运算
cvMorphologyEx(src,dst2,NULL,kernel,CV_MOP_CLOSE,1); //闭运算
cvMorphologyEx(src,dst3,NULL,kernel,CV_MOP_GRADIENT,1);//形态学梯度
cvMorphologyEx(src,dst4,temp,kernel,CV_MOP_TOPHAT,1); //礼帽
cvMorphologyEx(src,dst5,temp,kernel,CV_MOP_BLACKHAT,1); //黑帽
cvShowImage("src",src);
cvShowImage("open",dst1);
cvShowImage("close",dst2);
cvShowImage("gradient",dst3);
cvShowImage("tophat",dst4);
cvShowImage("blackhat",dst5);
cvWaitKey(0);
cvDestroyWindow("src");
cvDestroyWindow("open");
cvDestroyWindow("close");
cvDestroyWindow("gradient");
cvDestroyWindow("tophat");
cvDestroyWindow("blackhat");
cvReleaseImage(&src);
cvReleaseImage(&dst1);
cvReleaseImage(&dst2);
cvReleaseImage(&dst3);
cvReleaseImage(&dst4);
cvReleaseImage(&dst5);
cvReleaseImage(&temp);
return 0;
}