we are doing a mat lab based robotics project.which actually sorts objects based on its color so we need an algorithm to detect specific color from the image captured from a camera using mat lab. it will be a great help if some one can help me with it.its the video of the project
-
related question: [How can I convert an RGB image to grayscale but keep one color?](http://stackoverflow.com/questions/4063965/how-can-i-convert-an-rgb-image-to-grayscale-but-keep-one-color) – Amro Sep 30 '11 at 16:03
4 Answers
In response to Amro's answer:
The five squares above all have the same Hue value in HSV space. Selecting by Hue is helpful, but you'll want to impose some constraints on Saturation and value as well.
HSV allows you to describe color in a more human-meaningful way, but you still need to look at all three values.

- 11,111
- 2
- 40
- 77
-
one way to think about those is that they represent somewhat the same color (purple) under different lighting conditions. Therefore hue can be used to detect different shades of the same color, which is often helpful in computer vision tasks: skin detection, object tracking, etc.. – Amro Sep 30 '11 at 16:11
-
I agree that hue is the best starting point. But your statement does not apply to the edge cases. Noise and systematic errors in the imaging system produce false positives. For example, a grey object is going to have *some* hue value, but it will generally be a meaningless value caused by noise. Same with dark shadows. Highlights reflect the color of the light source. And constant hue values are useless if the camera isn't white balanced correctly. A robust algorithm should do some extra steps. – japreiss Sep 30 '11 at 16:42
As a starting point, I would use the rgb space and the euclidian norm to detect if a pixel has a given color. Typically, you have 3 values for a pixel: [red green blue]
. You also have also 3 values defining a target color: [255 0 0]
for red. Compute the euclidian norm between those two vectors, and apply a decision threshold to classify the color of your pixel.
Eventually, you want to get rid of the luminance factor (i.e is it a bright red or a dark red?). You can switch to HSV space and use the same norm on the H value. Or you can use [red/green blue/green]
vectors. Before that, apply a low pass filter to the images because divisions (also present in the hsv2rgb transform) tend to increase noise.

- 10,378
- 7
- 39
- 55
You probably want to convert to the HSV colorspace, and detect colors based on the Hue values. MATLAB offers the RGB2HSV function.
Here is an example submission on File Exchange that illustrate color detection based on hue.

- 123,847
- 25
- 243
- 454
For obtaining a single color mask, first of all convert the rgb image gray using rgb2gray. Also extract the desired color plane from the rgb image ,(eg for obtaining red plain give rgb_img(:,:,1)). Subtract the given plane from the gray image........

- 1