2

I'm trying to consistently detect a certain color between images of the same scene. The idea is to recognize a set of object based on a color profile. So, for instance, if I'm given a scene with a green ball in it and I select that green as part of my color palette, I would like a function which has a matrix reflecting that it detects the ball.

Can anyone recommend some matlab functions/plugins/starting points for this project? ideally the function for color recognition will take an array of color values and will match them within a certain threshold.

Kinda like this: http://www.mathworks.com/matlabcentral/fileexchange/18440-color-detection-using-hsv-color-space-training-and-testing

except it works (this one didn't)

Update:

Here's why I chose not to use the above toolkit..

I start by selecting some colors of interest in the picture

Everything is going well..

and then ask the function to recognize the road in later images...

uh oh

And absolutely nothing useful is triggered. So yeah, apart from the few bugs that I came across in the code on download and fixed, this was kind of the kicker. I didn't try to fix the body of the code that recognizes the colors because.. well, I don't know how, which is why I came here.

Maurits
  • 2,082
  • 3
  • 28
  • 32
Mark
  • 409
  • 2
  • 6
  • 17
  • What doesn't work with the stuff you've linked to? What did you try to fix it/improve it/work around it? Where's the code you used? Which parts of it doesn't work? – Mat Oct 07 '12 at 18:48
  • I will update my question to reflect this, one second. – Mark Oct 07 '12 at 18:52
  • 3
    See this related answer: [How can I convert an RGB image to grayscale but keep one color?](http://stackoverflow.com/a/4064205/97160) (as well as the list of linked questions on the side) – Amro Oct 07 '12 at 19:21
  • Are you just using images or images from a video stream? – Maurits Oct 07 '12 at 19:31
  • potentially both, what difference would it make? – Mark Oct 07 '12 at 20:20
  • @Mark, well, computer vision in the end is finding any feature possible to tie to some meta understanding of the scene. If you have video, you can postulate that the cars on the road move and leverage that. Can you post/link to 2-3 original images you have trouble with? – Maurits Oct 07 '12 at 21:45
  • @Mark, consider posting to dsp.stackexchange.com – Maurits Oct 07 '12 at 21:46
  • I think the time frame ( a few seconds) is prohibitive for any program that might try to leverage information in one shot against another. – Mark Oct 09 '12 at 07:10

1 Answers1

3

So, let me just start off by saying road detection with color profiles is a pathological problem. But if the color of the roads are consistent, and the lighting doesn't change the color of the object you are trying to recognize then you might have a shot. (this will be extremely difficult if this is taken outside, or with different cameras, or if shadows happen, or it taking place in any sort of real-world environment)

Here are a few things that might help.

Try smoothing the image beforehand, the reason you get the bad results in the first images is probably because of small pixel variations in the road. If you can blur them, or use some sort of watershed or local averaging, you might get regions with more consistent color.

You might also consider using the LAB color space instead of HSV or RGB.

Using edge detection (see matlab's canny edge detector) might be able to get you some boundary information. If you are looking for a smooth object, there will not be very many edges in it.

Edit: I tried to adhere to this advice in the most simplistic way. Here are the resulting code and a few samples.

im=rgb2gray(im) %for most basic color capturing.. using another color space is better practice
%imshow(im)
RoadMask=roipoly(im)%create mask 
RoadMask=uint8(RoadMask);%cast to so you can elementwise multiply
im=im.*RoadMask;%apply mask
[x y]=size(im);
for i=1:x
    for j=1:y
        %disp('here')
        if (im(i,j)<160 || im(i,j)>180) %select your values based on your targets range
            im(i,j)=0;                  %replace everything outside of range with 0
            %disp(im(x,y))              %if you'd like to count pixels, turn all values
        end                             %within range to 1 and do a sum at end
    end
end

First converted from RGB to grayscale enter image description here selected a region that generally matched the roads grayness enter image description here Notice parts of the road are not captured and the blocky edges. such as this -------------^

This implementation was quicky and dirty, but I wanted to put it up before I forgot. I'll try to update with code that implements smoothing, sampling, and the LAB color space.

Mark
  • 409
  • 2
  • 6
  • 17
Erotemic
  • 4,806
  • 4
  • 39
  • 80