Introduction to image processing - morphological operation

Morphological operation

1. Connectivity

In the image, the smallest unit is the pixel. There are 8 adjacent pixels around each pixel. There are three common adjacent relationships: 4 adjacent, 8 adjacent and D adjacent. As shown in the figure below:

4 adjacency: the 4 neighborhood of pixel p(x,y) is: (x+1,y); (x-1,y); (x,y+1); (x,y-1)

D adjacency: the D neighborhood of pixel p(x,y) is: the point on the diagonal (x+1,y+1); (x+1,y-1); (x-1,y+1); (x-1,y-1),

8 adjacency: the 8 neighborhood of pixel p(x,y) is: the point of 4 neighborhood + the point of D neighborhood

2. Morphological operation

Morphological transformation is a simple operation based on image shape. It is usually performed on binary images. Corrosion and expansion are two basic morphological operators. Then its variant forms, such as open operation, closed operation, top hat, black hat and so on.

2.1 corrosion and expansion

Corrosion and expansion are the most basic morphological operations. Both corrosion and expansion are for the white part (highlighted part).

Expansion is to expand the highlighted part of the image, and the effect image has a larger highlighted area than the original image; Corrosion is that the highlight area in the original image is eroded, and the effect image has a smaller highlight area than the original image. Expansion is the operation of finding local maximum value, and corrosion is the operation of finding local minimum value.

1) Corrode

The specific operation is as follows: scan each pixel in the image with A structural element, and operate with each pixel in the structural element and its covered pixel. If they are all 1, the pixel is 1, otherwise it is 0. As shown in the figure below, after structure A is corroded by structure B:

The function of corrosion is to eliminate the boundary points of the object, reduce the target, and eliminate the noise points smaller than the structural elements.

 cv.erode(img,kernel,iterations)

Parameters:

img: Image to process
kernel: Nuclear structure
iterations: The number of times of corrosion is 1 by default

2) Swell
The specific operations are as follows: scan each pixel in the image with A structural element, and operate with each pixel in the structural element and its covered pixels. If they are all 0, the pixel is 0, otherwise it is 1. As shown in the figure below, after structure A is corroded by structure B:

The function is to combine all the background points in contact with the object into the object to increase the target and supplement the holes in the target.

   cv.dilate(img,kernel,iterations)

Parameters:

img: Image to process

kernel: Nuclear structure

iterations: The number of times of corrosion is 1 by default

Example
We use a convolution kernel of 5 * 5 to realize the operation of corrosion and expansion:

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 read image
img = cv.imread("./image/image3.png")
# 2 create core structure
kernel = np.ones((5, 5), np.uint8)

# 3 image corrosion and expansion
erosion = cv.erode(img, kernel) # corrosion
dilate = cv.dilate(img,kernel) # expand

# 4 image display
fig,axes=plt.subplots(nrows=1,ncols=3,figsize=(10,8),dpi=100)
axes[0].imshow(img)
axes[0].set_title("Original drawing")
axes[1].imshow(erosion)
axes[1].set_title("Results after corrosion")
axes[2].imshow(dilate)
axes[2].set_title("Results after expansion")
plt.show()

2.2 opening and closing operation

Open operation and close operation are to treat corrosion and expansion in a certain order. But the two are not reversible, that is, opening first and then closing can not get the original image.

1) Open operation

The operation is to corrode first and then expand. Its function is to separate objects and eliminate small areas. Features: eliminate noise and small interference blocks without affecting the original image.

2) Closed operation

Contrary to the open operation, the closed operation expands first and then corrodes. Its function is to eliminate the holes in the "closed" object. Feature: it can fill the closed area.

Example

Implementation of opening and closing convolution using 10 * 10 kernel structure.

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 read image
img1 = cv.imread("./image/image5.png")
img2 = cv.imread("./image/image6.png")
# 2 create core structure
kernel = np.ones((10, 10), np.uint8)
# 3 opening and closing operation of image
cvOpen = cv.morphologyEx(img1,cv.MORPH_OPEN,kernel) # Open operation
cvClose = cv.morphologyEx(img2,cv.MORPH_CLOSE,kernel)# Closed operation
# 4 image display
fig,axes=plt.subplots(nrows=2,ncols=2,figsize=(10,8))
axes[0,0].imshow(img1)
axes[0,0].set_title("Original drawing")
axes[0,1].imshow(cvOpen)
axes[0,1].set_title("Open operation result")
axes[1,0].imshow(img2)
axes[1,0].set_title("Original drawing")
axes[1,1].imshow(cvClose)
axes[1,1].set_title("Closed operation result")
plt.show()

2.3 top hat and black hat

Top hat operation

The difference between the original image and the result of "open operation" is calculated as follows:

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

The top hat algorithm is used to separate patches that are lit up next to each other. When an image has a large background and small objects are regular, the top hat operation can be used for background extraction.

Black hat operation

Is the difference between the result image of "closed operation" and the original image. The mathematical expression is:

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.

Black hat operation is used to separate patches darker than adjacent points.

cv.morphologyEx(img, op, kernel)

Parameters:

img: image to process
op: processing method: if open operation is performed, it is set to CV MORPH_ Open, if closed operation is performed, it is set to CV MORPH_ CLOSE

Kernel: core structure

Example

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 read image
img1 = cv.imread("./image/image5.png")
img2 = cv.imread("./image/image6.png")
# 2 create core structure
kernel = np.ones((10, 10), np.uint8)
# 3 top hat and black hat operation of image
cvOpen = cv.morphologyEx(img1,cv.MORPH_TOPHAT,kernel) # Top hat operation
cvClose = cv.morphologyEx(img2,cv.MORPH_BLACKHAT,kernel)# Black hat operation
# 4 image display
fig,axes=plt.subplots(nrows=2,ncols=2,figsize=(10,8))
axes[0,0].imshow(img1)
axes[0,0].set_title("Original drawing")
axes[0,1].imshow(cvOpen)
axes[0,1].set_title("Top hat calculation result")
axes[1,0].imshow(img2)
axes[1,0].set_title("Original drawing")
axes[1,1].imshow(cvClose)
axes[1,1].set_title("Black hat operation result")
plt.show()

Keywords: OpenCV Computer Vision image processing

Added by Ward on Mon, 24 Jan 2022 21:41:51 +0200