Corrosion is usedAlgorithmTo corrode the edge of the image. The function is to remove the "glitch" of the target edge.
Expansion is to use algorithms to expand the image edge. It is used to fill out the target edge or internal pitfalls.
# Include <opencv2/highgui. HPP> # include <opencv2/imgproc. HPP> # include <stdlib. h> # include <stdio. h> using namespace CV; // global variablesmat SRC, erosion_dst, dilation_dst; int erosion_elem = 0; int erosion_size = 0; int dilation_elem = 0; int dilation_size = 0; int const max_elem = 2; int const max_kernel_size = 21;/** function headers */void erosion (INT, void *); void dilation (INT, void *); Int main (INT argc, char ** argv) {// load an imagesrc = imread ("D: \ image \ aa.jpg"); If (! SRC. data) {return-1;} // create windowsnamedwindow ("erosion Demo", cv_window_autosize); namedwindow ("dilation Demo", cv_window_autosize); cvmovewindow ("dilation Demo", SRC. cols, 0); // create erosion trackbarcreatetrackbar ("element: \ n 0: rect \ n 1: Cross \ n 2: ellipse", "erosion Demo", & erosion_elem, max_elem, erosion); createtrackbar ("kernel size: \ n 2n + 1", "erosion Demo", & erosion_size, max_kernel_size, erosion); // create dilation trackbarcreatetrackbar ("element: \ n 0: rect \ n 1: Cross \ n 2: ellipse "," dilation Demo ", & dilation_elem, max_elem, dilation); createtrackbar (" kernel size: \ n 2n + 1 "," dilation Demo ", & dilation_size, max_kernel_size, dilation); // default starterosion (0, 0); dilation (0, 0 ); waitkey (); imwrite ("D: \ image \ erosion.jpg", erosion_dst); imwrite ("D: \ image \ dilation.jpg", dilation_dst); Return 0 ;} void erosion (INT, void *) {int erosion_type; If (erosion_elem = 0) {erosion_type = morph_rect;} else if (Bytes = 1) {erosion_type = morph_cross ;} else if (response = 2) {erosion_type = morph_ellipse;} mat element = getstructuringelement (erosion_type, size (2 * erosion_size + 1, 2 * erosion_size + 1), point (erosion_size, erosion_size); // apply the erosion operationerode (SRC, erosion_dst, element); imshow ("erosion Demo", erosion_dst);} void dilation (INT, void *) {int dilation_type; If (dilation_elem = 0) {dilation_type = morph_rect;} else if (dilation_elem = 1) {dilation_type = morph_cross;} else if (dilation_elem = 2) {dilation_type = morph_ellipse;} mat element = getstructuringelement (dilation_type, size (2 * dilation_size + 1, 2 * dilation_size + 1), point (dilation_size, dilation_size )); // apply the dilation operationdilate (SRC, dilation_dst, element); imshow ("dilation Demo", dilation_dst );}