5. Advanced operations of opencv-python image processing (2) - Morphological operations

Learning objectives of this chapter:

Understanding image neighborhoods, connectivity

Understand different morphological operations: corrosion, expansion, opening and closing operations, top hat and black hat, etc., and the relationship between different operations

1. Connectivity

1. Connected

In an image, the smallest unit is a pixel, and each pixel is surrounded by 8 adjacent pixels. There are three common adjacency relationships: 4-adjacency, 8-adjacency and D-adjacency. As shown in the figure:
Insert image description here
4 neighbors: the 4 neighbors of pixel p(x,y) are: (x+1,y); (x-1,y); (x,y+1); (x,y -1), using the 4-adjacency D adjacency representing the pixel p: The D neighborhood of the pixel p(x, y) is: the point on the diagonal (x+1, y+1; (x+1, y-1 N4(p))
; (x-1, y+1); (x-1, y-1), used to ND(p)represent the D domain of pixel p.
8-adjacency: the 8-neighborhood of pixel p(x,y) is: the point of the 4-neighborhood The point in the +D neighborhood is N8(P)represented by the 8-neighborhood of the pixel p

Connectivity is an important concept for describing regions and boundaries. The two necessary conditions for the connection of two pixels are:
1. Whether the positions of the two pixels are adjacent
; 2. Whether the gray value of the two pixels satisfies a specific similarity criterion (or Whether they are equal)
According to the definition of connectivity, there are three types: 4 Unicom, 8 Unicom, and m Unicom.
4-connected: For pixels p and q with value V, if q is in the setN4(p), the two pixels are said to be 4-connected.
8-connected, for pixels p and q with value V, if q is in the setN8(P), these two pixels are said to be 8-connected.
Insert image description here
For pixels p and q with value V, if:
1. q is in the setN4(p), or
2. q is in the setND(p), andN4(p)withN4(q)is empty (pixels without V value)
, then these two pixels are said to be m-connected , that is, a mixture of 4-connected and D-connected.
Insert image description here
I don’t know what the specific use of connectivity is yet. I’ll look at it later. If it’s very important, I’ll go over the concept again.

2. Morphological operations

Morphological transformations are simple operations based on the shape of an image. It is usually performed on binary images. Erosion and dilation are two basic morphological operators. Then its variations include opening operation, closing operation, top hat and black hat, etc.

1. Corrosion and expansion

Erosion and expansion are the most basic morphological operations, and both corrosion and expansion are targeted at the white part (highlighted part).
Dilation is to expand the highlight area in the image, and the effect image has a larger highlight area than the original image;
corrosion is to encroach on the highlight area in the original image, and the effect image has a smaller highlight area than the original image.
Dilation is the operation of finding a local maximum.
Corrosion is the operation of finding a local minimum.

(1) Corrosion

The specific operation is: use a structural element to scan each pixel in the image, and perform an "AND" operation with each pixel in the structural element and the pixel it covers. If both are 1, the pixel is 1, otherwise it is 0, as follows As shown in the figure, structure A is corroded by structure B.
Insert image description here
The function of corrosion is to eliminate object boundary points, make the target smaller, and can eliminate noise points smaller than structural elements.

1)API
   cv.erode(img,kernel,iterations)

Parameters:
img: image to be processed
kernel: kernel structure
iterations: number of erosions, the default is 1

(2) Expansion

The specific operation is: use a structural element to scan each pixel of the image, and perform an "AND" operation with each pixel in the structural element and the pixel it covers. If both are 0, the pixel is 0, otherwise it is 1. As shown in the figure below, after structure A is expanded by structure B,
Insert image description here
the function of expansion is to merge all background points in contact with the object into the object, making the target larger and filling the holes in the target.

1)API
   cv.dilate(img,kernel,iterations)

Parameters:
img: image to be processed
kernel: kernel structure
iterations: number of erosions, the default is 1

(3) Code examples

We use a 5*5 convolution kernel to implement erosion and expansion operations:

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 读取图像
img = cv.imread("./image/image3.png")
# 2 创建核结构
kernel = np.ones((5, 5), np.uint8)

# 3 图像腐蚀和膨胀
erosion = cv.erode(img, kernel) # 腐蚀
dilate = cv.dilate(img,kernel) # 膨胀

