腐蚀:
/* erodes input image (applies minimum filter) one or more times. If element pointer is NULL, 3x3 rectangular element is used */ CVAPI(void) cvErode( const CvArr* src, CvArr* dst, IplConvKernel* element CV_DEFAULT(NULL), int iterations CV_DEFAULT(1) );
src和dst为输入和输出图像
element为核,默认为参考点位于中心的3*3矩形核
iterations为迭代次数
膨胀:
/* dilates input image (applies maximum filter) one or more times. If element pointer is NULL, 3x3 rectangular element is used */ CVAPI(void) cvDilate( const CvArr* src, CvArr* dst, IplConvKernel* element CV_DEFAULT(NULL), int iterations CV_DEFAULT(1) );
参数说明同上
自定义核:
/* creates structuring element used for morphological operations */ CVAPI(IplConvKernel*) cvCreateStructuringElementEx( int cols, int rows, int anchor_x, int anchor_y, int shape, int* values CV_DEFAULT(NULL) ); /* releases structuring element */ CVAPI(void) cvReleaseStructuringElement( IplConvKernel** element );
cvCreateStructuringElementEx创建核,cols和rows确定了核矩形大小,anchor_x和anchor_y为参考点坐标
shape可以有以下值:
/* Shapes of a structuring element for morphological operations */ enum { CV_SHAPE_RECT =0, //矩形 CV_SHAPE_CROSS =1, //十字交叉形 CV_SHAPE_ELLIPSE =2, //椭圆形 CV_SHAPE_CUSTOM =100 //用户自定义 };
values是当shape为CV_SHAPE_CUSTOM时,定义核的形状
更通用的形态学:
/* Performs complex morphological transformation */ CVAPI(void) cvMorphologyEx( const CvArr* src, CvArr* dst, CvArr* temp, IplConvKernel* element, int operation, int iterations CV_DEFAULT(1) );
src、dst、element和iterations同上
temp用的到地方:
CV_MOP_GRADIENT
CV_MOP_TOPHAT和CV_MOP_BLACKHAT 在src==dst下
operation有以下值:
/* Morphological operations */ enum { CV_MOP_ERODE =0, //腐蚀 CV_MOP_DILATE =1, //膨胀 CV_MOP_OPEN =2, //开运算 CV_MOP_CLOSE =3, //闭运算 CV_MOP_GRADIENT =4, //形态梯度 CV_MOP_TOPHAT =5, //“礼帽” CV_MOP_BLACKHAT =6 //“黑帽” };
开运算:先腐蚀后膨胀
闭运算:先膨胀后腐蚀
形态梯度:膨胀-腐蚀
礼帽:原图像-开运算
黑帽:闭运算-原图像