OpenCV-morphological processing (corrosion, expansion, open operation, close operation, top hat, bottom hat, morphological gradient)

Commonly used morphological processing methods include: corrosion, expansion, open operation, closed operation, top hat operation, bottom hat operation, among which corrosion and expansion are the most basic methods, and other methods are produced by the combination of the two.

corrosion

Structural element: Similar to the smoothing operation, the rectangular neighborhood is used in the smoothing operation, and the neighborhood can be a rectangular structure, or an elliptical or cross-shaped structure in the morphological processing. You also need to specify an anchor point.

In the corrosion operation, the minimum value in the structure element is taken as the value of the anchor point. Corrosion operations can be done on grayscale images or binary images. Take the following figure as an example (all take the center point as the anchor point):

Insert picture description here

The minimum values ​​of the neighborhoods in the top three figures are 11, 21, and 21 respectively. These minimum values ​​are output to the anchor point position in the image, and the complete output image can be obtained by moving the structure element and so on in other positions.

As can be seen from the above figure, because the minimum value in the neighborhood of each location is taken, the average value of the overall brightness of the output image after corrosion will be lower than that of the original image, and the area of ​​the brighter area in the image will become smaller or even disappear. The area of ​​the darker area will increase.

Because the area of ​​the brightness area is reduced after the image is corroded, for the white binary image in the foreground after threshold segmentation, the foreground boundary can be obtained by subtracting the original image and the corroded image.

OpenCV functions

Official address of erode function

// 腐蚀
// C++
void cv::erode(InputArray 		src,
			 	OutputArray 	dst,
				InputArray 	    kernel,
				Point 	        anchor = Point(-1,-1),
				int 	        iterations = 1,
				int 	        borderType = BORDER_CONSTANT,
				const Scalar & 	borderValue = morphologyDefaultBorderValue() 
							)
  
// Python
dst=cv.erode(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])

Parameter explanation:

parameter Explanation
src Input matrix, grayscale image or binary image
dst Output matrix
kernel Structure element, can be obtained by getStructuringElement function
anchor The position of the anchor point; the default value (-1, -1) means that the anchor point is at the center of the structure element.
iterations Number of corrosion
borderType Boundary extension type
borderValue Boundary extension

The commonly used borderTypes are BORDER_CONSTANT, BORDER_REPLICATE and BORDER_REFLECT. BORDER_CONSTANT is constant expansion, BORDER_REPLICATE is boundary replication expansion, and BORDER_REFLECT is mirror expansion.

GetStructuringElement function official address

// 获取结构元
// C++
Mat cv::getStructuringElement(int 	shape,
							Size 	ksize,
							Point 	anchor = Point(-1,-1) 
							)		
// Python:
retval	=	cv.getStructuringElement(	shape, ksize[, anchor])
parameter Explanation
shape Structure element shape
ksize Structure element size
anchor Structural element anchor

Structure element shape

  • MORPH_RECT

    Structuring element

  • MORPH_CROSS

    Structure element

  • MORPH_ELLIPSE

    Oval structure element

Python example

import cv2 as cv

if __name__ == '__main__':
    img_src = cv.imread("img2.jpg", 0)

    # 创建矩形结构元
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
    # 腐蚀图像
    img_erode = cv.erode(img_src, kernel)
    # 边界提取
    img_edge = img_src - img_erode

    cv.imwrite("./images/img2_src.jpg", img_src)
    cv.imwrite("./images/img2_erode.jpg", img_erode)
    cv.imwrite("./images/img2_edge.jpg", img_edge)

Corrosion on Binary Graph

Insert picture description here

The image after threshold segmentation will inevitably have a lot of sporadic white noise, which can be removed by corrosion. As can be seen from the above figure, the larger the corroded structure element, the smaller the area of ​​the target object (white area). If the image is repeatedly corroded, the entire target object will be eliminated.

Corrosion to grayscale

Insert picture description here

Corrosion of the grayscale image, as the size of the structure element increases, the area of ​​the darker grayscale area also increases, while the area of ​​the brighter grayscale area decreases, and the effect after processing You can vaguely see the shape of the structural elements, that is, many overlapping rectangles, which look like a mosaic effect. If an ellipse or cross-shaped structure element is used for erosion, a similar ellipse or cross shape will also appear.

