Object detecting using thresholding

蹲街弑〆低调 提交于 2020-01-10 04:23:20

问题


I'm working on a program in matlab to detect an object in a sequence of images. The object I'm trying to detect a red ball.

First, I tried to use thresholding to segment the ball from the image, but I couldn't do that. I couldn't get rid of the shadow under the ball. Any Ideas how to get rid of the small part under the ball?

My second question is, I want to make sure that the object I'm looking for is a red ball. my code will detect any red object, I want to make sure its a circle.

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

回答1:


Perhaps converting the image from RGB to HSV can be a good idea.

img = im2double(imread('http://i.stack.imgur.com/D3Zm7.jpg'));
imgHSV = rgb2hsv(img);

Let's display the H part, which contains only color information:

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

Notice the red portion is distributed at the extremes of the spectrum. Let's try thresholding it using empirical values (by looking at the bar, we can have an first guess of some "good" values):

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

And display the results:

imshow(BW);

No more shadows! :)



来源:https://stackoverflow.com/questions/23252114/object-detecting-using-thresholding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!