# 4 图像展示
fig,axes=plt.subplots(nrows=1,ncols=3,figsize=(10,8),dpi=100)
axes[0].imshow(img)
axes[0].set_title("原图")
axes[1].imshow(erosion)
axes[1].set_title("腐蚀后结果")
axes[2].imshow(dilate)
axes[2].set_title("膨胀后结果")
plt.show()

2. Opening and closing operations

Opening operations and closing operations process corrosion and expansion in a certain order. But the two are not reversible, that is, opening first and then closing cannot get the original image.

(1) Open operation

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

(2) Closed operation

The closing operation is the opposite of the opening operation. It expands first and then corrodes. Its function is to eliminate holes in the "closed" object.
Features: it can fill the closed area.
Insert image description here

(3)API

cv.morphologyEx(img, op, kernel)

Parameters:
img: image to be processed
op: processing method: if the opening operation is performed, set to cv.MORPH_OPEN, if the closing operation is performed, set to cv.MORPH_CLOSE
Kernel: Kernel structure

(4) Example

Implementation of opening and closing operations using 10*10 core structure

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 读取图像
img1 = cv.imread("./image/image5.png")
img2 = cv.imread("./image/image6.png")
# 2 创建核结构
kernel = np.ones((10, 10), np.uint8)
# 3 图像的开闭运算
cvOpen = cv.morphologyEx(img1,cv.MORPH_OPEN,kernel) # 开运算
cvClose = cv.morphologyEx(img2,cv.MORPH_CLOSE,kernel)# 闭运算
# 4 图像展示
fig,axes=plt.subplots(nrows=2,ncols=2,figsize=(10,8))
axes[0,0].imshow(img1)
axes[0,0].set_title("原图")
axes[0,1].imshow(cvOpen)
axes[0,1].set_title("开运算结果")
axes[1,0].imshow(img2)
axes[1,0].set_title("原图")
axes[1,1].imshow(cvClose)
axes[1,1].set_title("闭运算结果")
plt.show()

The effect diagram obtained by the code operation is as follows:
Insert image description here

3. Top hat and black hat

(1) Top hat operation

The difference between the original image and the structure map of the "opening operation" is calculated by the following formula:
Insert image description here
Because the result of the opening operation is to enlarge cracks or local low-brightness areas, therefore, the image after the opening operation is subtracted from the original image, The resulting image highlights areas that are brighter than those around the outline of the original image, and this operation is related to the selected kernel size.
Usage scenarios of top hat
☆ Top hat operation is used to separate patches that are a little brighter than their neighbors. When an image has a large background and small items are relatively regular, the top hat operation can be used for background extraction.

(2) Black hat operation

It is the difference between the result image of "closed operation" and the original image. The mathematical expression is:
Insert image description hereThe effect image after the black hat operation highlights the darker areas than the areas around the original image outline, and this operation is related to the size of the selected kernel.
The black hat operation is used to separate patches that are darker than neighboring spots.

(3)API

cv.morphologyEx(img, op, kernel)

Parameters:
img: image to be processed
op: processing method:
Insert image description hereKernel: kernel structure

(4) Code examples

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 读取图像
img1 = cv.imread("./image/image5.png")
img2 = cv.imread("./image/image6.png")
# 2 创建核结构
kernel = np.ones((10, 10), np.uint8)
# 3 图像的礼帽和黑帽运算
cvOpen = cv.morphologyEx(img1,cv.MORPH_TOPHAT,kernel) # 礼帽运算
cvClose = cv.morphologyEx(img2,cv.MORPH_BLACKHAT,kernel)# 黑帽运算
# 4 图像显示
fig,axes=plt.subplots(nrows=2,ncols=2,figsize=(10,8))
axes[0,0].imshow(img1)
axes[0,0].set_title("原图")
axes[0,1].imshow(cvOpen)
axes[0,1].set_title("礼帽运算结果")
axes[1,0].imshow(img2)
axes[1,0].set_title("原图")
axes[1,1].imshow(cvClose)
axes[1,1].set_title("黑帽运算结果")
plt.show()

Obtained example pictures:
Insert image description here

Summarize

Review of learning content in this chapter:

1. Connectivity

2. Morphological operations

(1) Corrosion and expansion

(2) Opening and closing operations

(3) Top hat and black hat

Guess you like

Origin blog.csdn.net/weixin_44463519/article/details/125920172