Flood Fill

Posted by ColinWhite on
URL: http://imagej.273.s1.nabble.com/Flood-Fill-tp3683773.html

Hi,

I've written a short program to flood fill a simple black and white image of the outline of a cell. The image is too big to do this recursively (about 400x400 pixels) so I've used queues. However, my program only runs for a short while and then returns and out of memory error. I'm really not sure why it ends up using an upwards of 400MB of memory before it crashes.

Any help is appreciated.
-Colin

import ij.*;
import ij.plugin.filter.PlugInFilter;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import java.util.*;
import java.lang.Math.*;

public class Filopodia_Analyzer implements PlugInFilter{
        ImagePlus analyzed;

        public int setup(String arg, ImagePlus analyzed){
                return DOES_ALL;
        }

        public void run(ImageProcessor anal_ip){

                Point n = new Point();

                ArrayDeque<Point> q = new ArrayDeque<Point>();

                if(anal_ip.getPixelValue(12, 10) == 255.0) q.offer(new Point(12, 10));

                while(q.size() != 0){
                        n = q.poll();
                        if(anal_ip.getPixelValue(n.x, n.y) == 255.0) anal_ip.putPixel(n.x, n.y, 223);

                        if(anal_ip.getPixelValue(n.x-1, n.y) == 255.0) q.offer(new Point(n.x-1, n.y));
                        if(anal_ip.getPixelValue(n.x+1, n.y) == 255.0) q.offer(new Point(n.x+1, n.y));
                        if(anal_ip.getPixelValue(n.x, n.y-1) == 255.0) q.offer(new Point(n.x, n.y-1));
                        if(anal_ip.getPixelValue(n.x, n.y+1) == 255.0) q.offer(new Point(n.x, n.y+1));
                        analyzed.updateAndDraw();
                }
        }
}