Connectivity zone MARK: Implementation of bwlabel in C ++ (based on opencv)

Source: Internet
Author: User

Similar to the C ++ version Implementation of bwlabel in MATLAB to mark connected areas, based on opencv, as follows:

 

////////

/// Bwlabel. cpp

# Include "CV. H "<br/> # include" highgui. H "<br/> # define no_object 0 <br/> # define min (x, y) (x) <(y ))? (X): (y) <br/> # define ELEM (IMG, R, c) (cv_image_elem (IMG, unsigned char, R, c )) <br/> # define onetwo (L, R, C, col) (L [(r) * (COL) + C]) <br/> int find (INT set [], int X) <br/>{< br/> int r = x; <br/> while (set [R]! = R) <br/> r = set [R]; <br/> return R; <br/>}< br/>/* <br/> labeling scheme <br/> +-+ <br/> | d | c | E | <br/> +-+ <br/> | B | A | <br/> +-+ <br/> | <br/> +-+ <br/> A is the center pixel of a neighborhood. in the 3 versions of <br/> connectedness: <br/> 4: A connects to B and C <br/> 6: A connects to B, C, and D <br/> 8: A connects to B, C, D, and e <br/> */<br/> //////////////////////////// /<br/> // By yysdsyl <br/> // input: IMG -- gray image <br/> // n -- N connectedness <br/> // labels -- label of each pixel, labels [row * Col] <br/> // output: number of connected regions <br/> // Email: yysdsyl@qq.com <br/> int bwlabel (iplimage * IMG, int N, int * labels) <br/> {<br/> If (n! = 4 & n! = 8) <br/> N = 4; <br/> int Nr = IMG-> height; <br/> int NC = IMG-> width; <br/> int Total = nR * NC; <br/> // results <br/> memset (labels, 0, total * sizeof (INT )); <br/> int nobj = 0; // number of objects found in image <br/> // other variables <br/> int * lset = new int [total]; // label table <br/> memset (lset, 0, total * sizeof (INT); <br/> int ntable = 0; <br/> for (INT r = 0; r <NR; r ++) <br/>{< br/> for (I Nt c = 0; C <NC; C ++) <br/>{< br/> If (ELEM (IMG, R, c )) // if A is an object <br/>{< br/> // get the neighboring pixels B, C, D, and E <br/> int B, c, d, E; <br/> If (C = 0) <br/> B = 0; <br/> else <br/> B = find (lset, onetwo (labels, R, C-1, NC); <br/> If (r = 0) <br/> C = 0; <br/> else <br/> C = find (lset, onetwo (labels, R-1, C, NC )); <br/> If (r = 0 | C = 0) <br/> D = 0; <br/> else <br /> D = find (lset, onetwo (labels, R-1, C-1, NC )); <br/> If (r = 0 | C = NC-1) <br/> E = 0; <br/> else <br/> E = find (lset, onetwo (labels, R-1, C + 1, NC )); <br/> If (n = 4) <br/> {<br/> // apply 4 connectedness <br/> If (B & C) <br/> {// B and C are labeled <br/> If (B = C) <br/> onetwo (labels, R, C, NC) = B; <br/> else {<br/> lset [c] = B; <br/> onetwo (labels, R, C, NC) = B; <br/>}< br />}< Br/> else if (B) // B is object but C is not <br/> onetwo (labels, R, C, NC) = B; <br/> else if (c) // C is object but B is not <br/> onetwo (labels, R, C, NC) = C; <br/> else <br/> {// B, c, d Not Object-new object <br/> // label and put into Table <br/> ntable ++; <br/> onetwo (labels, R, C, NC) = lset [ntable] = ntable; <br/>}< br/> else if (n = 6) <br/> {<br/> // apply 6 connected NES S <br/> If (d) // D object, copy label and move on <br/> onetwo (labels, R, C, NC) = D; <br/> else if (B & C) <br/> {// B and C are labeled <br/> If (B = C) <br/> onetwo (labels, R, C, NC) = B; <br/> else <br/>{< br/> int tlabel = min (B, c); <br/> lset [B] = tlabel; <br/> lset [c] = tlabel; <br/> onetwo (labels, R, C, NC) = tlabel; <br/>}< br/> else if (B) // B is object but C is not <br/> onetwo (L Abels, R, C, NC) = B; <br/> else if (c) // C is object but B is not <br/> onetwo (labels, R, c, NC) = C; <br/> else <br/> {// B, c, d Not Object-new object <br/> // label and put into Table <br/> ntable ++; <br/> onetwo (labels, R, C, NC) = lset [ntable] = ntable; <br/>}< br/> else if (n = 8) <br/>{< br/> // apply 8 connectedness <br/> If (B | c | d | E) <br/>{< br/> int tlabel = B; <br/> If (B) <br/> tlabel = B; <br/> else if (c) <br/> tlabel = C; <br/> else if (d) <br/> tlabel = D; <br/> else if (e) <br/> tlabel = E; <br/> onetwo (labels, R, C, NC) = tlabel; <br/> If (B & B! = Tlabel) <br/> lset [B] = tlabel; <br/> If (C & C! = Tlabel) <br/> lset [c] = tlabel; <br/> If (D & D! = Tlabel) <br/> lset [d] = tlabel; <br/> If (E & E! = Tlabel) <br/> lset [e] = tlabel; <br/>}< br/> else <br/> {<br/> // label and put into Table <br/> ntable ++; <br/> onetwo (labels, R, C, NC) = lset [ntable] = ntable; <br/>}< br/> else <br/> {<br/> onetwo (labels, R, C, NC) = no_object; // A is not an object so leave it <br/>}< br/> // specify lidate component table <br/> (int I = 0; I <= ntable; I ++) <br/> lset [I] = find (lset, I ); <br/> // run image through the look-up table <br/> for (INT r = 0; r <NR; r ++) <br/> for (int c = 0; C <NC; C ++) <br/> onetwo (labels, R, C, NC) = lset [onetwo (labels, r, C, NC)]; <br/> // count up the objects in the image <br/> for (INT I = 0; I <= ntable; I ++) <br/> lset [I] = 0; <br/> for (INT r = 0; r <NR; r ++) <br/> for (int c = 0; C <NC; C ++) <br/> lset [onetwo (labels, R, C, NC)] ++; <br/> // number the objects from 1 through n objects <br/> nobj = 0; <br/> lset [0] = 0; <br/> for (INT I = 1; I <= ntable; I ++) <br/> If (lset [I]> 0) <br/> lset [I] = ++ nobj; <br/> // run through the look-up table again <br/> for (INT r = 0; r <NR; r ++) <br/> for (int c = 0; C <NC; C ++) <br/> onetwo (labels, R, C, NC) = lset [onetwo (labels, R, C, NC)]; <br/> // <br/> Delete [] lset; <br/> return nobj; <br/>}< br/>

 

CodeDownload: bwlabel. cpp

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.