Matlab color detection

10,913

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.

Share:
10,913
Mark
Author by

Mark

Updated on June 04, 2022

Comments

  • Mark
    Mark almost 2 years

    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.