原图:
代码:
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)
结果: