matlab - 使用阈值检测对象

标签 matlab image-processing threshold

我正在 matlab 中开发一个程序来检测一系列图像中的对象。我要检测的对象是一个红球。

enter image description here

首先,我尝试使用阈值从图像中分割出球,但我做不到。我无法摆脱球下的影子。任何想法如何摆脱球下的小部分?

enter image description here

我的第二个问题是,我想确定我正在寻找的物体是一个红球。我的代码会检测到任何红色物体,我想确保它是一个圆圈。

我的代码:

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('http://i.stack.imgur.com/D3Zm7.jpg'));
imgHSV = rgb2hsv(img);

让我们显示 H 部分,它只包含颜色信息:

imshow(imgHSV(:,:,1))
colormap('hsv')
colorbar;

enter image description here

请注意,红色 部分分布在光谱的两端。让我们尝试使用经验值对其进行阈值处理(通过查看条形图,我们可以初步猜测一些“好”值):

BW = imgHSV(:,:,1) < 0.05 | imgHSV(:,:,1) > .15;

并显示结果:

imshow(BW);

enter image description here

没有阴影了! :)

关于matlab - 使用阈值检测对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23252114/

相关文章:

matlab - Ctrl+Backspace/Option+Delete 无法删除 MATLAB IDE 中的上一个单词

java - 使用 java 8 并行处理图像

matlab - 生产的内核类型

python - 如何在随机森林中设置自己的概率阈值?

r - R 中的 Gamma 分布下限

matlab - 在 MATLAB 中计算音频音高?

matlab - 如何访问元胞数组中的函数句柄?

oop - 为什么我不能对重载 subsref 的类使用内置函数?

c# - 如何在 C# 中将位图图像转换为黑白图像?

c++ - Opencv C++ 阈值在 absdiff、条码检测后不起作用