Algorithm Matlab中的洪水填充算法-工作正常,但不会停止
我现在正在MATLAB中开发洪水填充算法,我有一个小问题,花费了我很多时间:Algorithm Matlab中的洪水填充算法-工作正常,但不会停止,algorithm,matlab,image-processing,recursion,flood-fill,Algorithm,Matlab,Image Processing,Recursion,Flood Fill,我现在正在MATLAB中开发洪水填充算法,我有一个小问题,花费了我很多时间: 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
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
所以问题是:泛光填充正确地贯穿所有像素,但是当所有像素都设置为0时,它不会停止!e、 g.在本矩阵中:
testMatrix = zeros(20);
testMatrix(5:10,5:10) = 1;
…它在第五列下降,在第六列上升,在第七列下降。。在第十位(7,10->6,10->5,10),然后是(8,10->9,10->10,10->10,9…)。这种效果是从哪里来的?对不起。我把注意力集中在错误的事情上。。 我只是通过直接访问myMatrix而不是参数来解决它。 那很好
最好的问候,R93 在堆栈溢出问题上鼓励您回答自己的问题,如果您知道如何解决,但是如果您可以添加一些附加信息,并用更多细节解释解决方案,那就更好了。