Opencv learning notes (6)-opencv algorithm processing for morphology

Source: Internet
Author: User

First, you must understand the kernel structure in opencv: iplconvkernel. How to create

Iplconvkernel * cvcreatestructingelementex (INT cols, // line int rows, // column int anchor_x, // coordinate int anchor_y, int shape, // cv_shape_rect of the reference point inside the closed rectangle of the core: the core is a rectangle, cv_shape_cross: Cross, cv_shape_ellipse: elliptical, cv_shape_custom: User-Defined value int * values = NULL );

The release function is as follows:

void cvReleaseStructingElement(IplConvKernel** element);

For binary images, the cverode and cvdilate in opencv can be used for corrosion and expansion,

Void cvdilate (// Expansion
Iplimage * SRC,
Iplimage * DST,
Iplconvkernel * B = NULL,
Int iterations = 1
);

Void cverode (// Corrosion
Iplimage * SRC,
Iplimage * DST,
Iplconvkernel * B = NULL, // B is the core, the same below
Int iterations = 1 // such as name, number of iterations, the same below
);

The test code is as follows:

/******************************* 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 = 0; iplimage * DST = 0; iplconvkernel * element = 0; // declare a structure 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 a structure element/* iplconvkernel * cvcreatestructingelementex (INT cols, // line int rows, // column int anchor_x, // coordinate int anchor_y, int shape, // cv_shape_rect: the core is a rectangle, cv_shape_cross: Cross, cv_shape_ellipse: elliptical, cv_shape_custom: the user-defined value int * values = NULL ); */If (n <0) // open operation {cverode (SRC, DST, element, 1); // corrode the image cvdilate (DST, DST, ELE Ment, 1); // expanded image/* void cvdilate (iplimage * SRC, iplimage * DST, iplconvkernel * B = NULL, // The default value is 3x3 kernel int iterations = 1 // number of iterations,); * //} else // closed operation {cvdilate (DST, DST, element, 1); // expanded image cverode (SRC, DST, element, 1); // corroded 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); // corrosion} else {cvdilate (SRC, DST, element, 1); // expansion} cvreleasestructuringelement (& element ); // release the core cvshowimage ("erode/dilate", DST);} int main (INT argc, char ** argv) {src = cvloadimage ("D: \ opencv \ opencvproject \ opencv notes \ test.jpg "); If (! SRC) {printf ("Open File error"); Return 0 ;}// menu parameter description printf ("parameter: 1. e-CV_SHAPE_ELLIPSE; 2. r-CV_SHAPE_RECT, 3. /R-(element_shape + 1) % 3 "); DST = cvcloneimage (SRC); cvnamedwindow (" open/close ", 1); cvnamedwindow (" erode/dilate ", 1); open_close_pos = hosts = max_iters; cvcreatetrackbar ("iterations", "open/close", & open_close_pos, max_iters * 2 + 1, openclose); cvcreatetrackbar ("iterations ", "erode/dilate", & erode_di Late_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/clos E "); 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. ******************************/

2. However, for Grayscale or color images, cverode and cvdilate cannot be used directly. A more common function is required here, that is, cvmorphologyex;

Cvmorphologyex void cvmorphologyex (const cvarr * SRC, cvarr * DST, cvarr * temp, iplconvkernel * element, int operation, int iterations = 1); SRC input image. DST output image. temp temporary image, element structure element operation form operation type: cv_mop_open-open operation cv_mop_close-closed operation cv_mop_gradient-morphological gradient cv_mop_tophat-"Top Hat" cv_mop_blackhat-"Black Hat" iterations expansion and corrosion times. the cvmorphologyex function completes some advanced morphological transformations based on the Basic expansion and corrosion operations: the open operation DST = open (SRC, element) = dila Te (erode (SRC, element), element) Closed Operation DST = close (SRC, element) = erode (dilate (SRC, element), element) morphological gradient DST = morph_grad (SRC, element) = dilate (SRC, element)-erode (SRC, element) "Top Hat" DST = tophat (SRC, element) = Src-open (SRC, element) "Black Hat" DST = blackhat (SRC, element) = close (SRC, element) -The SRC temporary image temp is required in the in-place mode when the morphological gradient and the "Top Hat" and "Black Hat" operations are performed.

Note: Not recommended (from http://blog.csdn.net/shandianling/article/details/6423640)
By viewing the source code of cvmorphologyex, we can find that during the opening and closing operations, the function still uses the same structural element as the first step during morphological corrosion and expansion in step 2. This method is correct for general symmetric structural elements, but when the structure element is a custom asymmetric structural element, the resulting image will be incorrectly shifted. The correct method is, for example, in the introduction of open and closed operations, when performing the second step of corrosion and expansion operations, the ing (reflection) of structural elements should be used, cvmorphologyex is not recommended in this article.

Test code

# Include "stdafx. H "# include" CV. H "# include" highgui. H "# include" highgui. H "int main (INT argc, char ** argv) {cvnamedwindow (" sourceimage "); cvnamedwindow (" open "); cvnamedwindow (" close "); cvnamedwindow ("gradient"); cvnamedwindow ("tophat"); cvnamedwindow ("blackhat"); iplimage * src = cvloadimage ("test.bmp"); cvshowimage ("sourceimage ", SRC); iplimage * temp = cvcreateimage (cvgetsize (SRC), 8, 3); iplimage * IMG = cvcreateimage (cvgetsize (SRC), 8, 3); cvcopyimage (SRC, temp ); cvcopyimage (SRC, IMG); // cvmorphologyex (SRC, IMG, temp, null, // default 3*3 cv_mop_open, 4); cvshowimage ("open ", IMG); // closed cvmorphologyex (SRC, IMG, temp, null, // default 3*3 cv_mop_close, 4); cvshowimage ("close", IMG ); // morphological gradient cvmorphologyex (SRC, IMG, temp, null, // default 3*3 cv_mop_gradient, 3); cvshowimage ("gradient", IMG ); // cvwaitkey (0); // "gift hat" cvmorphologyex (SRC, IMG, temp, null, // default 3*3 cv_mop_tophat, 3); cvshowimage ("tophat ", IMG); // cvwaitkey (0); // "Black Hat" cvmorphologyex (SRC, IMG, temp, null, // default 3*3 cv_mop_blackhat, 3 ); cvshowimage ("blackhat", IMG); cvwaitkey (0); cvreleaseimage (& temp); cvreleaseimage (& SRC); cvreleaseimage (& IMG); cvdestroyallwindows (); Return 0 ;}

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.