Learning OpenCV 腐蚀与膨胀

Base on Opencv 2.4.7 reference manual

一、腐蚀erode

1、定义:使用特定的核腐蚀图像。腐蚀是求局部最小值的操作。核B与图像卷积,即计算核B覆盖的区域的像素最小值,并把这个值赋给参考点指定的像素。这样就会使图像中的高亮区域缩小。


2、函数cvErode

void cvErode(
const CvArr* src,//输入图像
	CvArr* det,//输出图像
	IplConvKernel* element=NULL,//构造的核
	int iteration=1//重复次数
	);

3、构造核

IplConvKernel* cvCreateStructuringElementEx( 
	int cols, //核的列数
	int rows, //核的行数
	int anchor_x,// 参考点的横坐标
	int anchor_y, //参考点的纵坐标
	int shape, //核的形状
	int* values=NULL// shape为CV_SHAPE_CUSTOM时构造核用的值
	);

shape的取值:

形状值含义
CV_SHAPE_RECT矩形
CV_SHAPE_CROSS十字交叉型
CV_SHAPE_ELLIPSE椭圆型
CV_SHAPE_CUSTOM用户自定义

二、膨胀dilate

1、定义:使用特定的核膨胀图像。膨胀是求局部最大值的操作。核B与图像卷积,即计算核B覆盖的区域的像素最大值,并把这个值赋给参考点指定的像素。这样就会使图像中的高亮区域逐渐增长。膨胀和腐蚀是反操作。


2、函数cvDialte

void cvDilate(
	const CvArr* src,//输入图像
	CvArr* det,//输出图像
	IplConvKernel* element=NULL,//构造的核
	int iteration=1//重复次数
	);
3、构造核

与erode的cvCreateStructuralElementEx相同

三、例程

// cvErode cvDilate
// Win7+VS2013+OpenCV2.4.7
#include<cv.h>
#include<highgui.h>
#include<stdio.h>
//void cvErode(
//const CvArr* src,//输入图像
//	CvArr* det,//输出图像
//	IplConvKernel* element=NULL,//构造的核
//	int iteration=1//重复次数
//	);
//void cvDilate(
//	const CvArr* src,//输入图像
//	CvArr* det,//输出图像
//	IplConvKernel* element=NULL,//构造的核
//	int iteration=1//重复次数
//	);
//IplConvKernel* cvCreateStructuringElementEx( 
//	int cols, //核的列数
//	int rows, //核的行数
//	int anchor_x,// 参考点的横坐标
//	int anchor_y, //参考点的纵坐标
//	int shape, //核的形状
//	int* values=NULL// shape为CV_SHAPE_CUSTOM时构造核用的值
//	);
int main(int argc,char** argv)
{
	//check argin
	if(argc<2)
	{
		printf("not enough inputs!\n");
		return 1;
	}
	IplImage* source=cvLoadImage(argv[1]);
	if(source==NULL)
	{
		printf("Invalid image !\n");
		return 1;
	}
	cvNamedWindow("source",CV_WINDOW_AUTOSIZE);
	cvShowImage("source",source);
	IplImage* dest1=cvCreateImage(cvGetSize(source),8,3);
	IplImage* dest2=cvCreateImage(cvGetSize(source),8,3);
	cvNamedWindow("Erode",CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Dilate",CV_WINDOW_AUTOSIZE);
	//构造腐蚀用的核
	IplConvKernel* k_erode=cvCreateStructuringElementEx(
		3,
		3,
		2,
		2,
		CV_SHAPE_RECT
		);
	//腐蚀
	cvErode(source,dest1,k_erode,1);
	cvShowImage("Erode",dest1);
	printf("now erode!\n");
	//构造膨胀用的核
	IplConvKernel* k_dilate=cvCreateStructuringElementEx(
		3,
		3,
		2,
		2,
		CV_SHAPE_RECT
		);
	//膨胀
	cvDilate(source,dest2,k_dilate,1);
	cvShowImage("Dilate",dest2);
	printf("now dilate\n");
	cvWaitKey(0);
	//free
	cvReleaseStructuringElement(&k_erode);
	cvReleaseStructuringElement(&k_dilate);
	cvReleaseImage(&source);
	cvReleaseImage(&dest1);
	cvReleaseImage(&dest2);
	cvDestroyAllWindows();
	return 0;
}


运行结果:


腐蚀图中,眼睛变大了;膨胀图中,眼睛变小了。

四、附录

OpenCV 2.4.7 参考手册中对dilate和erode函数的描述:

dilate
Dilates an image by using a specific structuring element.
C++: void dilate(InputArraysrc, OutputArraydst, InputArraykernel, Pointanchor=Point(-1,-1),
int iterations=1, intborderType=BORDER_CONSTANT, const Scalar&borderValue=morphologyDefaultBorderValue())
Python: cv2. dilate(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])dst
C: voidcvDilate(const CvArr*src, CvArr*dst, IplConvKernel*element=NULL, intiterations=1)
Python: cv. Dilate(src, dst, element=None, iterations=1)None
Parameters
src – input image; the number of channels can be arbitrary, but the depth should be one of
CV_8U,CV_16U,CV_16S,CV_32F‘ or ‘‘CV_64F.
dst – output image of the same size and type assrc.
element – structuring element used for dilation; ifelement=Mat() , a 3 x 3 rectangular
structuring element is used.
anchor – position of the anchor within the element; default value(-1, -1) means that the
anchor is at the element center.
iterations – number of times dilation is applied.
borderType – pixel extrapolation method (seeborderInterpolate() for details).
borderValue – border value in case of a constant border (seecreateMorphologyFilter()
for details).
The function dilates the source image using the specified structuring element that determines the shape of a pixel
neighborhood over which the maximum is taken:
dst(x, y) =max
( x ,y):element(x,y)̸=0
src(x + x , y + y )
The function supports the in-place mode. Dilation can be applied several (iterations ) times. In case of multichannel images, each channel is processed independently.
See also:
erode(), morphologyEx() , createMorphologyFilter()
Note:
• An example using the morphological dilate operation can be found at
opencv_source_code/samples/cpp/morphology2.cpp


erode
Erodes an image by using a specific structuring element.
C++: void erode(InputArray src, OutputArraydst, InputArraykernel, Pointanchor=Point(-1,-1),
int iterations=1, intborderType=BORDER_CONSTANT, const Scalar&borderValue=morphologyDefaultBorderValue())
Python: cv2. erode(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])dst
C: voidcvErode(const CvArr* src, CvArr*dst, IplConvKernel*element=NULL, intiterations=1)
Python: cv. Erode(src, dst, element=None, iterations=1)None
Parameters
src – input image; the number of channels can be arbitrary, but the depth should be one of
CV_8U,CV_16U,CV_16S,CV_32F‘ or ‘‘CV_64F.
dst – output image of the same size and type assrc.
element – structuring element used for erosion; ifelement=Mat() , a 3 x 3 rectangular
structuring element is used.
anchor – position of the anchor within the element; default value(-1, -1) means that the
anchor is at the element center.
iterations – number of times erosion is applied.
borderType – pixel extrapolation method (seeborderInterpolate() for details).
borderValue – border value in case of a constant border (seecreateMorphologyFilter()
for details).
The function erodes the source image using the specified structuring element that determines the shape of a pixel
neighborhood over which the minimum is taken:
dst(x, y) =min
( x ,y):element(x,y)̸=0
src(x + x , y + y )




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值