a1=im2bw(a0,thresh);
a2=imfill(a1,'holes');
se = strel('disk',3);
Image = imopen(a2,se);
I2=medfilt2(Image,[3 3]);
se = strel('disk',5);
I3=imerode(I2,se);
I4=imdilate(I3,se);
L0= bwlabel(I4);
Ar=zeros(1,max(L0(:)));
for i=1:max(L0(:))
Ar(i)=sum(I4(L0==i));
end
W2=bwareaopen(I4,2000);
W0=I4-W2;
X2=~W2;
Qt=bwdist(X2);
rm=imregionalmin(Qt);
im=imextendedmin(-Qt,3);
fim=a0;
fim(im)=255;
Lim=watershed(bwdist(im));
em=Lim==0;
I5=W2&~em;
aaa=bwareaopen(I5,300);
bbb=aaa+W0;
[L,NUM1]=bwlabel(bbb,8);
if ndims(I) == 2
colorpic(:,:,1)=I;
colorpic(:,:,2)=I;
colorpic(:,:,3)=I;
else
colorpic=I;
end
if ndims(colorpic) == 3
d1 = repmat(bbb(:), 1, 3);
d1 = reshape(d1, size(colorpic, 1), size(colorpic, 2), 3);
end
d2 = uint8(d1).*colorpic;
s = regionprops(L, 'centroid','BoundingBox');
centroids = cat(1, s.Centroid);
figure
imshow(I);
hold on
for i=1:size(s,1)
hold on;
rectangle('position',s(i).BoundingBox,'edgecolor','y','linewidth',2);
temp = s(i).Centroid;
plot(temp(1),temp(2),'r+');
drawnow;
end
hold off
求大神给下详细注释 谢谢了 |