Obtain the target object Center

Source: Internet
Author: User
Obtain the target object Center




The target object center is different from the connected region center. A connected region may contain multiple target objects. Obtaining the target object center is more complex than obtaining the connected region center, because it is necessary to solve the segmentation problem of the adhesion part.

After distance transformation is performed on a binary graph, the pixel value at the center of the target object has the local maximum property. In other words, the local maximum value in the distance image contains all centers. The local maximum point is defined as: For any non-zero point, if its pixel value is not less than the pixel value of its 8 fields, the point is the local maximum point.

The local maximum point contains the central point and the non-central point. If you can determine the central point and the non-central point, you can find all the centers. In the same connected domain, when the distance between any two local maximum points is smaller than a certain threshold value, a local maximum point with a smaller pixel value is not a central point. The threshold value is determined by a point with a large pixel value multiplied by an adhesion coefficient. The adhesion coefficient is usually a value near 1.

Because the number of local maximum points cannot be determined in advance and the appropriate memory space cannot be allocated in advance, you can use vector to dynamically manage the memory. Another troublesome problem is that all the local maximum points are grouped by connected areas, because the entire processing process is for the same connected area, rather than the entire image. There are many ways to group by connected region. The following method is to number each connected region, calculate the number of the connected region where the maximum local vertex is located, and sort by number, complete the group.


 

# Include "cxcore. H "<br/> # include" CV. H "<br/> # pragma comment (Lib," cxcore. lib ") <br/> # pragma comment (Lib," CV. lib ") <br/> struct centerpoint <br/>{< br/> int nobject; <br/> float fdistance; <br/> cvpoint2d32f ptcenter; <br/> Public: <br/> bool operator <(const centerpoint & Center) const <br/>{< br/> return nobject <center. nobject; <br/>}< br/>}; <br/> typedef STD: vector <centerpoint> vcenter; <br/> typed Ef std: vector <centerpoint>: iterator centeriter; <br/> // object center <br/> // parameter: <br/> // 1. pbinary: input binary image, single channel, bit depth ipl_depth_8u. <Br/> // 2. Pdistance: output distance image, single channel, depth ipl_depth_32f, and pbinary. <Br/> // 3. veccenter: return the object center. You can allocate enough memory in advance to reduce or avoid Memory adjustment. <Br/> // 4. fminradius: Minimum radius. This contour is ignored when the equivalent radius of the contour is smaller than fminradius. <Br/> // 5. fparam: Adhesion Coefficient. When the distance between two local maximum points is smaller than the larger local maximum point multiplied by fparam, a smaller local maximum point is removed. <Br/> void objectcenter (iplimage * pbinary, iplimage * Pdistance, vcenter & veccenter, float fminradius, float fparam) <br/>{< br/> int x, y, i, j, nsize; <br/> float dx, Dy; <br/> float fdistance, fthreshold; <br/> float * pline [3]; <br/> centeriter citer; <br/> pr_center ccenter; <br/> cvseq * pcontour = NULL; <br/> cvmemstorage * pstorage = NULL; <br/> // clear the object center <br/> veccenter. clear (); <br/> // execution condition <br/> If (P Binary & Pdistance) <br/>{< br/> // Distance Transformation <br/> cvdisttransform (pbinary, Pdistance, cv_dist_l2 ); <br/> // local maximum value <br/> for (y = 1; y <Pdistance-> height-1; y ++) <br/> {<br/> pline [0] = (float *) (Pdistance-> imagedata + Pdistance-> widthstep * (Y-1 )); <br/> pline [1] = (float *) (Pdistance-> imagedata + Pdistance-> widthstep * Y); <br/> pline [2] = (float *) (Pdistance-> imagedata + Pdistance-> widthstep * (Y + 1); <br/> for (x = 1; x <Pdistance-> width-1; X ++) <br/>{< br/> If (pline [1] [x]> = fminradius) <br/> {<br/> If (pline [1] [x]> = pline [0] [x-1]) & <br/> (pline [1] [x]> = pline [0] [x]) & <br/> (pline [1] [x]> = pline [0] [x + 1]) & <br/> (pline [1] [x]> = pline [1] [x-1]) & <br/> (pline [1] [x]> = pline [1] [x + 1]) & <br/> (pline [1] [x]> = pline [2] [x-1]) & <br/> (pline [1] [x]> = pline [2] [x]) & <br/> (pline [1] [X]> = pline [2] [x + 1]) <br/>{< br/> ccenter. nobject = 0; <br/> ccenter. ptcenter. X = (float) x; <br/> ccenter. ptcenter. y = (float) y; <br/> ccenter. fdistance = pline [1] [X]; <br/> veccenter. push_back (ccenter ); <br/>}< br/> // search for all outlines <br/> pstorage = cvcreatemstorage (0 ); <br/> cvfindcontours (pbinary, pstorage, & pcontour, sizeof (cvcontour), cv_retr_ccomp, cv_chain_approx_simple); <br />// Fill all outlines <br/> cvdrawcontours (pbinary, pcontour, cv_rgb (255,255,255), cv_rgb (255,255,255), 2, cv_filled, 8, cvpoint (0, 0); <br/> // external contour loop <br/> for (I = 1; pcontour! = NULL; pcontour = pcontour-> h_next, I ++) <br/>{< br/> for (citer = veccenter. Begin (); citer! = Veccenter. end (); citer ++) <br/>{< br/> // calculates the number of the connected region where the local maximum vertex is located. <br/> If (citer-> nobject = 0) <br/>{< br/> If (cvpointpolygontest (pcontour, citer-> ptcenter, 0)> = 0) <br/>{< br/> citer-> nobject = I; <br/>}< br/> cvreleasememstorage (& pstorage); <br/> pstorage = NULL; <br/> // sort by number <br/> sort (veccenter. begin (), veccenter. end (); <br/> // connected area loop <br/> nsize = (INT) veccenter. size (); <br/> For (x = 0; x <nsize;) <br/>{< br/> for (y = x + 1; y <nsize & veccenter [Y]. nobject = veccenter [X]. nobject; y ++); <br/> // process a single connected area <br/> for (I = x; I <Y-1; I ++) <br/> {<br/> for (j = I + 1; j <Y; j ++) <br/>{< br/> If (veccenter [I]. fdistance> = fminradius) & (veccenter [J]. fdistance> = fminradius) <br/>{< br/> // distance between two local maximum points <br/> dx = veccenter [I]. ptcenter. x-veccenter [J]. ptcenter. x; <br/> DY = veccenter [I]. ptcenter. y-veccenter [J]. ptcenter. y; <br/> fdistance = DX * dx + dy * dy; <br/> // distance threshold <br/> fthreshold = (veccenter [I]. fdistance> veccenter [J]. fdistance )? Veccenter [I]. fdistance * fparam: veccenter [J]. fdistance * fparam; <br/> fthreshold * = fthreshold; <br/> // remove unless the center point <br/> If (fdistance <fthreshold) <br/>{< br/> If (veccenter [I]. fdistance <veccenter [J]. fdistance) <br/>{< br/> veccenter [I]. fdistance = 0; <br/>}< br/> else <br/> {<br/> veccenter [J]. fdistance = 0; <br/>}< br/> X = y; <br/>}< br/>}


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.