OpenCV expansion and corrosion (callback function)

catalogue

1, Structural element

2, Swell

1. Principle

2. Dilation function

3. Code

4. Effect

3, Corrode

1. Principle

2. Corrosion function (erode)

3. Code

4. Effect

4, Simple operation - callback function and slider

1. Callback function

2. Slider creation

3. Code

4. Effect

reference material

1, Structural element

The getStructuringElement() function returns the structural element of the specified shape and size.

Mat getStructuringElement(int shape, Size esize, Point anchor = Point(-1, -1));

Parameter introduction:

Parameter 1: (convolution kernel shape)

Rectangle: MORPH_RECT;

Cross shape: MORPH_CROSS;

Oval: MORPH_ELLIPSE;

Parameter 2: kernel size (width, height) (must be odd)

Parameter 3: anchor position (the default value Point (- 1, - 1) means that the anchor is at the center Point)

example:

//Convolution kernel (structural element)
	Mat structureElement = getStructuringElement(MORPH_RECT, Size(11, 11) , Point(-1,-1));
	//				Returns the shape of a structural element with a specified shape and size 	    Dimensions (W, H) 	   Anchor point (center point)

2, Swell

1. Principle

The maximum value in the structural element field is taken, so the average value of the overall brightness of the expanded output image will be higher than the original image, the area of the brighter area in the image will become larger, and the size of the darker object will decrease or even disappear. (red is the reference point / anchor point)

2. Dilation function

//Expansion function
dilate(img, dst, structureElement, Point(-1, -1), 1);
//	   Convolution kernel of original graph and new graph (structural element) 	    Anchor point 			  Number of expansion operations (1 by default)

3. Code

//expand
void Dilate()
{
	//Convolution kernel (structural element)
	Mat structureElement = getStructuringElement(MORPH_RECT, Size(11, 11) , Point(-1,-1));
	//						 Returns the shape of a structural element with a specified shape and size 	                  Dimensions (W, H) 	   Anchor point (center point)
	//Expansion function
	dilate(img, dst, structureElement, Point(-1, -1), 1);
	//		 Convolution kernel of original graph and new graph (structural element) 		   Anchor point 			    Number of expansion operations (1 by default)
}

4. Effect

3, Corrode

1. Principle

The corrosion operation is similar to the expansion operation, except that it takes the minimum value in the field specified by the structural element as the output gray value of the position. Because the minimum value in each position field is taken, the average value of the overall brightness of the output image after corrosion will be lower than the original image, the area of the brighter area in the image will become smaller or even disappear, and the size of the darker area will expand.

2. Corrosion function (erode)

//Corrosion function
	erode(img, dst, structureElement, Point(-1, -1), 1);
	//   Convolution kernel of original graph and new graph (structural element) 	   Anchor point 			 Number of corrosion operations (1 by default)

3. Code

//corrosion
void Erode()
{
	//Convolution kernel (structural element)
	Mat structureElement = getStructuringElement(MORPH_RECT, Size(11, 11), Point(-1, -1));
	//						 Returns the shape of a structural element with a specified shape and size 	                  Dimensions (W, H) 	   Anchor point (center point)
	//Corrosion function
	erode(img, dst, structureElement, Point(-1, -1), 1);
	//		 Convolution kernel of original graph and new graph (structural element) 		   Anchor point 			    Number of corrosion operations (1 by default)
}

4. Effect

4, Simple operation - callback function and slider

1. Callback function

Callback function: take function pointer as parameter.

//Callback function expansion
void CallBack_Dilate(int,			void*)
//					Track bar position 	     user data
{
	cont = element_size * 2 + 1;	//Structural element size control
	Dilate();									//expand
	imshow("Dilate: ", dst);				//display
}

//Callback function corrosion
void CallBack_Erode(int,         void*)
//					Track bar position 	 user data
{
	cont = element_size * 2 + 1;	//Structural element size control
	Erode();									//corrosion
	imshow("Erode: ", dst);				//display
}

2. Slider creation

createTrackbar(conststring& trackbarname, conststring& winname, int* value, int count, TrackbarCallback onChange=0,void* userdata=0); 

Parameter interpretation:

1. trackbarname, the name of the track bar, is used to represent the track bar we created.
2. winname, fill in the name of the window, indicating which window this track bar will be attached to, that is, a window name filled in when namedWindow() creates the window. (the window name must be consistent with the displayed window)
3. Value, a pointer to an integer that represents the position of the slider. At the time of creation, the initial position of the slider is the current value of the variable.
4. count, which indicates the maximum position that the slider can reach. The value of the minimum position of the slider is always 0.
5. onChange, first notice that it has a default value of 0. This is a pointer to the callback function, which will call back every time the slider position changes. And the prototype of this function must be void XXXX(int,void *); The first parameter is the position of the track bar, and the second parameter is user data (see the following parameters). If the callback is a NULL pointer, it means that there is no call to the callback function, and only the third parameter value changes.
6. userdata, which also has a default value of 0. This parameter is the data passed to the callback function by the user to process the track bar event. If the value argument of the third parameter used is a global variable, you can completely ignore the userdata parameter.

//Create slider 1 (expansion)
createTrackbar("Structural element size", "Dilate: ",	&element_size, max_size, CallBack_Dilate);
//Create slider 2 (corrosion)
createTrackbar("Structural element size", "Erode: ",                 &element_size, max_size, CallBack_Erode);
//				Slider name window name (must be consistent with the original window) current value 		  Maximum value 	  Callback function (function pointer as parameter)
//Every time the slider position changes, the callback function will call back

3. Code

//Expansion and corrosion (callback function)
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

Mat img, dst;
int element_size = 3;		//Structural element size (2*ele +1)
int max_size = 21;			//Maximum capacity
int cont;							//Control struct size

//Image initialization
void Image_Init()
{
	img = imread("Resource/test4.jpg");
	dst = Mat::zeros(img.size(), img.type());

	if (img.empty())
	{
		printf("Image loading failed");
		exit(0);
	}
}

//Display image
void Show()
{
	imshow("Original drawing", img);
}

//expand
void Dilate()
{
	//Convolution kernel (structural element)
	Mat structureElement = getStructuringElement(MORPH_RECT, Size(cont, cont), Point(-1, -1));
	//						 Returns the shape of a structural element with a specified shape and size 	                  Dimensions (W, H) 	   Anchor point (center point)
	//Expansion function
	dilate(img, dst, structureElement, Point(-1, -1), 1);
	//		 Convolution kernel of original graph and new graph (structural element) 		   Anchor point 			    Number of expansion operations (1 by default)
}

//corrosion
void Erode()
{
	//Convolution kernel (structural element)
	Mat structureElement = getStructuringElement(MORPH_RECT, Size(cont, cont), Point(-1, -1));
	//						 Returns the shape of a structural element with a specified shape and size 	                  Dimensions (W, H) 	   Anchor point (center point)
	//Corrosion function
	erode(img, dst, structureElement, Point(-1, -1), 1);
	//		 Convolution kernel of original graph and new graph (structural element) 		   Anchor point 			    Number of corrosion operations (1 by default)
}

//Callback function expansion
void CallBack_Dilate(int,				void*)
//							  Track bar position 	 user data
{
	cont = element_size * 2 + 1;	//Structural element size control
	Dilate();									//expand
	imshow("Dilate: ", dst);				//display
}

//Callback function corrosion
void CallBack_Erode(int, void*)
//							  Track bar position 	 user data
{
	cont = element_size * 2 + 1;	//Structural element size control
	Erode();									//corrosion
	imshow("Erode: ", dst);				//display
}

int main()
{
	Image_Init();								//Image initialization

	//Dilate(); 									// expand
	//Erode(); 									// corrosion

	//Callback function (easy to control expansion and corrosion)
	CallBack_Dilate(element_size, 0);		//Display the picture first (expansion callback function)
	CallBack_Erode(element_size, 0);		//Display the picture first (corrosion callback function)
	//					  Track bar position user data
	//Create slider 1 (expansion)
	createTrackbar("Structural element size", "Dilate: ",							&element_size, max_size, CallBack_Dilate);
	//Create slider 2 (corrosion)
	createTrackbar("Structural element size", "Erode: ", &element_size, max_size, CallBack_Erode);
	//						Slider name window name (must be consistent with the original window) current value 			  Maximum value 	   Callback function (function pointer as parameter)
	//Every time the slider position changes, the callback function will call back

	Show();										//Display original image

	waitKey(0);

	return 0;
}

4. Effect

reference material

https://blog.csdn.net/weixin_41695564/article/details/79928835

https://www.bilibili.com/video/BV1uy4y1p7BR?from=search&seid=4644291277982613292

Keywords: C C++ OpenCV AI image processing

Added by MikeDXUNL on Mon, 27 Dec 2021 17:43:53 +0200