Swell

In the expansion operation, the maximum value in the structure element is taken as the value of the anchor point. You can perform expansion operations on grayscale or binary images. After expansion, the average value of the overall brightness of the output image will increase compared to the original image, the area of ​​the brighter area in the image will become larger, and the area of ​​the darker area will decrease.

OpenCV functions

Official address of dilate function

// 膨胀
// C++
void cv::dilate(InputArray 	  src,
				OutputArray   dst,
				InputArray 	  kernel,
				Point 	      anchor = Point(-1,-1),
				int 	      iterations = 1,
				int 	      borderType = BORDER_CONSTANT,
				const Scalar & 	borderValue = morphologyDefaultBorderValue() 
				)		
// Python:
dst	=cv.dilate(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])

The parameters are the same as those of the corrosion operation.

Python example

Next, use the progress bar to adjust the radius of the structure and observe the influence of the size of the structure element on the morphological processing

import cv2 as cv

def change_dilate_r(r):
    # 创建结构元
    kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (2 * r + 1, 2 * r + 1))
    # 腐蚀图像
    img_dilate = cv.dilate(img_src, kernel)
    # 显示膨胀效果
    cv.imshow('dilate', img_dilate)


if __name__ == '__main__':
    img_src = cv.imread("img5.jpg", 0)
    # 显示原图
    cv.imshow("src", img_src)
    # 结构元半径
    r = 1
    max_r = 20
    # 显示膨胀效果的窗口
    cv.namedWindow('dilate', 1)
    # 调节结构元半径
    cv.createTrackbar('r', 'dilate', r, max_r, change_dilate_r)
    change_dilate_r(0)
    cv.waitKey(0)

Insert picture description here

Open operation and close operation

Open operation Corrosion before expansion is called open operation, which has the functions of eliminating small areas with higher brightness, separating objects at delicate points, and smoothing the boundaries of larger objects without changing the area significantly.

The closed operation first expands and then corrodes is called the closed operation. It has the functions of filling small black holes in white objects, connecting adjacent objects, the same structure element, multiple iterations, and smoothing its boundaries without changing its area significantly. .

Open operation and close operation are a combination of expansion and corrosion, which can be done using the functions erode and dilate. OpenCV also provides functions for direct use

OpenCV functions

Official website address of morphologyEx function

// C++
void cv::morphologyEx(InputArray 	src,
					OutputArray 	dst,
					int 	op,
					InputArray 	kernel,
					Point 	anchor = Point(-1,-1),
					int 	iterations = 1,
					int 	borderType = BORDER_CONSTANT,
					const Scalar & 	borderValue = morphologyDefaultBorderValue() 
					)		
// Python:
dst	=cv.morphologyEx(src, op, kernel[,dst[,anchor[,iterations[,borderType[, borderValue]]]]])
parameter Explanation
on Types of morphological operations
kernel Structure element, can be obtained by getStructuringElement function
anchor The position of the anchor point; the default value (-1, -1) means that the anchor point is at the center of the structure element.
iterations Number of iterations
borderType Type of boundary expansion, see Corrosion
borderValue Boundary expansion value, see corrosion

op type of morphological operation

  • MORPH_ERODE 腐蚀 erode
  • MORPH_DILATE dilate
  • MORPH_OPEN open operation (corrosion first and then expansion)
  • MORPH_CLOSE closed operation (expand then corrode)
  • MORPH_GRADIENT Morphological gradient
  • MORPH_TOPHAT top hat calculation
  • MORPH_BLACKHAT bottom hat calculation

Python example

Use the progress bar to adjust the radius of the structure and the number of iterations, and observe the influence of the size of the structure element on the morphological processing

import cv2 as cv

def nothing(*args):
    pass

