matlab图像分割(标记大圆和小圆,计算数量)

 原图:

 

代码:

img = imread('Fig0941.tif');
 
% 二值化处理
bw_img = imbinarize(img);
 
%中值滤波
n=3;    %模板大小
[height, width]=size(bw_img);   %获取图像的尺寸(n小于图片的宽高)
bw_img=double(bw_img);  %数据类型转换
bw_img=medfilt2(bw_img,[n,n]);
 
% 开运算和闭运算处理
se1 = strel('disk',1);
se2 = strel('diamond',14);
se3= strel('disk',5);
bw_img = imopen(bw_img, se1);figure,subplot(221),imshow(bw_img );title('开运算');
bw_img = imdilate(bw_img, se1);subplot(222),imshow(bw_img );title('再膨胀');
 
bw_img = imerode(bw_img,se2);subplot(223),imshow(bw_img );title('再次腐蚀');
bw_img = imdilate(bw_img,se3);subplot(224),imshow(bw_img );title('再膨胀');
 
% 对图像进行连通域分析
cc = bwconncomp(bw_img);
 
% 获取连通域的属性,包括圆心坐标和半径
props = regionprops(cc, 'Centroid', 'EquivDiameter');
% 在图像上标记大圆和小圆
marked_img = img;
small = 0;
large = 0;
for i = 1:length(props)
    if props(i).EquivDiameter > 28
        marked_img = insertText(marked_img, props(i).Centroid, 'big', 'FontSize', 18, 'BoxOpacity', 0, 'TextColor', 'r');
        large=large+1;
    else
        marked_img = insertText(marked_img, props(i).Centroid, 'small', 'FontSize', 18, 'BoxOpacity', 0, 'TextColor', 'g');
        small=small+1;
    end
end
% 显示并保存标记后的图像
 
 
figure;
imshow(marked_img);
 
txt = ['大圆的数量: ' num2str(large) ];
text(4,500,txt,'Color','white','FontSize',14)
txt = ['小圆的数量: ' num2str(small) ];
text(4,520,txt,'Color','white','FontSize',14)

 结果:

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值