C++ opencv Erode and Dilate

Erode and Dilate

图像的像素值越大的地方,图像越亮,而腐蚀和膨胀就是求图像像素局部最小值和局部最大值的过程

膨胀(dilate):就是对图像的高亮部分进行膨胀,相当于高亮部分的领域扩张

腐蚀(erode):就是对图像的高亮部分的侵蚀,也就是经过腐蚀操作之后图像的高亮部分变得更少

  • The larger the pixel value of the image, the brighter the image, and corrosion and expansion is the process of finding the local minimum and local maximum of the image pixel
  • Dilate: Dilate the highlighted part of the image, equivalent to the field expansion of the highlighted part
  • Erode: The edge of the image is eroded, or the edge of the image becomes less intense after the erosion operation

1.膨胀与腐蚀的原理:

首先,在腐蚀与膨胀的操作过程中,我们得给出一个核窗口,它有一个单独定义出来的参考点,我们称之为锚点,(这个窗口的给出我们一般会结合函数getStructuringElement使用,这个函数的用法我们一会儿来介绍),然后我们把这个窗口在原图像上滑动,计算原图像在这个窗口覆盖部分之下的最值,然后把这个最值赋值给参考点指定的像素

如果这个最值是最大值,那么上述操作就是膨胀dilate,它会使原图像高亮部分的领域逐渐扩张

如果这个最值是最小值,那么上述操作就是腐蚀erode,它会使原图像高亮部分的领域被侵蚀

  • 1. Principles of expansion and corrosion:
  • First of all, in the process of corrosion and the expansion of the operation, we have to give a window, it has a separate defined reference point, we call it the anchor, (this window shows we usually getStructuringElement used in combination with functions, the function of usage to introduce to you soon), then we put this window sliding on the original image, calculate the original image under the window covering part of the value, then the most value assigned to the reference point specified pixel
  • If the maximum value is the maximum, then the above operation is dilate dilate, which will gradually expand the highlighted area of the original image
  • If this maximum is the minimum, the above operation is the erode erode, which erode the areas highlighted by the original image

2.dilate函数的使用方法:

我们来看看它的函数原型:

void dilate( const Mat& src, Mat& dst, const Mat& element,Point anchor=Point(-1,-1), int iterations=1,int borderType=BORDER_CONSTANT,

const Scalar& borderValue=morphologyDefaultBorderValue() );

第一个参数:输入图像

第二个参数:输出图像,要和原图像有一样的尺寸和类型

第三个参数:原图像类型的element,膨胀操作的核,当为NULL时表示使用的是参考点位于中心的3x3的核(通常使用函数getStructuringElement来计算)

第四个参数:锚点位置,有默认值Point(-1, -1)

第五个参数:迭代使用dilate的次数,默认值为1

第六个参数:有默认值BORDER_DEFAULT

第七个参数:const Scalar类型的 borderValue,一般不用管他

其实一般我们使用的时候,大部分只需要填前三个参数,后面四个都有默认值

  • 2. Use of DILate function:
  • Let's take a look at its functional prototype:
  • Void dilate(const Mat& SRC, Mat& DST, Const Mat& Element,Point anchor=Point(-1,-1), int iterations=1,int borderType=BORDER_CONSTANT,
  • Const Scalar & borderValue = morphologyDefaultBorderValue ());
  • The first parameter: Enter the image
  • Second parameter: output image with the same size and type as the original image
  • Third argument: element of the original image type, core of the expansion operation, when NULL means that the 3x3 core with the reference point in the center is used (usually calculated using the function getStructuringElement)
  • Fourth parameter: anchor position with default value Point(-1, -1)
  • The fifth parameter: the number of times dilate is used in the iteration, with the default value of 1
  • Parameter 6: Has the default value, BORDER_DEFAULT
  • The seventh parameter: the Const Scalar type's borderValue, which will generally leave him alone
  • In fact, when we use it, most of us only need to fill in the first three parameters, and the last four have default values

3. getStructuringElement函数的使用方法:

第一个参数:内核的形状,有下面三种形状可以选择:

MORPH_RECT : 矩形

MORPH_CROSS : 交叉形

MORPH_ELLIPSE : 椭圆形

Size类型的内核的尺寸

Point类型的锚点的位置,有默认值Point(-1, -1)

注意:交叉型的element形状唯一依赖于锚点的位置,其他情况下,锚点的位置只是影响到了形态学运算结果的偏移

Mat element = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));

  • 3. Use method of getStructuringElement function:
  • The first parameter is the shape of the kernel. There are three shapes to choose from:
  • MORPH_RECT: rectangle
  • MORPH_CROSS: MORPH_CROSS
  • Ellipse: An ellipse
  • Size the Size of the kernel
  • The location of an anchor Point of type Point, with the default value Point(-1, -1)
  • Note: Cross-shaped element shapes depend solely on the location of anchor points. In other cases, the location of anchor points only affects the deviation of morphological operations
  • Here's an example:
  • Mat Element = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));

3.dilate函数的使用方法

下面是函数原型:

void dilate( const Mat& src, Mat& dst, const Mat& element,Point anchor=Point(-1,-1), int iterations=1,int borderType=BORDER_CONSTANT,

const Scalar& borderValue=morphologyDefaultBorderValue() );

第一个参数:原图像

第二个参数:输出图像

第三个参数:和输入图像一样类型的核,通常配合getStructuringElement函数使用

第四个参数:Point类型的锚点的位置,有默认值(-1, -1)

第五个参数:int类型的迭代使用dilate函数的次数,默认值为1

第六个参数:有默认值BORDER_DEFAULT

第七个参数:const Scalar类型的 borderValue,一般不用管他

  • 3. Use of DILate function
  • Here is the function prototype:
  • Void dilate(const Mat& SRC, Mat& DST, Const Mat& Element,Point anchor=Point(-1,-1), int iterations=1,int borderType=BORDER_CONSTANT,

  • Const Scalar & borderValue = morphologyDefaultBorderValue ());

  • The first parameter: the original image

  • Second parameter: Output image

  • Third argument: a core of the same type as the input image, usually used with the getStructuringElement function

  • Fourth parameter: the location of the anchor Point of type Point, with default value (-1, -1)

  • Parameter 5: The number of times an int iteration USES the DILate function, with the default value of 1

  • Parameter 6: Has the default value, BORDER_DEFAULT

  • The seventh parameter: the Const Scalar type's borderValue, which will generally leave him alone