MATLAB的图像处理学习日志之对圆形目标的计算参数和具体方案要求详细02都在代码里面了
源码整理如下:
clear all;close all;clc;
rgb22=imread('yuanshi555.jpg');
subplot(221);
imshow(rgb22);title('原始图像');
I11=rgb2gray(rgb22);
thess=graythresh(I11);
bw01=im2bw(I11,thess);%阈值分割二值化
subplot(222);
imshow(bw01);title('二值图像');
bw02=bwareaopen(bw01,5);%形态学开运算去除像素小于10的目标
subplot(223);
imshow(bw02);title('开运算去除噪声');
sse=strel('disk',2);
%寻找图像目标的边界
[B,L]=bwboundaries(bw03,'noholes');
subplot(224);hold on;
imshow(label2rgb(L,@jet,[.1 .1 .1]));title('检测目标图像');
for k =1:length(B)
boundary=B{k};
plot(boundary(:,2),boundary(:,1),'y','LineWidth',1);
end
%获取标识区域的面积质心等参数
stats=regionprops(L,'Area','Centroid');
%设置阈值
thess=0.95;
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_str=sprintf('%2.2f',metric);
if metric>thess
centroid=stats(k).Centroid;
plot(centroid(1),centroid(2),'ko');
end
text(boundary(1,2)-35,boundary(1,1)+13,metric_str,...
'Color','y','FontSize',14,'FontWeight','bold');
end
结果显示:
参考教材资料:
MATLAB图像处理函数及其应用——电子工业出版社