[Image-SIG] An algorithm to find connected groups of pixels

Douglas S. Blank dblank at brynmawr.edu
Thu Sep 27 19:57:32 CEST 2007

Jeffrey Wise wrote:
> Hi, 
> I'm in my first week with PIL and doing well - it's a tremendous library!
> I've gotten to the point in my image analysis that I need to find all the
> groups of connected pixels in an image.  These groups form arbitrary shapes
> in the image.  I want to preserve the shape information for each group, so I
> can compare/analyze/categorize... it later.  
> The image is composed of pixels of value 0 or 255 in "L" B&W format.  I
> assume such an algorithm will need to find a pixel of value 255 and then
> check in every direction about it for a neighbor that is 255 too.  The
> algorithm would presumably recurse to find neighbors of the neighbor, etc.
> I suspect the trick in this algorithm is the data structures needed to keep
> track of the discovered adjacencies.  I have loaded the image into the 2D
> array-like accessor, so I have free access to each pixel.
> I haven't noticed such an algorithm in the PIL yet; maybe I've missed it.
> Or perhaps there is Python code available to do this algorithm? Any ideas?


You can take a look at some code from Pyrobot, Python Robotics, that was 
written by a student:


Start with the Blob class. We also have similar code in C++, and wrapped 
by Python using SWIG:


If you are interested in image processing, robotics, or other 
camera-based functions, see our course materials here:



> Thanks,
> .Jeff Wise
> _______________________________________________
> Image-SIG maillist  -  Image-SIG at python.org
> http://mail.python.org/mailman/listinfo/image-sig

More information about the Image-SIG mailing list