help in connected component algorithm

Status
Not open for further replies.

mona:)

New Member
Hi
i've written a matlab code to count connected component for binary images. but matlab shows this eeror "Subscript indices must either be real positive integers or
logicals" Could u help me to correct it?(i know that bwlable do it easily but i must write an algorithm)
thank u.
Here is the code:
imagepath='C:\Documents and Settings\harry patter\My Documents\My Pictures\m.bmp'
[I map]=imread(imagepath);

Ig=I;
if ~isempty(map)
Ig=ind2gray(I,map);
elseif length(size(I))==3
Ig=RGB2gray(I);
else
Ig=I;
end;
Ig=logical(Ig);
Ig=~Ig;
Ig=im2double(Ig);
imshow(Ig);
n=1;
[M, N]=size(Ig);
for i=1:1:M ;
for j=1:1:N;
if Ig(i,j)==1
Ig(i,j)=n;
n=n+1;
end
for k=-1:1:1;
for s=-1:1:1;
if I(i+k,j+s)==1
I(i+k,j+s)=n;
end
end
end
if (Ig(i-1,j)==0 && Ig(i,j-1)==0)
n=n+1;
end
d=[Ig(i-1,j-1) Ig(i-1,j) Ig(i-1,j+1) ;Ig(i,j-1) Ig(i,j) Ig(i,j+1)];
V=min(sparse(d));
Ig(i,j)=V;
end
end
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…