OpenCV4 (C++) - Morphology (erosion, dilation)


1. Corrosion (erode)

OpenCV 4 provides the erode() function for image erosion.

void cv::erode(src, dst, kernel, anchor, iterations, borderType, borderValue)

src:输入图像。
dst:输出图像,与输入图像具有相同的大小和类型。
kernel:腐蚀操作的结构元素。可以自己定义,也可以用getStructuringElement()函数生成。
anchor:结构元素的锚点位置,默认为 (-1, -1),表示位于结构元素的中心。
iterations:指定腐蚀操作的迭代次数,默认为 1
borderType:图像边框的处理方式,默认为 BORDER_DEFAULT。
borderValue:默认使用边界不变外推法时的边界值。当边框类型为 BORDER_CONSTANT 时,指定边框的固定值,默认为 0


cv::Mat cv::getStructuringElement(int  shape,
                              Size  ksize,  // 结构元素的尺寸大小
                              Point  anchor = Point(-1,-1)  // 中心点的位置,默认参数为结构元素的几何中心点
                              )
shape:结构元素的种类: 
	MORPH_RECT(简记:0) —— 矩形结构元素,所有元素都为1
	MORPH_CROSS(简记:1)—— 十字结构元素,中间的列和行元素为1
	MORPH_ELLIPSE(简记:2) —— 椭圆结构元素,矩形的椭圆内接元素为1

The code is as follows (example):

	//cv::Mat image = cv::imread("C:/Users/Opencv/temp/wz.png",cv::IMREAD_GRAYSCALE);
	cv::Mat image = cv::imread("C:/Users/Opencv/temp/black.png", cv::IMREAD_GRAYSCALE);
	cv::Mat erodeImage;

	cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
	cv::erode(image, erodeImage, kernel);

Note: Erosion literally means shrinking the outline, but the erode function only targets non-0 pixels in the image. Therefore, if the image is based on 0 pixels as the background, then after the corrosion operation, you will see that the content in the image becomes thinner; if the image is based on 255 pixels as the background, then after the erosion operation, you will see that the content in the image becomes thinner. Thick.

Insert image description here
Insert image description here

2. Dilate

Image expansion and image erosion are opposite processes. OpenCV 4 provides the dilate() function for image expansion. The function prototype is the same as erode.
  Also note that if the image is based on 0 pixels as the background, then after the expansion operation you will see that the content in the image becomes thicker; if the image is with 255 pixels as the background, then after the expansion operation you will see that the content in the image becomes thicker Tinier.

  Summary: Corrosion and expansion in conventional concepts are on a 0 pixel (black) background.

3. Morphological operations

Erosion and dilation are both types of image morphology operations, and they can be combined in many ways. Such as common opening operations and closing operations:

(1) Corrosion can remove small noise areas, but it will also reduce the area of ​​the main image area; therefore, the expansion operation can be performed after the corrosion operation to make up for the area reduction of the larger connected domain caused by corrosion. This operation is called an opening operation.

(2) Expansion can expand the area of ​​each region, fill smaller holes, and connect two adjacent connected domains, but it will also increase the area of ​​noise; therefore, the expansion operation can only be performed, and then the corrosion operation can be performed to reduce the noise caused by the expansion operation. Caused by the expansion of connected domain boundaries and areas. This operation is called a closed operation.

In addition, morphological operations include gradient (describing the boundary of the target), top hat operation (the difference between the original image and the result of the opening operation, used to separate patches that are brighter than their neighbors), and black hat operation (the difference between the original image and the result of the opening operation). The difference between the results of closed operations is often used to separate patches that are darker than adjacent points) and hit-miss transformation (finding structures that are exactly the same as structural elements).


OpenCV 4 provides the morphologyEx() function to cover these morphological operations
void cv::morphologyEx(InputArray  src, 
					OutputArray  dst,
					int  op,  // 形态学操作类型的标志
					InputArray  kernel,  // 结构元素,可以自己生成,也可以用getStructuringElement()函数生成
					anchor = Point(-1,-1),  // 中心点在结构元素中的位置,默认参数为结构元素的几何中心点
					int  iterations = 1,
					int  borderType = BORDER_CONSTANT,
					const Scalar &  borderValue = morphologyDefaultBorderValue() 

其中第三个参数形态学操作类型的标志:
标志参数	       简记	  作用
MORPH_ERODE  	0	图像腐蚀
MORPH_DILATE	1	图像膨胀
MORPH_OPEN	    2	开运算
MORPH_CLOSE	    3	闭运算
MORPH_GRADIENT	4	形态学梯度
MORPH_TOPHAT	5	顶帽运算
MORPH_BLACKHAT	6	黑帽运算
MORPH_HITMISS	7	击中击不中运算

4. Summary

Corrosion and expansion can actually be seen as performing one (multiple) convolution operations on the image. And you need to choose which morphological operation to use based on the actual situation, but generally speaking, the opening operation (corrosion first and then expansion) is more commonly used, because it can remove noise and some irrelevant small areas, and retain the main area. (But it feels like filtering)

Guess you like

Origin blog.csdn.net/qq_43199575/article/details/133852776