首页 > 解决方案 > 查找图像中物体的直径

问题描述

我在使用 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;

该代码用于显示斑点的直径并在线显示,但由于结果不准确而缺少某些内容。

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

输出

这是输入图像:

basebal.jpg

标签: matlabimage-processing

解决方案


根据您希望结果的准确性和自动化程度,您有几个机会。

最简单的是使用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);

这使

棒球


推荐阅读