Opencv built-in example (8) image corrosion and Expansion

Source: Internet
Author: User

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 );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.