clear
clc
tol=10;
img=double(imread('cameraman.tif'));
seed=32640;
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
for i=1:length(foreground_queue)
test_pixel=foreground_queue(i);
diff=abs(img(seed)-img(test_pixel));
if diff<=tol
foreground_queue_test{i} = bsxfun(@plus, test_pixel, neighbor_offsets);
foreground_pass{i}=test_pixel;
end
if diff>tol
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{:}));
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)