I need help with color detection algorithm

Hello everyone, I am a complete beginner in Matlab and I need your help. I want to write Matlab program that does the following things:
1-Displays dialog box that asks the user to input image file.
2-Convert the color of the image to HSI
3-Checks if the image has green color or red color. If green color is detected, display the text “Green” and if red color is detected display “red”.
4- The algorithm for detecting the color should be efficient and should detects all possible different shades. I appreciate your help. Thank you,,

3 Comments

You forgot to ask a question. How can we assist you in writing your program?
I need the program code, the commands names or some very simple tutorials that will help write this code.
Is HSI really necessary? There are built-in Matlab functions to convert to HSV color space.
I know this is a little bit nitpicking. But it is always the most efficient method to get an answer, if you aks a real question with a trailing question mark (or leading for the Spanish here). Of course I can reformulate your statements and find the unknowns by my own. But this is a less efficient method to communicate.
However, most likely the native English speakers need less energy for such client-side reformulations.

Sign in to comment.

Answers (4)

I'd prefer a cluster anaylsis if the color distribution is ambiguos.
[EDITED]
rgb = rand(100, 100, 3); % Test data, or e.g. IMREAD
hsv = rgb2hsv(rgb);
red = rgb2hsv([1, 0, 0]);
green = rgb2hsv([0, 1, 0]);
isRed = abs(hsv(:, :, 1) - red(1)) < 0.1;
isGreen = abs(hsv(:, :, 1) - green(1)) < 0.1;
imagContainsGreenObject = sum(isRed(:)) / numel(isRed) > 0.2;
imagContainsRedObject = sum(isRed(:)) / numel(isRed) > 0.2;

11 Comments

Dear Mr.Simon
Do you have any simpler tutorial for detecting red and green colors. These Deoms are like chicness for me. I am really sorry
Supposing uint8 data type:
RedObjectsLabelled = bwlabel(Image(:,:,1) >= 5);
GreenObjectsLabelled = bwlabel(Image(:,:,2) >= 5);
The 5 here is arbitrary, and is chosen to avoid the quantum noise that one often gets in near-black conditions.
Unfortunately, though, a red or green object in an environment with next to no light _continues_ to be a red or green object, and your terms of reference for your problem requires that such objects be found (because you need "all possible shades"). Determining what is noise and what is a real image is difficult.
@Memo: Did you see my edited message? Did it help already? Instead of asking for a "simpler tutorial" I recommend to ask for assistence to understand the already posted ones.
Dear Mr. Simon and Mr De,
I promise that this will be the last question. I took time to study your codes with friends who seem not better than me. Is it correct the way we put it together? I don’t know which variable holds the result.
I uploaded two images (geen0 and red0) to skydrive folder.
My last request is just (please tolerate me and show me two run examples). One for green light you can move and one for red light please stop, because I don’t understand the output of the two codes.
The teacher also refused to help us more after giving us hint code which we did not understand too.
https://skydrive.live.com/redir.aspx?cid=0e3c6d8231449250&resid=E3C6D8231449250!102
I know I am asking a lot but I don’t know someone else to help me.
I am really sorry.
Thank you very much.
>> clear
[baseFileName, folder] = uigetfile('*.*', 'Specify an image file');
fullImageFileName = fullfile(folder, baseFileName);
[rgbImage storedColorMap]= imread(fullImageFileName);
rgb = rgb2hsv(rgbImage);
hsv = rgb2hsv(rgb);
red = rgb2hsv([1, 0, 0]);
green = rgb2hsv([0, 1, 0]);
isRed = abs(hsv(:, :, 1) - red(1)) < 0.1;
isGreen = abs(hsv(:, :, 1) - green(1)) < 0.1;
imagContainsGreenObject = sum(isRed(:)) / numel(isRed) > 0.2;
imagContainsRedObject = sum(isRed(:)) / numel(isRed) > 0.2;
>> imagContainsGreenObject
imagContainsGreenObject =
0
>> imagContainsRedObject
imagContainsRedObject =
0
--------------------------------------------------------------------------
>> clear
[baseFileName, folder] = uigetfile('*.*', 'Specify an image file');
fullImageFileName = fullfile(folder, baseFileName);
[rgbImage storedColorMap]= imread(fullImageFileName);
I = rgb2hsv(rgbImage);
rg = {'red','green'};
idx= [true false];
idx = false(1,2);
idx(1) = any(any(all(bsxfun(@gt,I(:,:,1),I(:,:,2:3)),3))); %any red greater than both blue and green?
idx(2) = any(any(all(bsxfun(@gt,I(:,:,2),I(:,:,[1 3])),3)));
fprintf('Colors Detected: %s\n',rg{idx})
Colors Detected: red
Colors Detected: green
@Memo: The link you've posted is dead. It is not getting clear to me, what you are searching for. "Check if the image has green color or red color" is not an unequivocal description of a problem: Are you looking for *any* green pixel, or if there are *more* green then red pixels, or if the number of almost green pixels is *higher* than the number of almost red pixels, or if the *average* color is more red than green? Because we do not get an exact description, our program examples are just some general tips. E.g. you have to adjust the values 0.1 and 0.2 to your problem.
Mr. Simon I reposted the link :
https://skydrive.live.com/redir.aspx?cid=0e3c6d8231449250&resid=E3C6D8231449250!102
and here is another one:
http://www.2shared.com/file/iQGWVKNl/Traffic.html
The program is automatic judgment method of traffic light for pedestrians.
It reads a traffic light image. If the traffic is green it displays the text (you can walk) if the traffic is red , it displays (please stop). This is the description of the problem. I want the code with sample run for the two test cases. I tried hard to understand the 10 pages code you advised me read in the tutorials but I just can’t . It is above my level.
@Memo: The posted links are dead: "This page cannot be displayed...".
I've posted a very simple approach with only 7 lines of code. It seems, like this is still to complex to be used. Image processing is not a trivial task. I think you find all necessary information in this thread already, but you will need more time to get into the details. Good luck.
The skydrive link requires that you have enabled javascript from several sites before it will present the page that allows you to get at the file.
The 2shared link doesn't require you to enable javascript for as many sites.
@Walter: On 2shared I find a packed RAR file and this text: "What is 2shared? Free music collection. Here you can enjoy music compositions absolutely free." While I obviously can bear to suffer from this ridiculous keyboard latency, I do not see a reason to follow a link, download a RAR file, scan it for virusses, unpack it, display the picture, delete the picture and delete the RAR file just to answer a question. Others may be more enthusiastic than me.
It would be much more convenient, if pictures can be stored on a TMW server such that they are embedded directly in the message.
I am not a fan of .RAR files either, but the links are not dead.
You are right Mr. Simon. I am sorry for making you suffer to answer my question.
I uploaded the files again and here are direct links:
The green traffic image:
http://postimage.org/image/1cdeo4mkk/
The red traffic image:
http://postimage.org/image/1cdrwfeis/
The assignment:
http://www.mediafire.com/file/s3e96sy21bmf9ab/Assignment.docx
and this is the hint code the teacher provided:
http://www.mediafire.com/file/hatjjonvqw1mltt/Hint_Code.txt
I hope they are working.
I am very sorry again

