I need an algorithm to travel through all the cells, in an area which has been defined, in a 2 dimenional map (i.e. image)
Currently i have made and am using a wall following routine with a stack of “Multiple Direction Fork” co-ordinates. My problem is that the stack can get very large.
basically the algorithm is:
- set current pixel to (0,0)
- set Direction to none.
- if pixel above current is empty and pixel left is non-empty, Direction = UP
- else if pixel left of current is empty and pixel below is non-empty, Direction = LEFT
- else if pixel below current is empty and pixel right is non-empty, Direction = DOWN
- else if pixel right of current is empty and pixel above is non-empty, Direction = RIGHT
- if Direction is UP
6a. if pixels (-1,-1) and (+1,-1) or (+1,+1) are non empty then add current pixel to stack. - if Direction is DOWN
7a. if pixels (-1,+1) and (+1,+1) or (-1,-1) are non empty then add current pixel to stack. - if Direction is RIGHT
8a. if pixels (+1,-1) and (+1,+1) or (-1,+1) are non empty then add current pixel to stack. - if Direction is LEFT
9a. if pixels (-1,-1) and (-1,+1) or (+1,-1) are non empty then add current pixel to stack. - if direction is not none process current pixel
10a. else if there are pixels in the stack then pop a pixel and set the current pixel to it.
10b. else exit algorithm - move current pixel in direction specified.
- repeat 1
Does anyone know how to reduce the number of pixels entering the stack or indeed know of a better algorithm to traverse all the pixels in a shape.
The following pictures illusrate my problem:
With out stack pixel indicator(Blue):
www.geocities.com/budgetanime/lena.gif.txt
With stack pixel indicator (Blue)
www.geocities.com/budgetanime/lena1.gif.txt
www.geocities.com/budgetanime/server1.gif.txt
(shaded pixels are the pixels that have been processed, solid blue pixels are pixels that had been in the stack at some point, grey/white pixels define the bounding areas and black pixels are unprocessed pixels)
(you will have to change the file extension after you have downloaded it)
As you can see there are long diagonal lines of (blue) pixels that have been in the stack.
Any suggestions are welcome!
Thanks