Expansion and corrosion

expand

As long as one of the pixel values of the original image corresponding to the convolution kernel is 1, the pixel value of the central element is 1. Increases the white area (foreground) in the image. Generally, corrosion is used first and then expansion is used to remove noise. Because corrosion will not only remove the white noise, but also make the foreground object smaller. So we inflate him again. At this time, the noise has been removed and will not come back, but the prospect is still there and will increase.

effect:

1. Increase the size of the object by one pixel (3) × 3)
2. Smooth object edges
3. Reduce or fill edges between objects

cv2.dilate

Inflate the image with specific structural elements

```dilate(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) -> dst
```
• src: indicates the input picture
• kernel: indicates the size of the structural element used for expansion
• iteration: indicates the number of iterations

Example

Binarize and expand the image

```def dilate(image):
"""expand"""
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)  # Binary image
cv.imshow("binary", binary)

# Structure 5 × 5, which can be cross shaped, diamond shaped, square and X-shaped
kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))  # Structure 5 × Square structure element of 5
dst = cv.dilate(binary, kernel=kernel)  # expand
cv.imshow("dilate", dst)
```

result

Note: for the expansion and corrosion of binary image, pay attention to whether white or black is used as the foreground

Example

Expand the color map

```def dilate_color(image):
"""Color expansion"""
kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))  # Structure 5 × Square structure element of 5
dst = cv.dilate(image, kernel=kernel)  # expand
cv.imshow("dilate", dst)
```

result:

corrosion

The convolution kernel slides along the image. If all pixel values of the original image corresponding to the convolution kernel are 1, the central element will maintain the original pixel value, otherwise it will become zero. According to the size of the convolution kernel, all pixels close to the foreground will be corroded (become 0), so the foreground object will become smaller and the white area of the whole image will be reduced.

effect:

1. Reduce the size of the object by one pixel (3) × 3)
2. Smooth object edges
3. Weaken or split Peninsula connections between objects

cv2.erode

Corrosion images using specific structural elements

```erode(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) -> dst
```
• src: indicates the input picture
• kernel: indicates the size of structural elements used for corrosion
• iteration: indicates the number of iterations

Example

Binarization and corrosion of image

```def erode(image):
"""corrosion"""
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)  # Binary image
cv.imshow("binary", binary)

# Structure 5 × 5, which can be cross shaped, diamond shaped, square and X-shaped
kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))  # Structure 5 × Square structure element of 5
dst = cv.erode(binary, kernel=kernel)  # corrosion
cv.imshow("erode", dst)
```

result

Example

Corrosion of color pictures

```def erode_color(image):
"""Color corrosion"""
kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))  # Structure 5 × Square structure element of 5
dst = cv.erode(image, kernel=kernel)  # corrosion
cv.imshow("erode", dst)
```

result

Morphological operation

cv2.morphologyEx

Perform morphological operations

• Open operation: etching before expansion is called open operation, which is used to remove noise.
• Closed operation: expand first and then corrode. It is often used to fill small holes in foreground objects or small black spots on foreground objects.
• Top cap: the difference image between the original image and the opening operation
• Black hat: the difference image between the closing operation and the original image
• Morphological gradient: in fact, it is the difference between expansion and corrosion of an image. The result looks like the outline of the foreground object
• Basic gradient: the difference image obtained by subtracting the corroded image from the expanded image.
• Internal gradient: the difference image obtained by subtracting the corrosion image from the original image.
• External gradient: the difference image obtained by subtracting the original image from the expanded image
```morphologyEx(src, op, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) -> dst
```
• src: input image
• op: operation type
• MORTH_OPEN: open function
• MORTH_CLOSE: close the function
• MORTH_GRADIENT: the function performs morphological gradient operation
• MORTH_TOPHAT: the function performs the top hat operation
• MORTH_BLACKHAT: function performs black hat operation
• MORTH_DILATE: dilate function
• MORTH_ERODE: function performs corrosion operation
• Kernel: kernel type, obtained by getStructuringElement function.
• For example, if the kernel is (1,15), the vertical line is extracted, and the kernel is (15,1), the horizontal line is extracted

On operation

First corrosion and then expansion is called open operation, which is used to remove noise.

Example

On operation

```def open_image(image):
"""On operation"""
# print(image.shape)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)  # Binary image
cv.imshow("binary", binary)

kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))  # Structure 5 × Square structure element of 5
dst = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel=kernel)  # Perform morphological operation (on operation at this time)
cv.imshow("open_image", dst)
```

result

Closed operation

Expansion before corrosion. It is often used to fill small holes in foreground objects or small black spots on foreground objects.

Example

Closed operation

```def close_image(image):
"""Closed operation"""
# print(image.shape)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)  # Binary image
cv.imshow("binary", binary)

kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))  # Structure 5 × Square structure element of 5
dst = cv.morphologyEx(binary, cv.MORPH_CLOSE, kernel=kernel)  # Perform morphological operation (closed operation at this time)
cv.imshow("close_image", dst)
```

result:

Top cap

Difference image between original image and on operation
d s t = t o p h a t ( s r c , r l r m e n t ) = s r c − o p e n ( s r c , r l r m e n t ) dst = tophat(src,rlrment) = src - open(src,rlrment) dst=tophat(src,rlrment)=src−open(src,rlrment)

Because the result of the open operation is to enlarge the crack or local low brightness area, the effect image obtained by subtracting the opened image from the original image highlights the area brighter than the area around the contour of the original image, and this operation is related to the size of the selected core.

Top hat operations are often used to separate patches that are lit up near neighbors. When an image has a large background and small objects are regular, the top hat operation can be used for background extraction.

Example

```def tophat_image(image):
"""Top hat operation"""
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))  # Structure 5 × Square structure element of 5
dst = cv.morphologyEx(gray, cv.MORPH_TOPHAT, kernel=kernel)  # Perform morphological operation (top hat operation at this time)
cv.imshow("tophat_image", dst)
```

result:

Example

Direct binary image

Missing detail found in lower right corner

It is found that the brightness of the font and background in the lower right corner of the original image are somewhat high, so this situation will occur. Now if we want to separate the font from the background, we can brighten the foreground with the top hat operation.

Therefore, the top cap is carried out first and then binarized

```def comprehensive_example(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow("gray", gray)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (30, 30))  # Construction 30 × 30 square structure element
dst = cv.morphologyEx(gray, cv.MORPH_TOPHAT, kernel=kernel)  # Perform morphological operation (top hat operation at this time)
cv.imshow("tophat_image", dst)
ret, binary = cv.threshold(dst, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)  # Binary image
cv.imshow("binary", binary)
```

result:

Black hat

Close the difference image between the operation and the original image
d s t = b l a c k h a t ( s r c , r l r m e n t ) = c l o s e ( s r c , r l r m e n t ) − s r c dst = blackhat(src,rlrment) = close(src,rlrment) - src dst=blackhat(src,rlrment)=close(src,rlrment)−src

The effect image after black hat operation highlights the darker area than the area around the outline of the original image, and this operation is related to the size of the selected core.

Therefore, black hat operation is used to separate patches darker than adjacent points. You can get the outline effect picture.

Example

```def blackhat_image(image):
"""Black hat operation"""
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))  # Structure 5 × Square structure element of 5
dst = cv.morphologyEx(gray, cv.MORPH_BLACKHAT, kernel=kernel)  # Perform morphological operation (black hat operation at this time)
cv.imshow("blackhat_image", dst)
```

result:

In essence, it is the edge extraction of the image, which can also be said to be the result of expansion minus corrosion

Example

```def gardient_image(image):