Opencv implements connection Domains

Source: Internet
Author: User

In this article, the cvfloodfill method in opencv is used for statistics of image connection domains. However, in the cvfloodfill method, the number of cvconnectedcomp records cannot return the detailed coordinate position information, I found some materials and allocated space for cvseq, but there is no value for the contour content. It is estimated that the opencv2.00 version is not supported. Assume that there are two ways to obtain the coordinate information: one is to rewrite the cvfloodfill function by source code to return the coordinate information, and the other is to count the number of points based on the returned area after cvfloodfill is connected, then, mark the point and repeat the next connected area.
The following is the code implementation of Method 2:

// Connected region structure
Typedef struct myconnectedcomp
{
Double area;
Int value;
Rect;
List <point> listallpoint;
} Myconnectedcomp
/*
* Function Description: Collects the connected regions of images.
* Description of the number of bytes: SRC, indicating the original image; rectsrc, indicating the image region; listdata, indicating the linked list of connected domains; nunioncolor, indicating the connected colors (0, indicating black spots; 1, white Point); nunionmode, indicating the connection type (0, indicating 4 connections; 1, indicating 8 connections)
* Return value: int type. -1, indicating the number of pixels is incorrect; 0, indicating non-binary/Grayscale Images
*/
Int imageunionbyfloodfill (cximage * SRC, rect rectsrc, list <myconnectedcomp> & listdata, int nunioncolor, int nunionmode)
{
Int nret = 1;
// Check quota
If (src = NULL |! (Nunioncolor = 0 | nunioncolor = 1) |! (Nunionmode = 1 | nunionmode = 2) | (rectsrc. left = 0 & rectsrc. right = 0 | rectsrc. top = 0 & rectsrc. bottom = 0 | rectsrc. left> rectsrc. right | rectsrc. top> rectsrc. bottom ))
{
Nret =-1;
Return nret;
}
If (SRC-> getbpp ()> 8)
{
Nret = 0;
Return nret;
}
Listdata. Clear ();
Int ncolor = 255;
If (nunioncolor = 0)
{
Nunioncolor = 255;
}
If (nunioncolor = 1)
{
Nunioncolor = 0;
}
Int nflag = 4;
If (nunionmode = 1)
{
Nflag = 4;
}
If (nunionmode = 2)
{
Nflag = 8;
}
Int ibackcolor = getblackcolor (* SRC );
Long I = 0, j = 0;
Long m = 0, n = 0, newn = 0, newm = 0;
Long nwidth = 0, nheight = 0;

Iplimage * pcvimagetmp = NULL;
If (! Cximage2iplimage (SRC, & pcvimagetmp ))
{
Return 0;
}
// Perform binarization for a gray image
If (SRC-> getbpp ()! = 1)
{
Cvthreshold (PC vimagetmp, pcvimagetmp, 128,255, cv_thresh_binary );
}
Cvsize size = cvgetsize (pcvimagetmp );
Datumnconnectedcomp dcctmp;
Cvconnectedcomp;
Cvpoint seed;
For (j = rectsrc. Top; j <rectsrc. Bottom; j ++)
{
For (I = rectsrc. Left; I <rectsrc. Right; I ++)
{
If (I <0 | I> = size. Width | j <0 | j> = size. Height)
{
Continue;
}
Uchar * P = (uchar *) & (pcvimagetmp-> imagedata + J * pcvimagetmp-> widthstep) [I];
If (* P = nunioncolor | * P = 100 | * P = 101)
{
Continue;
}
// Black spots are processed for connection
Seed = cvpoint (I, j );
Cvflomaxfill (pcvimagetmp, seed, cvscalarall (100), cvscalarall (0), cvscalarall (0), & Comp, nflag, 0 );
Dcctmp. Area = Comp. area;
Dcctmp. rect. Left = Comp. rect. X;
Dcctmp. rect. Top = Comp. rect. Y;
Dcctmp. rect. Right = Comp. rect. x + Comp. rect. width;
Dcctmp. rect. Bottom = Comp. rect. Y + Comp. rect. height;
Dcctmp. value = int (Comp. value. Val [1]);
Dcctmp. listallpoint. Clear ();
Point ptinsert;
For (n = 0; n <= Comp. rect. height; n ++)
{
For (m = 0; m <Comp. rect. width; m ++)
{
Newn = N + Comp. rect. Y;
Newm = m + Comp. rect. X;
If (newn> = size. Height | newm> = size. width)
{
Continue;
}
Uchar * q = (uchar *) & (pcvimagetmp-> imagedata + newn * pcvimagetmp-> widthstep) [newm];
If (* q = 100)
{
Ptinsert. x = newm;
Ptinsert. Y = newn;
Dcctmp. listallpoint. push_back (ptinsert );
* Q = 101; // The flag is changed, indicating that the link has been connected.
}
}
}
Listdata. push_back (dcctmp );
}
}
Cvreleaseimage (& pcvimagetmp );
Return nret;
}

Note: cximage is an excellent image operation class library. It can quickly access, display, and convert various images.

: Http://www.codeproject.com/KB/graphics/cximage.aspx

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.