查找图像中对象的直径

时间:2018-07-09 17:28:33

标签: matlab image-processing

使用MATLAB查找图像中对象的直径时遇到问题。下面是它的源代码。请帮助我代码中缺少的内容。

obj=imread('C:\Users\Khan\Desktop\basebal.jpg');
imshow(obj);
red =obj(:,:,1);
green =obj(:,:,2);
blue =obj(:,:,3);
figure
subplot(2,3,1),imshow(obj);title('original image');
subplot(2,3,2),imshow(red);title('red ball');
subplot(2,3,3),imshow(green);title('green ball');
subplot(2,3,4),imshow(blue);title('blue ball');

figure
level =0.41;
bw2=im2bw(blue,level);
subplot(2,2,1),imshow(bw2);title('blue plane thresholded');
fill =imfill(bw2,'holes');
subplot(2,2,2),imshow(fill);title('holes filled');
clear = imclearborder(fill);
subplot(2,2,3),imshow(clear);title('remove blobs on border');
se=strel('disk',7);
open=imopen(fill,se);
subplot(2,2,4),imshow(open);title('remove small blobs');
diameter = regionprops(open,'MajorAxisLength');
figure
imshow(obj)
d=imdistline;

该代码用于显示斑点的直径并在线显示,但是 由于结果不准确,因此缺少一些东西。

此图显示了代码的输入和输出:

output

这是输入图像:

basebal.jpg

1 个答案:

答案 0 :(得分:1)

根据您希望得到的结果的准确性和自动化程度,您会有很多机会。

最简单的方法是使用d = imdistline;,它是一个交互式工具,可让您像尺子一样测量球。

imshow(obj)
d = imdistline;

或者,您可以使用imfindcircles函数,该函数允许您指定半径范围,然后查找具有该半径的圆形对象。虽然,您必须发挥敏感性,但我很容易发现以下方法有效

[centers,radii] = imfindcircles(obj,[100 150],'Sensitivity',0.95);

哪个告诉我半径为109.6432,因此直径为219.2864

您还可以使用

绘制圆
imshow(obj)
viscircles(centers,radii);

给出

baseball

相关问题