Sign in to comment.

And
doc uigetfile
for step (1).
More
for step 3:
rg = {'red','green'};
fprintf('Colors Detected: %s\n',rg{idx})
Where idx is a 1x2 logical vector or colors present. There! I've done half of your homework assignment.
Okay, I'll do step 4 too:
I = your_image;
idx = false(1,2); %see idx from above - they're the same
idx(1) = any(any(all(bsxfun(@gt,I(:,:,1),I(:,:,2:3)),3))); %any red greater than both blue and green?
idx(2) = any(any(all(bsxfun(@gt,I(:,:,2),I(:,:,[1 3])),3)));
This will detect any case of there being a pixel color of interest greater than the other two channels.

3 Comments

Thank you for helping with the first step. What about the second one, how can i convert the image to HSI?
And if the two colors are detected , the program should display “green and red colors are detected”
Once again , thank you.
I don't work with HSI/HSV.
Very helpful for color detection Thanks Abrham Debasu

Sign in to comment.

See http://www.mathworks.com/matlabcentral/fileexchange/13630 to convert the image to HSI. There is no built-in MATLAB conversion to HSI, just to HSV (which is often suitable for this kind of task.)
Detecting "all possible shades" of red or green is not a scientific question, but rather a matter of how you want to define "red" or "green" or shades of those. There is no particular boundary between shades of red and shades of green, except in the human perceptual system (which is hard-wired in such a way that it cannot detect red and green simultaneously in any one cone.)

4 Comments

Thank you for the information Mr. Roberson.
I managed to acquire an image into Matlab and store it. I asked about your note and the teacher said I’m allowed to use HSV color so I converted the image to HSV. What about a simple tutorial for color detection or simple code. My test case image would be like the one Mr. Sean de posted.
Look in the FEX for ImageAnalyst's tutorials on simple color detection.
But that's simple color detection, which is not what you asked for earlier: earlier you said it was necessary to determine "all possible shades" of red or green. Determining what is a shade of red and not a shade of green is a subject that has given many a scholar headaches throughout history.
John d'Errico had a fuzzy color detection contribution to the FEX; he spent months building it: he is a retired scientist specializing in color science, and to the best of his very considered scientific knowledge, a "fuzzy" determination of color shades is the most that is achievable.
Sorry Mr. Roberson. I understand your tone .I said it is necessary to detect the shades because this is what the teacher said to one of the students in the class. Her Matlab program detected green color in two images and failed to detect green in a third image. The teacher told her not to restrict the value of green to allow detection of more possible shades.
I posted the teacher’s question to prevent more misunderstanding:
https://skydrive.live.com/redir.aspx?cid=0e3c6d8231449250&resid=E3C6D8231449250!102
I apologize to all of you. I said I am a beginner . I started learning Matlab this week :’(
Thank you
Did you see my code above? It'll detect any time there is more green than anything else (most discretizable shades, as far as you're concerned).

Sign in to comment.

Check out my color segmentation tutorials: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862. You should be able to get all your "greens" by adjusting the delta E in my Delta E color demo.

Asked:

on 18 Jul 2011

Commented:

on 16 Oct 2014

Community Treasure Hunt

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

Start Hunting!