labeling
4 views (last 30 days)
Show older comments
I'm trying to do labeling after segmentation into four regions (white, light gray, dark gray and black) according to threshold method. This is the code:
clear all;
c = imread('image1_069.jpg');
a=double(c);
b = zeros(size(a));
b = uint8(a<90);
b(a >= 110) = 180;
b(a >= 150) = 200;
b(a >= 200) = 255;
figure(1);
imshow(b);
%Labeling
ImgLB=b;
ImgLB(1,:)=0; ImgLB(xsize,:)=0; ImgLB(:,1)=0;
ImgLB(:,ysize)=0;
label=0;
for kx=2:xsize-1
for ky=2:ysize-1
W=zeros(3);
W(1:3, 1:3)=ImgLB(kx-1:kx+1,ky-1:ky+1);
maxW=max(W(:));
if ImgLB(kx, ky)==1 & ImgLB(kx,ky-1)==0 & maxW==1
label=label+1;
ImgLB(kx,ky)=label;
end
if ImgLB(kx,ky)==1 & maxW>=1
ImgLB(kx,ky)=maxW;
end
end
end
figure(1)
subplot(1,3,1), imagesc(c),title ('Input')
subplot(1,3,2), imagesc(b), title('Threshold')
subplot(1,3,3), imagesc(unit8(ImgLB*255/max(max(ImgLB)))), title('Label')
colormap('gray');
loop=1;
while loop
flag=0;
for kx=2:xsize-1
for ky=2:ysize-1
if ImgLB(kx,ky)~=0 & ImgLB(kx+1,ky)~=0 & ImgLB(kx,ky)~=ImgLB(kx+1,ky)
old=ImgLB(kx,ky);
new=ImgLB(kx+1,ky);
flag=1;
break;
end
end
end
if flag==0
break;
end
for kx=2:xsize-1
for ky=2:ysize-1
if ImgLB(kx,ky)==old
ImgLB(kx,ky)==new;
end
end
end
end
But, there is error at the line subplot(1,3,3), imagesc(unit8(ImgLB*255/max(max(ImgLB)))), title('Label')
I have no idea what's the mistake in that line. Is there any suggestion. This is the image I used: http://i41.tinypic.com/243glli.jpg
0 Comments
Answers (2)
Walter Roberson
on 2 Feb 2012
There is no MATLAB routine named "unit8". Perhaps you meant "uint8".
5 Comments
Image Analyst
on 4 Feb 2012
There's a mistake there. You're confusing rows and columns with x and y. The size function gives rows and columns, in that order, which would be ysize and xsize. So the corrected code would be:
[ySize xSize numberOfColorChannels] = size(c);
if numberOfColorChannels > 1
a=double(c(:,:,2))/255.0;
else
a=double(c)/255.0;
end
You can learn about debugging in the help file somewhere, I'm sure.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!