Basic Learning notes opencv (22): A connection domain processing function in the Learning opencv book

Source: Internet
Author: User

 

  

  Preface

In the process of image processing, we often encounter such a part of the image. The whole part of the image can be seen at a glance, but it is broken due to various internal gaps, in this way, the computer "eye" is considered disconnected. In order to adapt the image to the human eye, these gaps and disconnected ports need to be connected, this requires the connection domain processing technology in computer graphics. This article provides a simple connection domain processing function, of course, this function is from the famous opencv learning opencv tutorial, but its interface is based on the C version of opencv, and so far, c ++-based opencv is already the mainstream, so I changed its interface to C ++, but some of its internal code basically didn't touch it.

Development Environment: opencv2.4.3 + qtcreator2.5.1

 

  Lab Basics

First, let's take a look at the form of the connection domain handler:

  Void connectedcomponents (MAT & mask_process, int poly1_hull0, float perimscale, int number = 0, rect & bounding_box = rect (), point & contour_centers = point (-1,-1 ));

The mask parameter indicates that a connection domain is required to process a binary image.

The polygon hull0 parameter indicates whether the contour edge adopts polygon fitting. If this parameter is set to 1, polygon fitting is used. Otherwise, convex hull fitting is used.

The perimscale parameter is used to remove small outlines, which indicate that their perimeter is smaller than (Mask Length + width)/perimscale. Of course, you can determine the area in the internal code.

The num parameter indicates the maximum number of outlines to be processed (if the input mask has multiple outlines). The processing here is to calculate the external rectangle and center point of these outlines. The default value is 0, indicating that the function does not need to process these external Rectangles and centers.

The BBS parameter indicates the external rectangle of the corresponding contour after processing. The default value is rect (), indicating that these external rectangles are not returned.

The centers parameter indicates the coordinates of the centers of the corresponding contour after processing. The default value is point (-1,-1), indicating that the centers are not returned.

 

  C/C ++ knowledge point summary:

If some functions require default values, they can be specified directly during function definition. this parameter is not necessarily a specific value or a null value. In addition, when implementing a function, pay attention to the particularity of the default value.

 

  Lab results

Grayscale image of the original image to be processed:

  

 

The corresponding mask image:

  

 

After processing the image using a polynomial fitting connected domain:

  

 

The following figure shows the Connected Domain processed by using a convex hull set:

  

 

  Lab code and comments (Engineering Code is included in the appendix ):

  Main. cpp:

  

