Im developing a flood_fill algorithm in MATLAB right now, and I have a little problem that cost me a lot of time:
function [ homoPoints ] = area2Points( matrix )
%AREA2POINTS makes an area of one's to only one one in the middle of the area
% Input : Matrix with areas of one's
% Output : result has one point in the middle of every former area
myMatrix = matrix;
[row, col] = find(myMatrix);
curPoint = [ row(1),col(1) ];
area = [curPoint(1),curPoint(2)];
myMatrix(curPoint(1),curPoint(2)) = 0;
myMatrix = fill(curPoint(1),curPoint(2),myMatrix);
%Problem
function[ matrix ] = fill(x,y,matrix)
area
%matrix(x,y) = 0; %theoretisch unnötig
%If the pixel under curPoint is a 1..
if(matrix(x + 1 , y) == 1)
area = vertcat(area,[x+1 , y]);
matrix(x+1,y) = 0;
fill(x+1,y,matrix);
end
%If the pixel left from curPoint is a 1..
if(matrix(x, y - 1) == 1)
area = vertcat(area,[x , y-1]);
matrix(x,y-1) = 0;
fill(x,y-1,matrix);
end
%If the pixel over curPoint is a 1..
if(matrix(x - 1, y) == 1)
area = vertcat(area,[x-1 , y]);
matrix(x-1,y) = 0;
fill(x-1,y,matrix);
end
%If the pixel right from curPoint is a 1..
if(matrix(x , y + 1) == 1)
area = vertcat(area,[x , y+1]);
matrix(x,y+1) = 0;
fill(x,y+1,matrix);
end
return
end
So the thing is: The flood_fill runs correctly through all pixels, but when all pixels are set 0, it doesnt stop! e.g. in this matrix:
testMatrix = zeros(20);
testMatrix(5:10,5:10) = 1;
..it goes down in the fith column, up in the six, down in the seventh.. up in the tenth (7,10 -> 6,10 -> 5,10) and then (8,10 -> 9,10 -> 10,10 -> 10,9 ...). Where does this effect come from?