From: MR SHIBI SHIBI on
the following is cinda of floodfill algorithm only instaed of paint pixel it save thier indexes
i am looking for a way to make it run faster



function P=floodfill(img,x,y,tolerance,target)
% input: img for an grayscale image
% x,y point in the image;(check it)
% tolerance is
% target is the pixel value usualy x,y value
% ouput: set of point that where fill

res=zeros(20000,2); %the final result is inserted here
q=zeros(40000,2); %temporrey queue
count=1;
sz=size(img);
zom=zeros(sz(1),sz(2)); %matrix that saves pixel alradey checked
i=1;
j=2;
q(1,1)=x;
q(1,2)=y;
while((j-i)>0)


if((abs(img(q(i,1),q(i,2))-target))<tolerance)
res(count,1)=q(i,1);
res(count,2)=q(i,2);
count=count+1;

if(count>=size(res,1)) %growing the space
sz=size(res);
tmp=zeros(sz(1)+20000,2);
tmp(1:sz(1),1)=res(:,1);
tmp(1:sz(1),2)=res(:,2);
res=tmp;
end

if(j+4>=40000)
tmp=zeros(40000,2);%copy the data
for k=1:((j-i))
tmp(k,1)=q(i+k-1,1);
tmp(k,2)=q(i+k-1,2);
end
q=tmp;
j=((j-i)+1);
i=1;

end
%the following insert new pixel into the checking queue->q
if(q(i,1)+1<size(img,1))
if(zom(q(i,1)+1,q(i,2))==0)



zom(q(i,1)+1,q(i,2))=1;
q(j,1)=q(i,1)+1;
q(j,2)=q(i,2);
j=j+1;
end
end
if(q(i,1)-1>0)
if(zom(q(i,1)-1,q(i,2))==0)



zom(q(i,1)-1,q(i,2))=1;
q(j,1)=q(i,1)-1;
q(j,2)=q(i,2);
j=j+1;
end
end
if(q(i,2)+1<size(img,2))
if(zom(q(i,1),q(i,2)+1)==0)




zom(q(i,1),q(i,2)+1)=1;
q(j,1)=q(i,1);
q(j,2)=q(i,2)+1;
j=j+1;
end
end
if(q(i,2)-1>0)
if(zom(q(i,1),q(i,2)-1)==0)



zom(q(i,1),q(i,2)-1)=1;
q(j,1)=q(i,1);
q(j,2)=q(i,2)-1;
j=j+1;
end
end
end
i=i+1;
end
count=count-1;
i=res(1:count,1);
j=res(1:count,2);
P=[i j];

thnk
From: Darren Rowland on
I didn't take a good look at your code but it does seem quite messy. It might be better if you start with a working flood-fill procedure and modify it for what you want.

The following code comes from the "Writing Fast Matlab Code" guide by Pascal Getreuer on the FEX.

Hth,
Darren

function I = flood2(I,c,x,y)
% Flood fills image I from point (x,y) with color c.
LastFlood = zeros(size(I));
Flood = LastFlood;
Flood(y,x) = 1;
Mask = (I == I(y,x));
FloodFilter = [0,1,0; 1,1,1; 0,1,0];
while any(LastFlood(:) &#732;= Flood(:))
LastFlood = Flood;
Flood = conv2(Flood,FloodFilter,'same') & Mask;
end
I(find(Flood)) = c;