3

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.

enter image description here

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?

enter image description here

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
N4LN
  • 95
  • 3
  • 10
  • Are you asking for a code that identifies a "red object" as a ball? You might be better off splitting this into two questions. The first asking how to get rid of the shadow (but make sure to show what you have tried), the second asking about how to ensure that a detected object is a ball (again, provide what you have attempted and why it doesn't work). – Schorsch Apr 23 '14 at 18:27
  • No, I want the way to do that. For the first part, I showed my code on the question. For the second part, I'm asking what ways can be used to make sure it is a ball not any other object. As if I only look for red colour, there might be another object which is not a circle and its red. – N4LN Apr 23 '14 at 20:57

1 Answers1

8

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

img = im2double(imread('https://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;

enter image description here

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);

enter image description here

No more shadows! :)

Rafael Monteiro
  • 4,509
  • 1
  • 16
  • 28
  • Thanks Rafael. Do you have any idea how can I check if its a circle or not ? – N4LN Apr 23 '14 at 21:44
  • There was another question a couple days ago about segmenting lung nodules. The OP wanted to detect round connected components, and I gave a suggestion. Here is the answer, it might be useful for you: http://stackoverflow.com/a/23171975/2325919 – Rafael Monteiro Apr 23 '14 at 21:48
  • 2
    you can simply use `edge` and then a circular Hough transform http://www.mathworks.com/help/images/ref/imfindcircles.html – bla Apr 24 '14 at 05:39