Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/
/******************************* Mathematical morphology calculation, there are seven common basic operations: corrosion, expansion, open operation, closed operation, hitting, refining, and roughening. They are the basis of all morphology. * ******************************/# Include "CV. H "# include" highgui. H "# include <stdlib. h> # include <stdio. h> iplimage * src =/blog.armyourlife.info/0?iplimage * DST = 0; iplconvkernel * element = 0; // declare a schema element int element_shape = cv_shape_rect; // rectangular shape element int max_iters = 10; int open_close_pos = 0; int erode_dilate_pos = 0; void openclose (int pos) {int n = open_close_pos-max_iters; int an = n> 0? N:-N; element = cvcreatestructuringelementex (an * 2 + 1, an * 2 + 1, an, An, element_shape, 0 ); // create the structure element if (n <0) {cverode (SRC, DST, element, 1); // corrode the image cvdilate (DST, DST, element, 1 ); // expanded image} else {cvdilate (DST, DST, element, 1); // expanded image cverode (SRC, DST, element, 1 ); // corrosive image} cvreleasestructuringelement (& element); cvshowimage ("open/close", DST);} void erodedilate (int pos) {int n = erode_dilate_pos-max_iters; int an = n> 0? N:-N; element = cvcreatestructuringelementex (an * 2 + 1, an * 2 + 1, an, An, element_shape, 0); If (n <0) {cverode (SRC, DST, element, 1) ;}else {cvdilate (SRC, DST, element, 1) ;} cvreleasestructuringelement (& element ); cvshowimage ("erode/dilate", DST);} int main (INT argc, char ** argv) {char * filename = argc = 2? Argv [1] :( char *) "lena.jpg"; if (src =/blog.armyourlife.info/cvloadimage (filename, 1) = 0) Return-1; DST = cvcloneimage (SRC ); cvnamedwindow ("open/close", 1); cvnamedwindow ("erode/dilate", 1); open_close_pos = keys = max_iters; cvcreatetrackbar ("iterations", "open/close ", & open_close_pos, max_iters * 2 + 1, openclose); cvcreatetrackbar ("iterations", "erode/dilate", & erode_dilate_pos, max_iters * 2 + 1, Erodedilate); For (;) {int C; openclose (open_close_pos); erodedilate (erode_dilate_pos); C = cvwaitkey (0); If (C = 27) {break ;} switch (c) {Case 'E': element_shape = cv_shape_ellipse; break; Case 'r': element_shape = cv_shape_rect; break; Case '/R': element_shape = (element_shape + 1) % 3; break; default: Break ;}} cvreleaseimage (& SRC); cvreleaseimage (& DST); cvdestroywindow ("open/close"); cvdestroywindow ("Erode/dilate"); Return 0 ;} /**************************** corrosion and expansion, it looks like a pair of reciprocal operations. In fact, these two operations do not have an inverse relationship. Open and closed operations evolve based on the irreversible nature of corrosion and expansion. The process of first corrosion and then expansion is called an open operation. The closed operation is achieved through the execution of corrosion and expansion in another different order. The closed operation is the process of first expansion and then corrosion, its function is to fill in small holes in the body, connect adjacent objects, and smooth the border, without significant changes but not significantly change the area. ******************************/
Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/