OpenCV (31): Morphological operations

​​​​​1. Morphological operations

       OpenCV provides a wealth of functions to perform morphological operations, including corrosion, expansion, opening operations, closing operations, etc. Here are some commonly used OpenCV morphological operation functions:

  1. Erosion: erode(src, dst, kernel, anchor, iterations, borderType, borderValue)This function performs an erosion operation on the foreground area in the input image, shrinking the foreground area by replacing the pixel values ​​in the area with the minimum value in the structural element.

  2. Dilation: dilate(src, dst, kernel, anchor, iterations, borderType, borderValue)This function performs a dilation operation on the foreground area in the input image, expanding the foreground area by replacing the pixel values ​​in the area with the maximum value in the structural element.

  3. Opening: morphologyEx(src, dst, MORPH_OPEN, kernel, anchor, iterations, borderType, borderValue)The opening operation is to first erode the image and then perform the expansion operation. This operation removes small noise and smooths the boundaries of the image.

  4. Closing: morphologyEx(src, dst, MORPH_CLOSE, kernel, anchor, iterations, borderType, borderValue)The closing operation is to first expand the image and then perform the erosion operation. This operation fills small holes and smooths the edges of the image.

  5. Gradient: morphologyEx(src, dst, MORPH_GRADIENT, kernel, anchor, iterations, borderType, borderValue)Gradient operation can highlight edges by dilating and eroding the input image.

  6. Top Hat: morphologyEx(src, dst, MORPH_TOPHAT, kernel, anchor, iterations, borderType, borderValue)Top Hat can highlight edges and details by comparing the difference between the input image and the result of the open operation.

  7. Black Hat: morphologyEx(src, dst, MORPH_BLACKHAT, kernel, anchor, iterations, borderType, borderValue)Black Hat operation can highlight edges and details by comparing the difference between the closed operation result and the input image.

In the above function, srcit is the input image, dstit is the output image, kernelit is the structural element, anchorit is the anchor point position of the structural element, it is iterationsthe number of iterations, borderTypeit is the boundary pixel processing method, and borderValueit is the value of the boundary pixel.

2. Related function morphologyEx()

MorphologyEx() is one of the functions in the OpenCV library for morphology operations. It can perform a series of morphological operations including erosion, expansion, opening operations, closing operations, etc. The prototype of this function is as follows:

void morphologyEx(

InputArray src,

OutputArray dst,

int on,

InputArray kernel,

Point anchor = Point(-1,-1),

int iterations = 1,

int borderType = BORDER_CONSTANT,

const Scalar& borderValue = morphologyDefaultBorderValue()

);

The parameters of this function include:

  • src: Input image or image array.

  • dst: output image or image array.

  • op: Specifies the type of morphological operation.

  • kernel: Structural element (kernel) used for morphological operations.

  • Anchor: The position of the center of the structural element. The default is (-1, -1), which means that the center position of the structural element is the center of the core.

  • iterations: The number of iterations to perform morphological operations, the default is 1.

  • borderType: border pixel processing method, the default is BORDER_CONSTANT.

  • borderValue: Specifies the value of the border pixel when borderType is BORDER_CONSTANT. The default is morphologyDefaultBorderValue().

Among them, the types of morphological operations:

3. Sample code:

void image_morphologyEx(Mat image){
    Mat gray;
    cvtColor(image,gray,COLOR_BGR2GRAY);//灰度化
    threshold(gray,gray,80,255,THRESH_BINARY);
    //5×5矩阵结构元素
    Mat kernel= getStructuringElement(0,Size(5,5));
    Mat open,close,gradient,tophat,blackhat;
    //对图像进行开运算
    morphologyEx(gray,open,MORPH_OPEN,kernel);
    //对图像进行闭运算
    morphologyEx(gray,close,MORPH_CLOSE,kernel);
    //对图像进行梯度运算
    morphologyEx(gray,gradient,MORPH_GRADIENT,kernel);
    //对图像进行顶帽运算
    morphologyEx(gray,tophat,MORPH_TOPHAT,kernel);
    //对图像进行黑帽运算
    morphologyEx(gray,blackhat,MORPH_BLACKHAT,kernel);
  
}

Guess you like

Origin blog.csdn.net/weixin_63357306/article/details/132763956