Python from 0 to 1 | Understanding erosion and expansion in image morphology operations

Abstract: This article will explain the knowledge of image morphology in detail, mainly introducing image erosion processing and expansion processing.

This article is shared from Huawei Cloud Community " [Python from Zero to One] 47. Detailed Explanation of Corrosion and Expansion in Image Enhancement and Computing ", author: eastmount.

1. Theoretical knowledge of morphology

The application of mathematical morphology can simplify image data, keep their basic shape characteristics, and get rid of irrelevant structures. The algorithm of mathematical morphology has a natural parallel implementation structure, mainly for binary images (0 or 1). In image processing, binary morphology is often applied to image segmentation, thinning, skeleton extraction, edge extraction, shape analysis, corner detection, watershed algorithm, etc. Because the algorithm is simple and the algorithm can be operated in parallel, it is often applied to hardware [1-2].

Common image morphology operations include:

  • corrosion
  • expand
  • open operation
  • Close operation
  • gradient operation
  • top hat operation
  • bottom hat operation

These operations are mainly implemented in OpenCV through the MorphologyEx() function, which can use basic expansion and erosion techniques to perform more advanced morphological transformations, such as opening and closing operations, morphological gradients, top hats, black hats, etc., can also be implemented The most basic image dilation and erosion. Its function prototype is as follows:

  • dst = cv2.morphologyEx(src, model, kernel)
    – src represents the original image
    – model represents the image for morphological processing, including:
    (1) cv2.MORPH_OPEN: Opening Operation
    (2) cv2.MORPH_CLOSE: Closing operation (Closing Operation)
    (3)cv2.MORPH_GRADIENT: Morphological Gradient
    (4)cv2.MORPH_TOPHAT: Top Hat
    (5)cv2.MORPH_BLACKHAT: Black Hat
  • Kernel represents the convolution kernel, which can be constructed with the numpy.ones() function

2. Image corrosion

Image erosion (Erosion) and expansion (Dilation) are two basic morphological operations, which are mainly used to find extremely small and extremely large areas in the image. Image erosion is similar to "field erosion", which reduces and refines the highlighted areas or white parts in the image, and the operating result is smaller than the highlighted areas of the original image.

Let A and B be a set, and A is corroded by B, which is denoted as A-B, which is defined as:

This formula indicates that image A uses convolution template B to perform corrosion processing, and performs convolution calculation with template B and image A to obtain the minimum value of pixels in the area covered by B, and use this minimum value to replace the pixel value of the reference point. As shown in Figure 1, the original image A on the left is corroded into the effect image AB on the right.

Image corrosion mainly includes two input objects of binary image and convolution kernel. The convolution kernel is the key array in corrosion, which can be generated by using the Numpy library. The center point of the convolution kernel scans the original image pixel by pixel, and the scanned pixel in the original image has a value of 1 only when the element values ​​corresponding to the convolution kernel are all 1, otherwise its pixel value is modified to 0. In Python, the erode() function of OpenCV is mainly called to realize image erosion.

Its function prototype is as follows:

  • dst = cv2.erode(src, kernel, iterations)
    – src represents the original image
    – kernel represents the convolution kernel
    – iterations represents the number of iterations, the default value is 1, which means an erosion operation

You can use the function numpy.ones((5,5), numpy.uint8) to create a 5×5 convolution kernel, as follows:

The code for the image erosion operation is as follows:

# -*- coding: utf-8 -*-
# By:Eastmount
import cv2  
import numpy as np  
#读取图片
src = cv2.imread('test01.jpg', cv2.IMREAD_UNCHANGED)
#设置卷积核
kernel = np.ones((5,5), np.uint8)
#图像腐蚀处理
erosion = cv2.erode(src, kernel)
#显示图像
cv2.imshow("src", src)
cv2.imshow("result", erosion)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

The output result is shown in Figure 2. The left side represents the original image, and the right side is the image after corrosion processing. It can be found that the interfering thin lines (noise) in the image have been cleaned.

If the image after erosion still has noise, you can set the number of iterations to perform multiple erosion operations. For example, the core code for performing 9 corrosion operations is as follows:

  • erosion = cv2.erode(src, kernel,iterations=9)

The final output image after nine times of corrosion processing is shown in Figure 3.

3. Image Expansion

Image dilation is the inverse operation of the erosion operation, similar to "field expansion", which expands the highlighted area or white part in the image, and its operating result is larger than the highlighted area of ​​the original image.

Let A and B be sets, ∅ is an empty set, and A is expanded by B, which is recorded as A⊕B, where ⊕ is the expansion operator, and the expansion is defined as:

This formula indicates that image A is expanded with B, where B is a convolution template, and its shape can be square or circular. Convolution calculation is performed between template B and image A, and each pixel in the image is scanned. Use the template element and the binary image element to do "AND" operation, if both are 0, then the target pixel is 0, otherwise it is 1. In this way, the maximum value of the pixel points in the coverage area of ​​B is calculated, and this value is used to replace the pixel value of the reference point to achieve image expansion. Figure 4 is the effect image A⊕B on the right after the original image A on the left is expanded.

After the image is corroded, it will remove the noise, but at the same time it will compress the image, while the image dilation operation can remove the noise and keep the original shape, as shown in Figure 5.

In Python, the dilate() function of OpenCV is mainly called to realize image erosion. The function prototype is as follows:

  • dst = cv2.dilate(src, kernel, iterations)
    – src represents the original image
    – kernel represents the convolution kernel, which can be constructed with the numpy.ones() function
    – iterations represents the number of iterations, the default value is 1, which means an expansion operation

The code for the image dilation operation is as follows:

# -*- coding: utf-8 -*-
# By:Eastmount
import cv2  
import numpy as np  
#读取图片
src = cv2.imread('zhiwen.png', cv2.IMREAD_UNCHANGED)
#设置卷积核
kernel = np.ones((5,5), np.uint8)
#图像膨胀处理
erosion = cv2.dilate(src, kernel)
#显示图像
cv2.imshow("src", src)
cv2.imshow("result", erosion)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

The output result is shown in Figure 6:

Four. Summary

This paper mainly introduces image morphology processing, and explains image erosion processing and dilation processing in detail. Mathematical morphology is a new method applied in the field of image processing and pattern recognition. Its basic idea is to use structural elements with a certain shape to measure and extract the corresponding shape in the image to achieve the purpose of image analysis and recognition.

references:

  • [1] Written by Gonzales, translated by Ruan Qiuqi. Digital Image Processing (3rd Edition) [M]. Beijing: Electronic Industry Press, 2013.
  • [2] Ruan Qiuqi. Digital Image Processing (3rd Edition) [M]. Beijing: Electronic Industry Press, 2008.
  • [3] Mao Xingyun, Leng Xuefei. Introduction to OpenCV3 Programming [M]. Beijing: Electronic Industry Press, 2015.
  • [4] Eastmount. [Python image processing] Eight. Image corrosion and image expansion [EB/OL]. (2018-10-31). https://blog.csdn.net/Eastmount/article/details/83581277.

 

Click to follow and learn about Huawei Cloud's fresh technologies for the first time~

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4526289/blog/8907605