This article mainly introduces two aspects: morphological operation and tool bar.
First, perform morphological operations. Here we will only introduce four simple ones: corrosion, expansion, opening and closing. The most basic morphological operations are corrosion and expansion. Other operations can be derived from corrosion and expansion.
Introducing them from the perspective of set theory is very troublesome. Here is another way of thinking: Let's make a certain assumption: For an image, the foreground (the part we are interested in) is white, and the background (the part we are not interested in) it is black. Then, let us look at it for a moment: the corrosion operation will make the prospects smaller, and the expansion will make the prospects larger. This is mainly caused by the definition of the two operations when the structure element (the Basic Template used for image processing) Acts on the edge of the image. During the corrosion operation, only when the entire structural element is within the image edge, the anchor (the point in which the structural element is aligned with each pixel in the image, usually taken as the geometric center of the structural element) the target pixel is retained and determined as the foreground. Otherwise, the target pixel is determined as the background. The expansion operation is that as long as the structure element and the image have an intersection, the pixels at the anchor point will be retained and the image will be regarded as the foreground. Corrosion can be used to eliminate the prospect of some small false detection, while expansion can fill some small holes.
Note that the corrosion effect of a 3*3 Template is the same as that of a 7*7 Template. The expansion result is similar.
With the basic definitions of these two operations, we can define the open and closed operations: the open operation is to expand after corrosion, and the closed operation is to expand and then corrode. Closed operations allow small holes to be filled, and the adjacent targets are connected together (small holes or gaps that cannot be accommodated by any structure element will be filled ). The open operation removes some small spots.
It makes no sense to use open operations for an image multiple times and close operations. This is different from corrosion and expansion.
In opencv, erode and dilate are used for corrosion and expansion. To implement open or closed operations, we can certainly do this through methods such as expansion after corrosion, but opencv uses the morphologyex function to implement different morphological operations by changing the parameters in the function. The following code briefly describes these four operations:
# Include <opencv2/CORE/core. HPP> # include <opencv2/highgui. HPP> # include <opencv2/imgproc. HPP> using namespace CV; int main (void) {mat image = imread ("D:/picture/images/binary.bmp"); If (! Image. data) Return-1; imshow ("source image", image); // corrosion operation mat eroded; // by default, the structure element is 3*3 erode (image, eroded, MAT (); erode (eroded, eroded, MAT (); erode (eroded, eroded, MAT (); imshow ("corrosion result", eroded ); // use the custom structure meta mat element (7, 7, cv_8u, scalar (1); erode (image, eroded, element ); imshow ("7*7 structure meta-corrosion results", eroded); // use the default structure meta-repetition operation to get the same results as erode (image, eroded, MAT (), point (-1,-1), 3); imshow ("Repeat 3 times with default schema elements", eroded); // expansion operation mat dilated; dilate (image, dilated, MAT (); imshow ("Expansion result", dilated); // use the closed operation mat element5 (5, 5, cv_8u, scalar (1); MAT closed; morphologyex (image, closed, CV: morph_close, element5); imshow ("5*5 structure meta close operation", closed); // The closed operation mat result is obtained by first expanding and then corrosion; dilate (image, result, element5); erode (result, result, element5); imshow ("first expansion, then corrosion equals closed operation", result ); // use the open operation mat opened; morphologyex (image, opened, CV: morph_open, element5); imshow ("5*5 structured element open operation", opened ); // close the operation before enabling morphologyex (image, result, CV: morph_close, element5); morphologyex (result, result, CV: morph_open, element5 ); imshow ("first close operation, on open operation", result); waitkey (0); Return 0 ;}
The following describes how to use the toolbar.
In opencv, use the createtrackbar function to create a toolbar. The first parameter is the name of the toolbar, and the second parameter is the window in which the toolbar is to be placed. (sorry, I used to think that namedwindow contains a lot of words, just fill in the window name directly in imshow. Why do I need namedwindow? The third parameter is the value changed by the toolbar (the initial position is also set by default), and the fourth parameter is the maximum value of the changed value. The fifth parameter is the function to which the value acts. The sixth value is the data that the user passes to the callback function. However, because I use global variables, the last parameter is not used.
Let's look at the program:
# Include <opencv2/CORE/core. HPP> # include <opencv2/highgui. HPP> # include <opencv2/imgproc. HPP> # include <iostream> using namespace CV; using namespace STD; // defines global variables: mat image; MAT eroded; MAT dilated; MAT opened; MAT closed; // morphological operation: corrosion, expansion, opening, and closing. // create a control for each morphological operation. // parameters required by the control: // parameters to be determined for each morphological operation: // type of the structure element: int erosion_element = 0; int dilation_element = 0; int open_element = 0; int close_element = 0; // Structure Element Int erosion_size = 0; int dilation_size = 0; int open_size = 0; int close_size = 0; // the maximum value of the four types of morphological operations: one of the three types: Square, cross, and elliptical int const max_elements = 2; // the maximum value of the structure element (four types share one) int const max_kernel_size = 5; // The callback function void erosion (INT, void *) of the toolbar; void dilation (INT, void *); void open (INT, void *); void close (INT, void, void *); int main () {cout <"structure element type: 0-rectangle, 1-cross, 2-elliptical" <Endl; cout <"structure element size: 2 * n + 1" <Endl; image = imread ("D:/picture/I Mages/binary.bmp "); If (! Image. data) Return-1; imshow ("source image", image); eroded. create (image. rows, image. cols, image. type (); dilated. create (image. rows, image. cols, image. type (); namedwindow ("corrosion"); namedwindow ("expansion"); namedwindow ("open operation"); namedwindow ("Closed Operation "); // create a toolbar // corrosion operation: createtrackbar ("Core Function Type", "corrosion", & erosion_element, max_elements, erosion); createtrackbar ("core function size ", "corrosion", & erosion_size, max_kernel_size, erosion); // call the corrosion function erosion (); // createtrackbar ("structure element type", "expansion ", & amp; dilation_element, max_elements, dilation); createtrackbar ("", "expansion", & amp; dilation_size, max_kernel_size, dilation); dilation ); // open createtrackbar ("structure element type", "open operation", & open_element, max_elements, open); createtrackbar ("core function size", "open operation ", & open_size, max_kernel_size, open); open (); // The createtrackbar ("structure element type", "closed operation", & close_element, max_elements, close ); createtrackbar ("core function size", "closed operation", & close_size, max_kernel_size, close); close (); waitkey (0); Return 0 ;}// callback function: // corrosion function: void erosion (INT, void *) {int erosion_type; If (erosion_element = 0) {erosion_type = morph_rect;} is not required ;} else if (erosion_element = 1) {erosion_type = morph_cross;} else if (erosion_element = 2) {erosion_type = morph_ellipse;} // obtain the corrosion element. The parameter is: type, size, and anchpoint mat element = getstructuringelement (erosion_type, size (2 * erosion_size + 1, 2 * erosion_size + 1), point (-1,-1 )); // perform corrosion calculation: parameters: original image, result, and corrosion element erode (image, eroded, element); // display the corrosion result imshow ("corrosion", eroded );} // expansion callback function void dilation (INT, void *) {int dilation_type; If (dilation_element = 0) {dilation_type = morph_rect;} else if (dilation_element = 1) {dilation_type = morph_cross;} else if (dilation_element = 2) {dilation_type = morph_ellipse;} // obtain the corrosion element. The parameter is: type, size, and anchpoint mat element = getstructuringelement (dilation_type, size (2 * dilation_size + 1, 2 * dilation_size + 1), point (-1,-1 )); // conduct corrosion calculation: parameter: original image, result, and corrosion element (image, dilated, element); // display the corrosion result imshow ("expansion", dilated );} void open (INT, void *) {int open_type; If (open_element = 0) {open_type = morph_rect;} else if (open_element = 1) {open_type = morph_cross ;} else if (open_element = 2) {open_type = morph_ellipse;} // obtain the corrosion element. The parameter is type, size, and anchor mat element = getstructuringelement (open_type, size (2 * open_size + 1, 2 * open_size + 1), point (-1,-1); // perform the morphologyex (image, opened, CV: morph_open, element); imshow ("open operation", opened);} // closed operation void close (INT, void *) {int close_type; If (close_element = 0) {close_type = morph_rect;} else if (close_element = 1) {close_type = morph_cross;} else if (close_element = 2) {close_type = morph_ellipse;} // obtain the corrosion element: parameters: type, size, and anchpoint mat element = getstructuringelement (close_type, size (2 * close_size + 1, 2 * close_size + 1), point (-1,-1 )); // perform the on Operation morphologyex (image, closed, CV: morph_close, element); imshow ("close operation", closed );}
To change the structure element type and size of morphological operations, I have defined many global variables.
If you want to know more about morphological operations, you can refer to the classic digital image processing file of Gonzalez!