Recent posts / Archive

Categories

Getting stuck in a loop

Originally posted by on 14:46 Thu 27 September 2007, last modified 18:27 Sat 15 December 2007.

File under: object segmentation objective-c phd programming

I've recently been battling with an algorithm, or rather bugs in my implementation of an algorithm, to isolate homogeneous regions in an image. Homogeneous regions? By this we mean areas of identical intensity (the pixel values are the same). An example of my input image is shown below:

Some shapes

While humans can look at the above image and easily determine that there are 10 or so distinct sizable shapes at once, i.e. considering the image in its entirety in one go, a computer can't do this. We have to iterate over the image domain and consider each pixel in turn. Therefore we have to determine the connectivity of pixels which have the same intensity values. I was getting stuck in an infinite loop because my algorithm was attempting to merge groups of pixels together if it determines that they are part of the same shape. So, such an algorithm could start where every pixel is a distinct shape/group, and then recursively merge adjoining pixels of the same intensity until all the shapes have been discovered.

The problem with such an approach is that it is very tricky to merge groups correctly in a systematic way, without ending in a loop such as merge A into B, and B into C, and C in A. As the diagram at the top shows, a complex path can emerge that creates an infinite loop. Obviously it is possible to watch for such a senario, but a simpler method is described in the flow diagram below:

Object Isolation Flow Diagram

So we can use a queue (instead of the stack) to perform recursion. We add the 8 neighbourhood pixels to the queue (if they should be part of the shape), and keep going until the queue is empty. Once the queue is empty, the current shape is complete, and control returns to traversing through the image. Simple!

comment

Comment on article