No BSD License  

Highlights from
Flood Fill

2.0

2.0 | 2 ratings Rate this file 13 Downloads (last 30 days) File Size: 1.06 KB File ID: #5319

Flood Fill

by James Goodwin

 

21 Jun 2004 (Updated 22 Jun 2004)

Flood Fill code for matrix.

| Watch this File

File Information
Description

The flood fill will alter all values of 0 to one flood filling from a start point (xc,yc)

If the flood fill starts in an enclosed space it will fill up to the boundary.

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
22 Feb 2007 Jyotirmoy Banerjee

Available stack space may not be enough as it applies recursion
Increase the recursion limit using
  N = 200;
  set(0,'RecursionLimit',N);
note -> if you increase N to a very large value then you may get segmentation fault

you can instead use imfill cmd in matlab

% Add this to make it 8 connected
if (fill(y,x) == old)

    fill(y,x) = new;

    flood_fill(xc,yc,x+1,y,new,old,width,height);

    flood_fill(xc,yc,x,y+1,new,old,width,height);

    flood_fill(xc,yc,x-1,y,new,old,width,height);

    flood_fill(xc,yc,x,y-1,new,old,width,height);

    flood_fill(xc,yc,x+1,y+1,new,old,width,height);

    flood_fill(xc,yc,x-1,y+1,new,old,width,height);

    flood_fill(xc,yc,x+1,y-1,new,old,width,height);

    flood_fill(xc,yc,x-1,y-1,new,old,width,height);

end

17 Mar 2010 Trevor Beugeling

Although this code is a good example of recursion, it has a bug which can give incorrect flood-fill results.

The termination conditions need to be changed to:

if (x<2 || x>=width)
    x = xc;
    y = yc;
end

if (y<2 || y>=height)
    x = xc;
    y = yc;
end

Also note that these conditions mean that the border of the matrix will not be evaluated. If you wish to fill right to the edges of the matrix, the conditions need to be adjusted accordingly.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
data exploration James Goodwin 22 Oct 2008 07:24:43
flood fill James Goodwin 22 Oct 2008 07:24:43
code for matrix James Goodwin 22 Oct 2008 07:24:43
graphical data James Goodwin 22 Oct 2008 07:24:43
graphics James Goodwin 22 Oct 2008 07:24:43
flood James Goodwin 22 Oct 2008 07:24:43
code for matrix Tareq Almustafa 02 Dec 2011 12:41:15

Contact us at files@mathworks.com