[Learning OpenCV4] Summary of OpenCV morphological operations

The content shared in this article comes from the book "Learning OpenCV 4: Python-based Algorithm Combat". The content of the book is as follows:

1章 OpenCV快速入门;
第2章 图像读写模块imgcodecs;
第3章 核心库模块core;
第4章 图像处理模块imgproc(一);
第5章 图像处理模块imgproc(二);
第6章 可视化模块highgui;
第7章 视频处理模块videoio;
第8章 视频分析模块video;
第9章 照片处理模块photo;
第102D特征模块features2d;
第11章 相机标定与三维重建模块calib3d;
第12章 传统目标检测模块objdetect;
第13章 机器学习模块ml;
第14章 深度神经网络模块dnn

Welcome to the books "Deep Learning Computer Vision Practice" and "Learning OpenCV4: Python-based Algorithm Practice", welcome to join the QQ group: 187042448


Case 36: Corrosion

The erosion operation calculates the local minimum within the kernel coverage. The function erode of the erosion operation provided in OpenCV is defined as follows:

dst = erode(src, kernel, dst=None, anchor=None, iterations=None, borderType=None, borderValue=None)

The parameters are explained as follows:
src, the input image;
kernel, the kernel structure used for the erosion operation;
dst, the output image (return value); anchor
, the aiming point position, the default is the center position of the corresponding area of ​​the kernel;
The number of iterations to apply the erosion operation;
borderType, the border mode, defined by BorderTypes (see Section 3.4.5);
borderValue, the border pixel value when the border mode is BORDER_CONSTANT.
The input image used in this example is shown in Figure 4.9.
insert image description here

The case code for the erosion operation in Figure 4.9
is shown below:

import cv2

#读取图像
src = cv2.imread("cvbook.jpg")
#定义3×3的腐蚀运算矩形核结构
element = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# 腐蚀运算
erode_img = cv2.erode(src, element)
#图像显示
cv2.imshow("erode_img", erode_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

In order to make the effect more obvious, a 3×3 core structure of corrosion operation is selected, and the result of corrosion operation is shown in Figure 4.10.
Figure 4.10
Figure 4.10 The
erosion operation selects the minimum pixel value in the core structure as the pixel value at the aiming point. Therefore, for the text in Figure 4.9, the black text area will become larger after the erosion operation, so the text will become thicker.

Case 37: Inflation

In contrast to erosion, the dilation operation calculates the local maximum within the kernel coverage. The function dilate of the dilation operation provided in OpenCV is defined as follows:

dst = dilate(src, kernel, dst=None, anchor=None, iterations=None, borderType=None, borderValue=None)

The parameters are explained as follows:
src, the input image; kernel
, the kernel structure used for the dilation operation; dst
, the output image (return value); The number of iterations to apply the dilation operation; borderType, the border mode, defined by BorderTypes (see Section 3.4.5); borderValue, the border pixel value when the border mode is BORDER_CONSTANT. The input image used in this case is shown in Figure 4.9, and the case code of the dilation operation is as follows:




import cv2

#读取图像
src = cv2.imread("logo.jpg")
#定义3×3的膨胀运算矩形结构
element = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# 膨胀运算
dilate_img = cv2.dilate(src, element)
#图像显示
cv2.imshow("dilate_img", dilate_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

The result of the dilation operation is shown in Figure 4.11.
insert image description here

Figure 4.11 The
expansion operation selects the maximum pixel value in the core structure as the pixel value at the aiming point. Therefore, for the text in Figure 4.9, the black text area after the expansion operation becomes smaller, so the text will become thinner, and the smaller text below was almost wiped out.
Comparing the two operations, it can be found that the dilation operation will dilate the bright areas, and the erosion operation will dilate the dark areas, so dilation can be used to fill concave surfaces, and erosion can be used to eliminate protrusions.

Case 38: Other Morphological Operations

The function morphologyEx for morphological operations is provided in OpenCV. The function definition is as follows:

dst = morphologyEx(src, op, kernel, dst=None, anchor=None, iterations=None, borderType=None, borderValue=None)

Parameter descriptions are as follows:
src, input image;
op, morphological operation type, defined by MorphTypes (see Section 4.1);
kernel, kernel structure for morphological operation;
dst, output image (return value);
 Anchor, the position of the aiming point, the default is the center of the corresponding area of ​​the kernel;
iterations, the number of iterations of applying morphological operations;
borderType, the border mode, defined by BorderTypes (see Section 3.4.5);
borderValue, when the border mode is Boundary pixel value when BORDER_CONSTANT.
The calculation methods and uses of several morphological operations are shown in Table 4.1.
Table 4.1
insert image description here

The input image used in this case is Figure 4.9. The erosion and dilation operations have been introduced in the previous two sections, and will not be repeated in this section. The case codes of other morphological operations are as follows:

import cv2

#图像读取
src = cv2.imread("cvbook.jpg")
#获取形态学运算结构
element = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))

#开运算
open_img = cv2.morphologyEx(src, cv2.MORPH_OPEN, element)
#如图4.12
cv2.imshow("open_img", open_img)

#闭运算
close_img = cv2.morphologyEx(src, cv2.MORPH_CLOSE, element)
#如图4.13
cv2.imshow("close_img", close_img)

#形态学梯度运算
grad_img = cv2.morphologyEx(src, cv2.MORPH_GRADIENT, element)
#如图4.14
cv2.imshow("grad_img", grad_img)

#顶帽运算
tophat_img = cv2.morphologyEx(src, cv2.MORPH_TOPHAT, element)
#如图4.15
cv2.imshow("tophat_img", tophat_img)

#底帽运算
blackhat_img = cv2.morphologyEx(src, cv2.MORPH_BLACKHAT, element)
#如图4.16
cv2.imshow("blackhat_img", blackhat_img)	

cv2.waitKey(0)
cv2.destroyAllWindows()

The results after execution are shown in Figure 4.12~4.16.
The calculation method of the opening operation is to corrode first and then dilate, and the execution results are shown in Figure 4.12.
insert image description here
Figure 4.12
The calculation method of the closing operation is to dilate first and then corrode, and the execution result is shown in Figure 4.13.
insert image description here
Figure 4.13
The calculation method of the morphological gradient operation is the dilation result minus the corrosion result, and the execution result is shown in Figure 4.14.
insert image description here
Figure 4.14
The calculation method of the top hat operation is the result of subtracting the open operation from the original image, and the execution result is shown in Figure 4.15.
insert image description here
Figure 4.15
The calculation method of the bottom hat operation is the result of subtracting the original image from the closing operation, and the execution result is shown in Figure 4.16.
insert image description here
Figure 4.16

Guess you like

Origin blog.csdn.net/lxiao428/article/details/122933139