I need help with color detection algorithm
Show older comments
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
Jan
on 18 Jul 2011
You forgot to ask a question. How can we assist you in writing your program?
Memo
on 19 Jul 2011
Jan
on 19 Jul 2011
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.
Answers (4)
Jan
on 18 Jul 2011
For a detection of the main color see this trivial method: http://www.mathworks.com/matlabcentral/answers/1360-detect-the-major-color-in-an-image-red-or-blue or http://www.mathworks.cn/matlabcentral/answers/4508-modify-this-code-to-detect-yellow-colour.
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
Memo
on 19 Jul 2011
Walter Roberson
on 19 Jul 2011
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.
Jan
on 21 Jul 2011
@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.
Memo
on 25 Jul 2011
Jan
on 25 Jul 2011
@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.
Memo
on 25 Jul 2011
Jan
on 25 Jul 2011
@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.
Walter Roberson
on 26 Jul 2011
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.
Jan
on 26 Jul 2011
@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.
Walter Roberson
on 26 Jul 2011
I am not a fan of .RAR files either, but the links are not dead.
Memo
on 26 Jul 2011
Sean de Wolski
on 18 Jul 2011
And
doc uigetfile
for step (1).
What if it has green and red? http://3.bp.blogspot.com/_LTx7LfTHrX0/TUnhgAFTbKI/AAAAAAAAA9Y/NWILJ4RwoQg/s1600/arrow.jpg
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
Memo
on 19 Jul 2011
Sean de Wolski
on 19 Jul 2011
I don't work with HSI/HSV.
Abrham Debasu
on 16 Oct 2014
Very helpful for color detection Thanks Abrham Debasu
Walter Roberson
on 19 Jul 2011
2 votes
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
Memo
on 19 Jul 2011
Walter Roberson
on 19 Jul 2011
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.
Memo
on 21 Jul 2011
Sean de Wolski
on 21 Jul 2011
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).
Image Analyst
on 25 Jul 2011
2 votes
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.
Categories
Find more on ROI-Based Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!