Sto sviluppando un algoritmo flood_fill in MATLAB proprio adesso, e ho un piccolo problema che mi è costato un sacco di tempo:
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
Quindi la cosa è: flood_fill funziona correttamente attraverso tutti i pixel, ma quando tutti i pixel sono impostati su 0, non si ferma! per esempio. in questa matrice:
testMatrix = zeros(20);
testMatrix(5:10,5:10) = 1;
... scende nella colonna fith, su nel sei, giù nel settimo ... su nel decimo (7,10 -> 6,10 -> 5,10) e poi (8,10 -> 9, 10 -> 10,10 -> 10,9 ...). Da dove viene questo effetto?
risposte:
0 per risposta № 1Mi dispiace, mi sono concentrato sulla cosa sbagliata L'ho appena risolto accedendo a myMatrix direttamente, invece del parametro. Ha funzionato bene!
Cordiali saluti, R93!