MATLAB Answers

How to do color detection without using Image Processing Toolbox ?

36 views (last 30 days)
Kye
Kye on 13 May 2014
Answered: Image Analyst on 13 May 2014
I'm a newbie in using MATLAB
May I know how can I do the color detection without without using Image Processing Toolbox ??
(eg. detect red color in an image )
I have no idea of how to write the algorithm.
Or is there any link/tutorial available ??
Hope someone can help, thanks !!

Accepted Answer

Benjamin Avants
Benjamin Avants on 13 May 2014
How you find the color depends somewhat on what format your image data is in. If it is a color image, it is likely to be a W x H x 3 matrix where W and H are width and height in pixels and the third dimension is RGB. Depending on the image format, there may also be alpha data or the colors may be something like CMYK.
The data may also be of different types. These are usually double, uint8, or uint16. In the case of the type double, the color intensity for a given pixel will be expressed as a number between 0 (black) to 1 (full intensity of whichever color you're looking at). If the data type is uint 8 or 16, the color intensities will be integers ranging from 0 to 255 or 65535 (for 8 and 16 respectively).
Once you know the data type and color format, you need to identify how the color you're looking for is expressed.
Ex. You are working with an RGB image in uint8 format and want to identify areas that are fully red with no other color. The color you're looking for is [255,0,0].
One way to identify these pixels is to do the following:
% Test each color for pixels with the values you're looking for
R = img(:,:,1) == 255;
G = img(:,:,2) == 0;
B = img(:,:,3) == 0;
% AND together the logical arrays to find pixels with the appropriate value
RedPixels = R & G & B;
% RedPixels is now a logical array of size W x H x 1 where pixels that are fully
% red are TRUE and all others are FALSE.
This can be modified to search for ranges of a color or to ignore particular colors:
R = img(:,:,1) > 235;
B = img(:,:,3) < 25;
will find pixels that are close to fully red and only contain a little blue but may have any value of green.

More Answers (1)

Image Analyst
Image Analyst on 13 May 2014
Some color segmentation can be done without the Image Processing Toolbox. See my demos in my File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 Just replace imshow() with image(), and you won't be able to calculate areas or things like that. For conversion to other color spaces, instead of using functions like rgb2hsv() you'll have to do it yourself with formulas from places like EasyRGB

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by