What is morphology

To understand corrosion, we need to understand one concept: morphology.

Morphology, also known as Mathematical Morphology, is a very important research direction in image processing. Morphology mainly extracts component information from the image, which is usually of great significance to express and describe the shape of the image, and is usually the most essential shape feature used in image understanding.

For example, in the recognition of handwritten numbers, the skeleton information can be obtained through morphological operation, and in the specific recognition, only the skeleton can be calculated. Morphological processing is very important in visual detection, word recognition, medical image processing, image compression and coding.

Morphological operation mainly includes: corrosion, expansion, open operation, closed operation, morphological gradient operation, top hat operation, black hat operation and so on. Corrosion operation and expansion operation are the basis of morphology operation. The combination of corrosion and expansion can realize open operation, close operation, morphological gradient and other operations.

So this post will focus on corrosion and expansion.

corrosion

Corrosion is one of the most basic morphological operations, which can eliminate the boundary points of the image, make the image shrink inward along the boundary, and can also remove the parts smaller than the specified structure elements. Corrosion is mainly used to “shrink” or “refine” the foreground in binary images to achieve noise reduction, element segmentation and other functions.

In the process of corrosion, a structure element is usually used to scan the image to be corroded pixel by pixel, and the corrosion result is determined according to the relationship between the structure element and the corroded image.

First, let’s look at a picture of corrosion treatment:

(1) represents the image to be corroded

(2) Structural element

(3) The orange number is the three possible positions of the structure element when it is completely inside the foreground object when traversing the image; At this point, the structural elements are located at img[2,1], img[2,2], img[2,3] respectively.

(4) Corrosion result, that is, when the structure element is completely located in the foreground image, the value of the pixel in result corresponding to its center point is set to 1; When the structure element is not completely located in the foreground image, the pixel point in result corresponding to its center point is set to 0. (by bit and)

In OpenCV, the cv2.erode() function is used to achieve the corrosion operation, which is defined as follows:

def erode(src, kernel, dst=None, anchor=None, iterations=None, borderType=None, borderValue=None) :
Copy the code

SRC: original image

The kernel: structure yuan, you can customize, but can be by function cv2. GetStructuringElement ()

Iterations: Specifies the number of iterations of the corrosion operation. The default value is 1, which indicates that only one operation is performed

As for other parameters and values, the previous blog has been introduced, not repeated here.

Let’s use this function to test the corrosion operation:

import cv2
import numpy as np

img = cv2.imread("8.jpg",cv2.IMREAD_UNCHANGED)
kernel = np.ones((9.9), np.float32)
result = cv2.erode(img,kernel)
cv2.imshow("img", img)
cv2.imshow("result", result)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, we get the effect comparison figure as follows:

As can be seen, the burrs in the original image are eroded by the erosion operation. If you want to make the erosion more serious, you can set the value of the Iterations parameter.

inflation

Expansion is the opposite of corrosion, expansion can expand the image boundary. The expansion operation merges the background points in contact with the current object (foreground) into the current object, so as to realize the outward expansion of image boundary points. If two objects in the image are close to each other, they may be joined together after expansion.

Expansion operation is very helpful to fill the gaps in the image after image segmentation. Similarly, inflation requires a structural element to operate. Now, let’s use matrices to explain how expansion works.

(1) The original drawing to be expanded

(2) Structural element

(3) the orange part refers to two situations where the center pixel of the structural element is located at [1,1] and img[3,3] when the structural element traverses the original image. In fact, there are nine such possible positions that overlap with the foreground object. Structure yuan center located in img [1, 1], img [1, 2], img [1, 3], img [2, 1], the img [2], img [2, 3], img (3, 1), img [3, 2], img [3, 3].

(4) The inflated result image result: In the structural element, when any pixel overlapped with the foreground object, the value of the pixel in the inflated result image corresponding to its center point was 1; When there is no coincidence between the structure element and the foreground object, the value of the pixel point in the expansion result image corresponding to its center point is 0. (bitwise or)

In OpenCV, it gives us cv2.dilate() to perform the image expansion operation. Its full definition is as follows:

def dilate(src, kernel, dst=None, anchor=None, iterations=None, borderType=None, borderValue=None) : 
Copy the code

SRC: original image

Kernel: indicates the structure element

Other parameters are the same as above and will not be described here. Here, we use this function to test the effect of inflation, as shown below:

import cv2
import numpy as np

img = cv2.imread("8.jpg",cv2.IMREAD_UNCHANGED)
kernel = np.ones((9.9), np.float32)
result = cv2.dilate(img,kernel)
cv2.imshow("img", img)
cv2.imshow("result", result)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, the result should look like this:

As you can see, the head of the image has been expanded to connect with the body, and the lines around it have been enlarged. To increase inflation, you can change the value of the Iterations parameter.