Marching squares: Finding multiple contours within one source field?

Posted by TravisG on Game Development See other posts from Game Development or by TravisG
Published on 2011-08-26T20:52:13Z Indexed on 2012/09/14 21:49 UTC
Read the original article Hit count: 227

Filed under:

Principally, this is a follow-up-question to a problem from a few weeks ago, even though this is about the algorithm in general without application to my actual problem.

The algorithm basically searches through all lines in the picture, starting from the top left of it, until it finds a pixel that is a border. In pseudo-C++:

int start = 0;
for(int i=0; i<amount_of_pixels; ++i)
{
   if(pixels[i] == border)
   {
      start = i;
      break;
   }
} 

When it finds one, it starts the marching squares algorithm and finds the contour to whatever object the pixel belongs to.

Let's say I have something like this:

enter image description here

Where everything except the color white is a border.

And have found the contour points of the first blob:

enter image description here

For the general algorithm it's over. It found a contour and has done its job. How can I move on to the other two blobs to find their contours as well?

© Game Development or respective owner

Related posts about 2d