# Include <iostream> # include <opencv. HPP> using namespace CV; using namespace STD; // just some convienience macros # define 1_cv_rgb (0xff, 0xff, 0xff) # define cv_cvx_black cv_rgb (0x00,0x00,0x00) void connectedcomponents (MAT & mask_process, int poly1_hull0, float perimscale, int number = 0, rect & bounding_box = rect (), point & contour_centers = point (-1,-1 )) {/* the following four sentences of code are used to be compatible with the original function interface, that is, the C style is used internally, but the interface is C ++ */Iplimage * mask = & mask_process.operator iplimage (); int * num = & number; cvrect * BBS = & bounding_box.operator cvrect (); cvpoint * centers = & contour_centers.operator cvpoint (); static cvmemstorage * mem_storage = NULL; static cvseq * contours = NULL; // clean up raw mask // smooth Contour, remove the details and disconnect the cvmorphologyex (mask, mask, null, null, cv_mop_open, 1). // open the input mask. cvclose_itr indicates the number of open operations, output as a mask image // closed operation Calculation function: Smooth contour, connection gap cvmorphologyex (mask, mask, null, null, cv_mop_close, 1); // The number of times the input mask is closed, and cvclose_itr is closed, output as a mask image // find your s around only bigger regions if (mem_storage = NULL) mem_storage = cvcreatememstorage (0); else cvclearmemstorage (mem_storage ); // cv_retr_external = 0 is defined in types_c.h. cv_chain_approx_simple = 2 is also the cv1_1_1_1_= cvstartfind1_s (mask, mem_storage, sizeof (C Vcontour), cv_retr_external, cv_chain_approx_simple); cvseq * C; int numcont = 0; // This while internally only replaces large contour Curves while (C = cvfindnextcontour (outline ))! = NULL) {double Len = cv1_perimeter (c); double Q = (mask-> height + mask-> width)/perimscale; // calculate perimeter Len threshold if (LEN <q) // get rid of Blob if it's perimeter is too small {cvsubstitutecontour (hour, null ); // replace the original profile with null} else // smooth it's edges if it's large enough {cvseq * c_new; If (poly1_hull0) // Polygonal Approximation of the segmentation c_new = cvapproxp Oly (C, sizeof (cvcontour), mem_storage, bytes,); else // convex hull of the segmentation c_new = cvconvexhull2 (C, mem_storage, cv_clockwise, 1 ); cvsubstitutecontour (hour, c_new); // replace numcont ++ with convex hull or polynomial fitting curve with numcont ++ ;}} contours = cvendfindcontours (& hour ); // end contour search operation // paint the found regions back into the image cvzero (mask); iplimage * masktemp; // calc center of mass and or bound Ing rectangles if (* num! = 0) {int n = * num, numfilled = 0, I = 0; cvmoments moments; double m00, M01, M10; masktemp = cvcloneimage (mask ); for (I = 0, c = s; C! = NULL; C = C-> h_next, I ++) // h_next is the next Contour in the contour sequence {if (I <n) // only process up to * num of them {// cv_cvx_white indicates cvdrawcontours (masktemp, C, cv_cvx_white, cv_cvx_white,-1, cv_filled, 8) in this program ); // find the center of each contour if (centers! = & Cvpoint (-1,-1) {cvmoments (masktemp, & moments, 1 ); // calculate the moment m00 of the mask image up to Level 3 = cvgetspatialmoment (& moments, 0, 0 ); // extract x 0 times and Y 0 moment M10 = cvgetspatialmoment (& moments, 1, 0 ); // extract the first time of X and the 0th moment of Y. M01 = cvgetspatialmoment (& moments ); // extract 0 times of x and 1 moment of Y. Centers [I]. X = (INT) (M10/m00); // obtain the center coordinate of the contour using the moment result. Centers [I]. y = (INT) (M01/m00);} // bounding rectangles around blobs if (BBS! = & Cvrect () {BBS [I] = cvboundingrect (c); // calculate the external rectangle of contour c} cvzero (masktemp); numfilled ++ ;} // draw filled contours into mask cvdrawcontours (mask, C, cv_cvx_white, cv_cvx_white,-1, cv_filled, 8 ); // draw to central mask} // end looping over contours * num = numfilled; cvreleaseimage (& masktemp );} // else just draw processed into s into the mask else {for (C = contours; C! = NULL; C = C-> h_next) {cvdrawcontours (mask, C, cv_cvx_white, cv_cvx_black,-1, cv_filled, 8) ;}} int main () {mat SRC, mask; src = imread ("test.png", 0); // read imshow ("src", Src) as a grayscale image; mask = SRC> 0; // convert to a binary image imshow ("Mask", mask); connectedcomponents (mask, 1, 8.0, 1, rect (), point (-1,-1 )); // use polygon fitting to handle imshow ("out1", mask); connectedcomponents (mask, 0, 8.0, 1, rect (), point (-1,-1 )); // C uses convex packets for processing imshow ("out2", mask); waitkey (0); Return 0 ;}

 

  Experiment summary:Through the connection domain processing, it can achieve a certain effect.

 

  References:

  Bradski, G. and A. kaehler (2008). Learning opencv: computer vision with the opencv library, O 'Reilly media.

 

  Appendix:Lab project code.

 

 

 

 

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.