Flood filling algorithm stop condition
조회 수: 8 (최근 30일)
이전 댓글 표시
I've written an algorithm to compute a flood fill operation that finds all pixels which match the brightness of a seed pixel within a given tolerance. The goal of the algorithm is to follow this flow chart, and stop when the 'foreground queue' is empty. Here is the code I have written so far, it can succesfully find the adaptive neighborhood, but I'm having trouble finding a way to make it stop once every possible pixel has been checked.
%%LANHE
clear
clc
tol=10;
img=double(imread('cameraman.tif'));
%img=imresize(img,.25);
seed=32640;
%seed=2016;
img=padarray(img,[1 1],1);
[m,n]=size(img);
M=size(img,1);
neighbor_offsets =[M, M+1, 1, -M+1, -M, -M-1, -1, M-1];
foreground_queue=seed;
background_queue=0;
iterations=0;
while iterations<155 %~isempty(foreground_queue)
for i=1:length(foreground_queue)
test_pixel=foreground_queue(i); %top pixel off queue
diff=abs(img(seed)-img(test_pixel));
if diff<=tol %if foreground
foreground_queue_test{i} = bsxfun(@plus, test_pixel, neighbor_offsets);
foreground_pass{i}=test_pixel;
end
if diff>tol %if background
background_queue=[background_queue test_pixel];
end
end
foreground_passmat=horzcat(foreground_pass{:});
foreground_passmat(foreground_passmat==seed)=[];
foreground_queue=unique(horzcat(foreground_queue_test{:}));
%foreground_queue=setdiff(foreground_queue,foreground_passmat);
iterations=iterations+1;
l(iterations)=length(foreground_queue);
end
length(foreground_passmat)
background_queue(background_queue==0)=[];
figure()
hold on
[j,i]=ind2sub([m,n],foreground_passmat);
[j1,i1]=ind2sub([m,n],background_queue);
plot(i,-j,'*')
plot(i1,-j1,'*')
figure(2)
plot(l)
댓글 수: 0
채택된 답변
Navya Seelam
2019년 8월 7일
Hi,
You can count the total number of pixels checked by the sum of lengths of foreground_queue and background_queue. Once the sum is greater than the total number of pixels use “break” statement to break the loop.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!