if __name__ == '__main__':
    img_src = cv.imread("img1.jpg", 0)

    r, i = 1, 1
    max_r, max_i = 20, 20

    cv.namedWindow('morphology', 1)
    cv.createTrackbar('r', 'morphology', r, max_r, nothing)
    cv.createTrackbar('i', 'morphology', i, max_i, nothing)

    while True:
        r = cv.getTrackbarPos('r', 'morphology')
        i = cv.getTrackbarPos('i', 'morphology')
        kernel = cv.getStructuringElement(cv.MORPH_RECT, (2*r+1, 2*r+1))
        # img_open = cv.morphologyEx(img_src, cv.MORPH_OPEN, kernel, iterations=i)
        # cv.imshow('morphology', img_open)
        img_erode = cv.erode(img_src, kernel, iterations=i)
        cv.imshow('morphology', img_erode)

        ch = cv.waitKey(5)
        if ch==27:
            break
        elif ch==115:
            # cv.imwrite(f'./images/img1_{i}.jpg', img_open)
            cv.imwrite(f'./images/img1_{i}.jpg', img_erode)
    cv.destroyAllWindows()

Insert picture description here

The above figure shows the processing effect of the open operation (upstream) and corrosion (downstream) on the binary graph. The 3x3 rectangular structure element is used to perform 1, 5, and 20 iterations in sequence. As the number of iterations increases, the open operation eliminates the small and brighter areas around the white object, and the area of ​​the white object does not change significantly, and the boundary becomes smoother locally. However, the corrosion operation will make the area of ​​the object smaller or even disappear. If the expansion operation is used multiple times, the area of ​​the white object will gradually increase.

The open operation also has a very important function: eliminate the brighter area under the dark background. As shown in the figure below, the purpose is to eliminate the white area on the ball without changing the area of ​​the black ball. The corrosion operation will increase the area of ​​the ball, and the open operation can prevent the area of ​​the ball from increasing.

Insert picture description here

For the closed operation, as shown in the figure below, the purpose is to remove the black area on the dice. Both expansion and closing operations can be done, but expansion will increase the area of ​​the dice, while closing operations will not.

Insert picture description here

Top hat transformation, bottom hat transformation and morphological gradient

Top hat transformation and bottom hat transformation are based on open operation and closed operation respectively

Top hat transformation The original image is subtracted from the result of the open operation. Since the open operation can eliminate the brighter area under the dark background, if you subtract the result of the open operation from the original image, you can get the brighter gray-scale area in the original image, so it is also called white top hat transformation . Can correct uneven illumination.

The bottom hat transforms the original image by subtracting the result of the closing operation. Since the closed operation can delete the darker area under the higher brightness background, if you subtract the result of the closed operation from the original image, you can get the darker grayscale area of ​​the original image, so it is also called the black hat transformation.

The result of morphological gradient expansion minus the result of corrosion, because expansion is to take the maximum value in the neighborhood to increase the area of ​​the high-brightness area, while corrosion is to take the minimum value in the neighborhood to reduce the high-brightness area Area, so the resulting image is the boundary of the object in the image.

Python example

import cv2 as cv

def nothing(*args):
    pass

if __name__ == '__main__':
    img_src = cv.imread("/img5.jpg", 0)

    r, i = 1, 1
    max_r, max_i = 20, 20

    type = 0
    max_type = 2

    cv.namedWindow("morphology", 1)
    cv.createTrackbar('r', 'morphology', r, max_r, nothing)
    cv.createTrackbar('i', 'morphology', i, max_i, nothing)
    cv.createTrackbar('t', 'morphology', type, max_type, nothing)
    m_type = cv.MORPH_TOPHAT
    while True:
        r = cv.getTrackbarPos('r', 'morphology')
        i = cv.getTrackbarPos('i', 'morphology')
        t = cv.getTrackbarPos('t', 'morphology')
        if t==0:
            m_type = cv.MORPH_TOPHAT
        elif t==1:
            m_type = cv.MORPH_BLACKHAT
        else:
            m_type = cv.MORPH_GRADIENT
            
        kernel = cv.getStructuringElement(cv.MORPH_RECT, (2*r+1, 2*r+1))
        img_mor = cv.morphologyEx(img_src, m_type, kernel, iterations=i)

        cv.imshow('morphology', img_mor)
        ch = cv.waitKey(5)
        if ch == 27:
            break
        elif ch == 115:
            cv.imwrite(f'./images/img_{t}.jpg', img_mor)
    cv.destroyAllWindows()

The following figure shows the result of 12 top hat operations using a 3x3 rectangular structure element, 10 bottom hat operations using a 3x3 rectangular structure element, and the result of the morphological gradient operation.

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_38007695/article/details/112710057