Pixel "selection" algorithm

I am trying to write an algorithm that operates on a 2-bit (Black and white) image. Given a black pixel, it needs to be able to find all other black pixels that are directly connected to it, or indirectly connected to it (connected to it by being connected to another pixel that is directly connected to it, however many levels deep). (If you have ever used photoshop before, I am basically refering to the idea of a very simplified magic wand selection tool) I currently have this method implemented recursively, it traverses through the pixels as though they were a tree structure, which works for smaller images, but for larger images (longer paths to traverse) it gets a stack overflow. Does anyone know of any algorithms that would preform this kind of a task?

[772 byte] By [AcidRainLiTEa] at [2007-10-2 0:55:38]
# 1
maybe you could review some open-source graphic programs and check their algorithms? something on linux? (gimp?)
AcidRainLiTEa at 2007-7-15 18:15:34 > top of Java-index,Other Topics,Algorithms...
# 2

It is fairly easy to convert a recursive "connected-components" algorithm to an iterative one. You just have to maintain your own stack (Use one of the List implementations in java.util)

Remember that the pathological case for a recursive connected components algorithm results in the depth of recursion equaling the number of pixels in the image.

matfud

matfuda at 2007-7-15 18:15:34 > top of Java-index,Other Topics,Algorithms...
# 3
http://forum.java.sun.com/thread.jspa?threadID=642222&messageID=3786772#3786772here is another thread on this topic. The algorithm that you are looking for is called "floodfill" which you can google.
marlin314a at 2007-7-15 18:15:34 > top of Java-index,Other Topics,Algorithms...
# 4

It depends on traversal algorithm used. If you use depth first search i.e. for each black spot you then visit all its neighbours then you can end up building a stack because you are moving outwards from a pointed and this movement is unbounded.

I suggest you use breadth first search as one point would have only 8 near by neightbours (Bounded)

quarKreiga at 2007-7-15 18:15:34 > top of Java-index,Other Topics,Algorithms...