我现在正在用MATLAB开发一个flood_fill算法,我有一个小问题,花了我很多时间:
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所以问题是: flood_fill在所有像素上正确运行,但当所有像素都设置为0时,它不会停止!例如,在这个矩阵中:
testMatrix = zeros(20);
testMatrix(5:10,5:10) = 1;..it在第一列下降,在第六列上升,在第七列下降..第10位向上(7,10 -> 6,10 -> 5,10),然后是(8,10 -> 9,10 -> 10,10 -> 10,9 ...)。这种效果从何而来?
发布于 2014-10-07 15:32:02
对不起。我把注意力集中在错误的事情上..我只是通过直接访问myMatrix而不是参数来解决它。它工作得很好!
致以最好的问候,R93!
https://stackoverflow.com/questions/26230426
复制相似问题