An algorithm for finding connected regions of binary images

Source: Internet
Author: User

The binary processing of an image often contains multiple areas, which need to be extracted separately by markers. The simple and effective way to mark each area of a segmented image is to check the connectivity of each pixel to its neighboring pixels.

in a binary image, the value of the background area pixel is 0 , the pixel value of the target area is 1 . Suppose that a picture is scanned from left to right, from top to bottom, to mark the current pixel being scanned, it needs to be checked for connectivity to several neighboring pixels that were scanned before it.

consider 4 connected situations. The image is scanned per pixel.

if the current pixel value is 0 , move to the next scan location.

if the current pixel value is 1 , check the two neighboring pixels on the left and top of it (these two pixels must be scanned before the current pixel). The combination of these two pixel values and tags has four scenarios to consider.

    1. They have a pixel value of 0. A new token (representing the beginning of a new connected domain) is given to the pixel at this time.

    2. They have a pixel value of 1 in between. The marker for the current pixel = 1 of the pixel value.

    3. They have a pixel value of 1 and the tag is the same. The marker for the current pixel is now = the tag.

    4. They have a pixel value of 1 and are marked differently. Assigns the smaller value to the current pixel. It then goes back from the other side to the start pixel of the region. Perform each of the four judgment steps in each retrospective.

This ensures that all connected fields are marked. You can then complete the tag by assigning different colors to different markers or by adding them to the border.

1 /// <summary>2 ///The backtracking method marks the connected domain3 /// </summary>4 /// <param name= "x" >the horizontal axis of the point</param>5 /// <param name= "y" >the ordinate of the point</param>6 /// <param name= "ismarked" >has been marked to record a backtracking route. The default value is False, and you should specify true if the point has already been marked. </param>7         Private voidConnect (intXintYBOOLismarked =false)8         {9             if(x = =0&& y = =0)//mat[0, 0]Ten             { One                 if(f (x, y) = =1) mat[x, y] = mark;//New Area A             } -  -             Else if(X! =0&& y = =0)//First Row the             { -                 if(f (x, y) = =1) -                 { -                     if(Mat[x-1, y]! =0) +                     { -Mat[x, Y] = mat[x-1, y];// left One +Connect (X-1Ytrue); A                     } at                     Else -                     { -                         if(ismarked = =false) -Mat[x, y] = ++mark;//New Area -                     } -                 } in             } -  to             Else if(x = =0&& Y! =0)//First Column +             { -                 if(f (x, y) = =1) the                 { *                     if(Mat[x, Y-1] !=0) $                     {Panax NotoginsengMat[x, Y] = mat[x, y-1];// up one -Connect (x, Y-1,true); the                     } +                     Else A                     { the                         if(ismarked = =false) +Mat[x, Y] = + +Mark; -                     } $                 } $             } -  -             Else if(X! =0&& Y! =0)//Other pixel the             { -                 if(f (x, y) = =1)Wuyi                 { the                     if(Mat[x, Y-1] ==0&& Mat[x-1, y] = =0)//New Area -                     { Wu                         if(ismarked = =false) -Mat[x, Y] = + +Mark; About                     } $                     Else if(Mat[x, Y-1] ==0&& Mat[x-1, y]! =0) -                     { -                         if(ismarked = =false) -Mat[x, Y] = mat[x-1, y]; A                         Else +                         { the                             if(Mat[x-1, Y] >mat[x, y]) -Mat[x-1, Y] =mat[x, y]; $Connect (X-1Ytrue);//continue backtracking in the X direction the                         } the                     } the                     Else if(Mat[x, Y-1] !=0&& Mat[x-1, y] = =0) the                     { -                         if(ismarked = =false) inMat[x, Y] = mat[x, y-1]; the                         Else the                         { About                             if(Mat[x, Y-1] >mat[x, y]) theMat[x, Y-1] =mat[x, y]; theConnect (x, Y-1,true);//continue backtracking in the Y direction the                         } +                     } -                     Else if(Mat[x, Y-1] !=0&& Mat[x-1, y]! =0&& mat[x, Y-1] = = Mat[x-1, y]) the                     {Bayi                         if(ismarked = =false) theMat[x, Y] = mat[x, y-1]; the                         Else -                         { -                             if(Mat[x, Y-1] >mat[x, y]) the                             { theMat[x, Y-1] = mat[x-1, Y] =mat[x, y]; theConnect (X-1Ytrue);//When there are marked pixels on both the top and left side, both sides backtrack simultaneously theConnect (x, Y-1,true); -                             } the                         } the  the                     }94                     Else if(Mat[x, Y-1] !=0&& Mat[x-1, y]! =0&& mat[x, Y-1]! = mat[x-1, y]) the                     { theMat[x, Y] = math.min (Mat[x-1, y], mat[x, y-1]); theMat[x-1, y] = mat[x, y-1] = mat[x, y];//Direct elimination of equivalence classes98Connect (X-1Ytrue); AboutConnect (x, Y-1,true); -                     }101                 }102}

The following results are performed:

The image after binary value

Images marked with connected fields

Of course, one of the problems with this approach is that execution is inefficient, and a larger picture takes a long time to complete the tagging step. But the accuracy rate is still relatively high.

Reference documents:

[1] Zhang Yujin. image segmentation. Science Press, 2001, pp.63

[2] R.gonzalez. Digital Image processing . Electronic industry Press, 2014, pp.38-40

An algorithm for finding connected regions of binary images

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.