Detailed explanation of the morphological operation (corrosion and expansion) principle of python-opencv

Morphological manipulation

  1. Removing noise.
  2. Isolation of individual elements and joining disparate elements in an image.
  3. Finding of intensity bumps or holes in an image.

The most basic morphological operations are erosion and dilation. Let's understand these operations in more detail.

Erosion Corrosion

principle

It erodes the boundaries of foreground objects and removes small-scale details from the image, but at the same time reduces the size of the region of interest.

In this operation, an odd-sized convolution kernel of any shape in the image is convolved, and if all pixels under the kernel are 1, the pixel (1 or 0) in the original image is considered to be 1, otherwise it is eroded , even if it is zero.

So, depending on the size of the kernel, all pixels close to the border will be discarded. Thus the thickness or size of foreground objects is reduced or white areas in the image are reduced.

This method is suitable for removing small white noise and separating two connected objects.

Basically, it computes local minima over a given kernel region. The thickness or size of foreground objects will be reduced. In other words, the white areas in the image will be reduced.

function prototype

cv2.erode(src, kernel, dst,anchor,iterations,borderType,borderValue)
src: input image

kernel: Structural element for erosion If element = Mat(), use a 3 × 3 rectangular structural element. Kernels can be created using getStructuringElement.

dst: It is the output image of the same size and type as src.

anchor: It is a variable of integer type, representing the anchor point, and its default value point is (-1, -1), which means that the anchor point is located at the center of the kernel.

borderType: Whether to apply a border, specify the border type

iterations: number of iterations to apply erosion

borderValue: If it is a constant border type, you need to specify a value

Return Value: return an image

example

#腐蚀操作
import cv2
import matplotlib.pyplot as plt
import numpy as np

img =cv2.imread("./cycle.png",cv2.IMREAD_COLOR)
cv2.imshow("cycle",img)

kernel = np.ones((5,5),np.uint8)              #设置kenenel大小
erosion_1 = cv2.erode(img,kernel,iterations=2)
erosion_2 = cv2.erode(img,kernel,iterations=3)
erosion_3 = cv2.erode(img,kernel,iterations=4)
res = np.hstack((erosion_1,erosion_2,erosion_3)) # 参数的传入为tuple
cv2.imshow("res",res)
cv2.waitKey(0)
cv2.destroyAllWindows()

The results show

Original
insert image description here
result
insert image description here

Dilation

principle

Dilation operations typically use a construction element to detect and expand the shapes contained in the input image. This effect is the opposite of erosion
. In this operation, an arbitrary shape of an odd-sized convolution kernel is convolved in the image. If at least one pixel under the kernel is 1, the pixel element is 1.

So it increases the white area in the image or the size of the foreground object increases.

To remove noise, erosion is followed by dilation. Because, erosion removes white noise, but also shrinks our objects. So we expand it. Because the noise is gone, they don't come back, but our target area increases.

It is also useful when connecting broken parts of objects.

function prototype

cv2.dilate(src, kernel, dst,anchor,iterations,borderType,borderValue)
src: input image

kernel: Structural element for expansion If element = Mat(), use a 3 × 3 rectangular structural element. Kernels can be created using getStructuringElement.

dst: It is the output image of the same size and type as src.

anchor: It is a variable of integer type, representing the anchor point, and its default value point is (-1, -1), which means that the anchor point is located at the center of the kernel.

borderType: Whether to apply a border, specify the border type

iterations: number of iterations to apply dilation

borderValue: If it is a constant border type, you need to specify a value

Return Value: return an image

example

#腐蚀还原
import cv2
import matplotlib.pyplot as plt
import numpy as np

img =cv2.imread("./cycle.png",cv2.IMREAD_COLOR)
cv2.imshow("cycle",img)

kernel = np.ones((5,5),np.uint8)              #设置kenenel大小
erosion = cv2.erode(img,kernel,iterations=3)  # 腐蚀去除白噪点
cv2.imshow("erosion",erosion)
dilate = cv2.dilate(erosion,kernel,iterations=3) # 膨胀还原图形
cv2.imshow("dilate",dilate)

#res = np.hstack((erosion_1,erosion_2,erosion_3)) # 参数的传入为tuple

cv2.waitKey(0)
cv2.destroyAllWindows()

Result display

Original picture
insert image description here
After corrosion
insert image description here
After expansion and reduction
insert image description here

Guess you like

Origin blog.csdn.net/qq_38505858/article/details/126782368