RGB = imread('pillsetc.png');%读取图像
figure; imshow(RGB);%显示
I = rgb2gray(RGB);%转化为灰度图像
threshold = graythresh(I);%阈值
bw = im2bw(I,threshold);%转化为二值图像
figure; imshow(bw)%显示二值图像
bw = bwareaopen(bw,30);%去除小目标
se = strel('disk',2);%圆形结构元素
bw = imclose(bw,se);%关操作
bw = imfill(bw,'holes');%填充孔洞
figure; imshow(bw)%显示填充孔洞后的图像
[B,L] = bwboundaries(bw,'noholes');%图像边界
figure; imshow(label2rgb(L, @jet, [.5 .5 .5]))%不同颜色显示
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2),boundary(:,1), 'w', 'LineWidth', 2)%显示白色边界
end
stats = regionprops(L,'Area','Centroid');%求取面积、质心等
threshold = 0.94;%阈值
for k = 1:length(B)
boundary = B{k};
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));%求取周长
area = stats(k).Area;%面积
metric = 4*pi*area/perimeter^2;%圆形的量度
metric_string = sprintf('%2.2f',metric);