clear,clc
RGB = imread('pillsetc.png');
figure,imshow(RGB);
I = rgb2gray(RGB);
threshold = graythresh(I);
bw = im2bw(I,threshold);
figure,imshow(bw)
% remove all object containing fewer than 30 pixels
bw = bwareaopen(bw,30);
% fill a gap in the pen's cap
se = strel('disk',2);
bw = imclose(bw,se);
% fill any holes, so that regionprops can be used to estimate
% the area enclosed by each of the boundaries
bw = imfill(bw,'holes');
figure,imshow(bw)
ed=edge(bw);
figure,imshow(ed)
%%%%%以上是图像二值化 上面制作二值化图像L = bwlabel(bw);
L1 = bwlabel(ed);
Ar=zeros(1,max(L(:)));
perimeter=zeros(1,max(L1(:)));
metric=zeros(1,max(L1(:)));
Pwl=zeros(1,max(L1(:)));
Pr=zeros(1,max(L1(:)));
for i=1:max(L(:))
Ar(i)=sum(bw(L==i));
perimeter(i)=sum(ed(L==i));
metric(i) = 4*pi*Ar(i)/perimeter(i)^2;
[y,x]=find(L==i);
x0=min(x(:));
x1=max(x(:));
y0=min(y(:));
y1=max(y(:));
hold on
rectangle('Position',[x0,y0,x1-x0,y1-y0],'edgeColor','g','LineWidth',1)
if x1-x0>=y1-y0
Pwl(i)=(x1-x0)/(y1-y0);
else
Pwl(i)=(y1-y0)/(x1-x0);
end
Pr(i)=Ar(i)/((y1-y0)*(x1-x0));
end
Ar
perimeter
metric
Pwl
Pr