Image processing basics: Python implementation of image expansion, erosion, opening and closing operations, and gradient operations

Table of contents

1. Why image processing is necessary?

2. Four basic operations of image morphology

1. Image expansion

1.1. Basic concepts

1.2. Expansion function code display

2. Image corrosion

2.1. Basic concepts

2.2. Corrosion function code display

3. Image opening operation

3.1. Basic concepts

3.2. Open operation function code display

4. Image closing operation

4.1. Basic concepts

4.2. Code display of closed operation function

5. Image gradient operation

5.1. Basic concepts

5.2. Gradient operation function code display

3. Summary


1. Why image processing is necessary?

Image processing can effectively improve image quality problems such as low definition and deformation caused by pollution, interference and other factors through techniques such as enhancement, restoration, geometric transformation, algebraic operations, and filtering, so as to achieve subjective satisfaction for the human eye or More satisfactory results. Moreover, image processing can extract certain features of the target in the image through edge detection, image segmentation, texture analysis and other technologies to facilitate computer analysis or robot recognition.

2. Four basic operations of image morphology

1. Image expansion
1.1. Basic concepts

Image dilation is similar to "field expansion" and is a basic morphological operation, mainly used to find extremely large areas in the image. This operation expands the highlighted area or white part of the image, making the resulting image larger than the highlighted area of ​​the original image. Its operator is "⊕", which can be implemented by convolving the defined structural element or convolution kernel with the original image. In binary images, the dilation operation can be implemented using the "AND" operation. Specifically, each pixel of the image is scanned with structural elements. If at least one pixel in the area covered by the structural element has a value of 1, the value of the pixel is set to 1. In this way, the highlighted area in the image will expand, achieving a dilation effect. It should be noted that the dilation operation does not change the areas where the pixel value is 0 in the original image. The original image expansion result is shown in the figure below:

Original image:

Structural elements:

Results after expansion:

1.2. Expansion function code display

Expansion function:

cv2.dilate(img, kernel, iteration)
  • img: The original image that needs to be dilated.
  • kernel: Structural elements or convolution kernels used for dilation operations.
  • iteration: Optional parameter, indicating the number of iterations of the expansion operation. If this parameter is specified, the expansion operation will be repeated the specified number of times.

Actual case display:

wenzi = cv2.imread('wenzi.png')#读取原图像
cv2.imshow('src',wenzi)
cv2.waitKey(100000)

kernel = np.ones((2,2),np.uint8)  #设置kenenel大小
wenzi_new = cv2.dilate(wenzi,kernel,iterations=2) #膨胀操作
cv2.imshow('wenzi_new',wenzi_new)
cv2.waitKey(100000)

Original picture:

Image after dilation:

2. Image corrosion
2.1. Basic concepts

Image erosion is similar to "area encroachment" and is a basic morphological operation mainly used to find extremely small areas in the image. This operation reduces and refines the highlighted area or white part of the image, making the resulting image smaller than the highlighted area of ​​the original image. Its operator is "Θ", which can be implemented by convolving the defined structural element or convolution kernel with the original image. In binary images, the corrosion operation can be implemented using the "AND" operation. Specifically, each pixel of the image is scanned with structural elements. If the value of all pixels in the area covered by the structural element is 1, the value of the pixel is set to 1. In this way, the highlighted areas in the image will be reduced, achieving an erosion effect. It should be noted that the erosion operation does not change the areas where the pixel value is 0 in the original image. The corrosion results of the original image are shown below:

Original picture:

Corrosive elements:

Result after corrosion:

2.2. Corrosion function code display

Corrosion function:

cv2.erode(src, kernel, dst,anchor,iterations,borderType,borderValue)
  • src: Original image, that is, the image that needs to be corroded.
  • kernel: Structural elements or convolution kernels used for corrosion operations.
  • dst: Optional parameter, indicating the output image after corrosion operation. If this parameter is not specified, the function will return a new image object.
  • anchor: Optional parameter, indicating the anchor point position of the structural element. By default, the anchor point is located at the center of the structural element.
  • iterations: Optional parameter, indicating the number of iterations of the corrosion operation. If this parameter is specified, the erosion operation will be repeated the specified number of times.
  • borderType: Optional parameter, indicating the type of image boundary. If this parameter is specified, the function will perform boundary processing on the image before the erosion operation.
  • borderValue: Optional parameter, indicating the boundary value. If theborderType parameter is specified, then this parameter represents the pixel value used for boundary processing.

Actual case display:

import numpy as np
import cv2
sun = cv2.imread('sun.png')
cv2.imshow('src',sun)
cv2.waitKey(100000)

