Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Foundation of basic knowledge

Corrosion and expansion of the image is the image morphological operation knowledge, morphological eraser is also the first time come into contact with, looking for some simple instructions, basic meaning is to change the shape of the objects in the image, generally in the binary map used to connect the adjacent elements or separated into independent elements, simple and have a look at that, really like didn’t learn.

We still learn the corrosion and expansion application layer first, and then go back to the course after mastering it.

Erosion of image

The basic principle is as follows: select a convolution kernel to move along the image. If all pixels of the original image resulting from the convolution check are 1, the central element will keep the original pixel value, otherwise it will change to 0.

Corroded image needs to pass in two contents, one is the binary image, the other is the convolution kernel.

It disconnects connected objects in the image. This operation causes the foreground objects to become smaller and the white area of the entire image to be reduced. The intuitive feeling is that the highlight of the original image has been eroded and the image area has become thinner.

The function prototype

The prototype function of Erosion is as follows:

dst = cv2.erode(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])
Copy the code

The parameters are described as follows:

  • src: Binary diagram to be corroded;
  • kernel: Corrupts the kernel of the operation. The default is3X3Matrices can also be usedcv2.getStructuringElementThe function specifies the shape, as described below;
  • anchor: Kernel matrix anchor point, no value or value is(1, 1)Then the central position of the kernel matrix is taken as the anchor point.
  • iterations: Number of corrosion times, default is 1.
  • borderType

The kernel for nuclear matrix, the structural elements can be rectangular, elliptical, cross, is through cv2. GetStructuringElement function to generate different shape of structural elements.

  • kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)): rectangular structure;
  • kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)): elliptical structure;
  • kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5)): cruciform structure.

The test code is shown below, and you can learn from the comments again.

import cv2 as cv
import numpy as np

# image reading
src = cv.imread("./t2.png")
# Grayscale image
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
# binarization
ret, binary = cv.threshold(gray, 0.255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
cv.imshow("binary", binary)
# convolution kernels
kernel = np.ones((3.3), np.uint8)

The default iteration is 1
dst = cv.erode(binary, kernel)

# Image display
cv.imshow("src", src)
cv.imshow("dst", dst)

cv.waitKey(0)
cv.destroyAllWindows()
Copy the code

After running, the effect is as follows, from left to right are the original picture, binarization picture, corrosion picture respectively.

If the original image is corroded directly, the effect is not very obvious.

If you adjust the corrosion iteration parameters, such as the following code, the image will be completely corroded.

dst = cv.erode(binary, kernel,iterations=3)
Copy the code

Image dilation

Once you have corrosion, it’s easy to understand inflation, which is a pair of opposite concepts.

Expansion is to replace the pixel in the center with the largest pixel under the position covered by the convolution kernel.

The realization principle is as follows: when the center point of the convolution kernel scans each pixel point in the original image one by one, if one pixel point in the scanned original image is found to have a value of 1, it is 1; otherwise, it is 0. Expansion can be used to connect two separate objects.

The function prototype

dst = cv2.dilate(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])
Copy the code

Parameters are not specified, consistent with corrosion content. After running with the following code, the resulting graph is noticeably fatter.

import cv2 as cv
import numpy as np


# image reading
src = cv.imread("./t2.png")
# Grayscale image
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
# binarization
ret, binary = cv.threshold(gray, 0.255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)

# convolution kernels
kernel = np.ones((3.3), np.uint8)

The default iteration is 1
dst = cv.dilate(binary, kernel)

# Image display
cv.imshow("src", src)
cv.imshow("binary", binary)

cv.imshow("dst", dst)

cv.waitKey(0)
cv.destroyAllWindows()
Copy the code

In the end, I also learned some techniques, such as corrosion first and then expansion when removing noise, or corrosion and expansion of color images directly without gray scale and binarization, but the effect was not very ideal.

The effects of expansion, though not yet fully exposed, can be recorded (as opposed to corrosion) :

  • Object size increases by one pixel (3×3);
  • Smooth object edges;
  • Reduces or fills the distance between objects.