我正在 matlab 中编写一个程序来检测图像序列中的对象。我试图检测的物体是一个红球。
首先,我尝试使用阈值从图像中分割球,但我做不到。我无法摆脱球下的阴影。有什么想法如何去除球下的小部分吗?
我的第二个问题是,我想确保我正在寻找的物体是一个红球。我的代码将检测任何红色物体,我想确保它是一个圆圈。
My code:
I1 = imread('images/B1.jpg'); % read image
ID1 = im2double(I1); % convert to double
IDG1 = rgb2gray(ID1); % conver to gray scale
t = 112; % set a thresholding value
IT = im2bw(IDG1, t/255); % apply the threshold
I2 = ~IT; % get a nigative image
I3 = bwareaopen(I2,40); % get rid of small unwanted pixels
I4 = imclearborder(I3); % clear pixels of the borders
I5 = bwareaopen(I4,60); % get rid of small unwanted pixels
I6 = imfill(I5,'holes'); % fill the gap on the ball top part
I7 = imclearborder(I6); % get rid of small unwanted pixels
也许将图像从 RGB 转换为 HSV 是个好主意。
img = im2double(imread('https://i.stack.imgur.com/D3Zm7.jpg'));
imgHSV = rgb2hsv(img);
让我们显示H
部分,仅包含颜色信息:
imshow(imgHSV(:,:,1))
colormap('hsv')
colorbar;
注意red部分分布在频谱的极端处。让我们尝试使用经验值对其进行阈值处理(通过查看条形图,我们可以初步猜测一些“好的”值):
BW = imgHSV(:,:,1) < 0.05 | imgHSV(:,:,1) > .15;
并显示结果:
imshow(BW);
不再有阴影! :)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)