kernel = np.ones((3,3),np.uint8)  #设置kenenel大小
erosion_1 = cv2.erode(sun,kernel,iterations=5)  #进行腐蚀操作
cv2.imshow('erosion_1',erosion_1)
cv2.waitKey(100000)

Image after corrosion:

3. Image opening operation
3.1. Basic concepts

The image opening operation is a process in which the image undergoes corrosion and expansion processing in sequence. Specifically, after the image is corroded, the noise can be removed, but the image will also be compressed; then the corroded image is expanded, which can further remove the noise and restore the original image size. Open operations can remove small objects from images, separate objects at delicate points, and smooth the boundaries of larger objects without significantly changing their area.

3.2. Open operation function code display
cv2.morphologyEx(src, op, kernel, dst=None, anchor=None, iterations=None, borderType=None, borderValue=None)

The above is the function code for morphological expansion operations, which is suitable for opening operations, closing operations, gradient operations, etc., and will not be repeated below.

  • op: Specified morphological operation type, which can be opening operation (cv2.MORPH_OPEN), closing operation (cv2.MORPH_CLOSE), gradient operation ( cv2.MORPH_GRADIENT) etc.
  • dst: Optional parameter, indicating the output image after morphological operation. If this parameter is not specified, the function will return a new image object.
  • anchor: Optional parameter, indicating the anchor point position of the structural element. By default, the anchor point is located at the center of the structural element.
  • iterations: Optional parameter, indicating the number of iterations of the morphological operation. If this parameter is specified, the morphological operation will be repeated the specified number of times.
  • borderType: Optional parameter, indicating the type of image boundary. If this parameter is specified, the function will perform boundary processing on the image before morphological operations.
  • borderValue: Optional parameter, indicating the boundary value. If theborderType parameter is specified, then this parameter represents the pixel value used for boundary processing.

Actual case display:

zhiwen = cv2.imread('zhiwen.png')
cv2.imshow('src',zhiwen)
cv2.waitKey(100000)

kernel = np.ones((2,2),np.uint8)  #设置kenenel大小
zhiwen_new = cv2.morphologyEx(zhiwen,cv2.MORPH_OPEN,kernel) #开运算
cv2.imshow('zhiwen_new',zhiwen_new)
cv2.waitKey(100000)

Original image:

Open operation image result:

4. Image closing operation
4.1. Basic concepts

Image closing operation is a process in which the image is sequentially expanded and corroded. Specifically, the image first expands and then erodes, which can help close small holes inside foreground objects or remove small black spots on objects. The closing operation can fill in small lakes (i.e. small holes) and bridge small cracks, while the overall position and shape remain unchanged.

4.2. Code display of closed operation function

Based on the above opening operation, there is still some noise in the image that has not been removed. The following code performs the image closing operation.

Actual case display:

zhiwen = cv2.imread('zhiwen_duan.png')
cv2.imshow('src',zhiwen)
cv2.waitKey(100000)

kernel = np.ones((4,4),np.uint8)  #设置kenenel大小
zhiwen_new = cv2.morphologyEx(zhiwen,cv2.MORPH_CLOSE,kernel) #闭运算
cv2.imshow('zhiwen_new',zhiwen_new)
cv2.waitKey(100000)

Image closing operation result:

5. Image gradient operation
5.1. Basic concepts

Image gradient operation is a morphological operation that obtains the boundary information of the image by calculating the difference between the image expansion result and the erosion result. Specifically, the dilation operation expands the highlight area or white part of the image, while the erosion operation shrinks the highlight area in the image. Therefore, by calculating the difference between expansion and erosion, the edge contour of the object in the image can be obtained, that is, the gradient information of the image. This operation can be used to extract edge features in images, enhance image visualization, and perform tasks such as image segmentation and recognition.

5.2. Gradient operation function code display
wenzi = cv2.imread('wenzi.png')
cv2.imshow('wenzi_new',wenzi)
cv2.waitKey(100000)

kernel = np.ones((2,2),np.uint8)  #设置kenenel大小
bianyuan = cv2.morphologyEx(wenzi,cv2.MORPH_GRADIENT,kernel)#梯度运算
cv2.imshow('bianyuan',bianyuan)
cv2.waitKey(100000)

Gradient operation image results:

Original picture:

Gradient operation image results:

3. Summary

Through these morphological operations, we can better understand the basic principles and methods of image processing, master the basic skills of image processing, and lay the foundation for further image analysis and processing.

Guess you like

Origin blog.csdn.net/AI_dataloads/article/details/133818692