This script is a demo that shows you how to find objects of a certain color in an image. For this example I'm using the standard onion, peppers, or kids image that ships with the Image Processing Toolbox. You can also select one of your own color images instead if you wish. Then I separate the image into its component hue, saturation, and value color bands. I ask you what color you want to find (red, green, yellow, white). I compute thresholds, and ultimately find a mask for the regions of the color you specified. Prompts will guide you through the demo step by step. I multiply this mask by the original image to show the image with only the requested color showing and everything else blacked out.
If you want to do any measurements of the objects, I refer you to my blob measurement demo file at http://www.mathworks.com/matlabcentral/fileexchange/25157
Requires the Image Processing Toolbox. Tested with MATLAB release R2014a.
Image Analyst (2020). SimpleColorDetectionByHue() (https://www.mathworks.com/matlabcentral/fileexchange/28512-simplecolordetectionbyhue), MATLAB Central File Exchange. Retrieved .
what are the algorithms are used in this demo
Thank you so much for uploading this, very helpful.
dear image analyst .. i need a part of code that calculate area and diameter of leaf .. can u help me as i didnt find suck kind of reltd data source
dear image analyst thank you for very nice code.
i used this code successfully to detect leaf from background. but i want to add some more in this, i want to remove petiole from segmented image.
please provide code for that.
Oshinowo, ask your question in the Answers forum, not here.
Please I need a code to detect nitrogen deficiency in crop leaves
Chloe, you change the thresholds. Note that this code was written before the "Color Thresholder" app on the Apps tab of the tool ribbon was written. That app lets you determine thresholds visually and interactively.
Hi, how do i detect colours which arn't red green or blue? I need to detect a brown colour scale from an image can anybody help me?
i have to check red color pixel but due to shadow or light effects shades of red color are there in image but i have to consider that one also...but it showing different pixel value due shades of red color ...how i can use hue parameter for this....
Hi Image Analyst,
Congratulations for the amazing work.
I have a doubt.. Is there any way to plot the HSV wrt to wavelength?
Thank you, this code really helped me a lot ! Keep up the good work!
hi Image Analyst, I am seeking guidance for some image processing task. Would you be willing to help me with your valuable advice? Is there a way to reach you?
Hello! I have tried your code, but it is leaving out parts of the red object that I am trying to detect. Do you think I should modify the mask?
Hi Image Analyst,
For what reason would I want to perform color detection in HSV color space as opposed to RGB?
Thank you very much for a nice explanation.
Can you please elaborate how to acquire required colored portion while making the remaining background as white instead of black
Dear Image Analyst,
Thanks for the very nice work.
Please update me how I can detect two colors by applying thresholds simultaneously . I am interested in Green and Red.
Moj, ask your question in the Answers forum, not here. Disha, you must have changed something. Post your code in the Answers forum.
I am getting the error "too many input arguments"
Dear ImageAnalyst , I am going to do some analysis on an image and i need to write a code to recognize the coordinates of different points on red color automatically without human interfering. I will be grateful if you propose some methods. Thanks in advance.
Are you available to review a paper on Log. Image Processing for an international conference?
Jack, I've just downloaded and tried the code and it works flawlessly. You must have modified it somehow. Please post your code in the Answers forum so we can help.
Mohammad, you can use the Color Thresholder App on the APPS tab to get an idea of the thresholds you can start with.
doesnt work, code for choosing which color to find has problems
Hi bro, I have a question:
How do you decide the values of threshold? I need detect skin in webcam's image but i dont know how choose value of these.
PD: sorry for my bad english, i'm beginner
Dear Mr Image Analyst,
How do I know (technique) to set the initial low and high threshold for HSV in order to detect the pigment spots colour on iris surface? As example shows in http://d3hu9binmobce5.cloudfront.net/content/royopensci/3/1/150424/F4.large.jpg (pointed by the arrow).
TQ in advanced.
Anyone can contact me through the Answers forum, not by email.
How do I can contact you? Can you give me your email address?
charuleelaa, post your image and code to the Answers forum so I can fix it. Look for the line that says "button = menu('What color do ......". When it hits that line it should bring up the question. Perhaps some other window is covering it up, so look in the taskbar.
its not working for an input image of my own. It does not show me the option of choosing the color i want. whats the problem?
Venkatesh, sorry but this demo is not intended to give advice on Arduino programming. Ning Wu, it allows you to pick from a limited list of several colors. If you want others, you have to adjust the threshold. Kinjal, try rgb2ind() to quantize the colors into a predefined, limited number of classes.
your code is detecting any colour by placing proper clour intensities. its awasome. but i want some further information that is if i detect red colour i want to send an output '5' to aurdino. if i detect a green colour i want an output '6' to aurdino. how can i send this outputs to aurdinio. i installed aurdino support package and drivers but i dont know send information when i detected a particular colour
Nice, really helpful.
Can someone tell how to detect other colors such as red, white, blue with this. Specifically, the demo or the Matlab code does not show which parameters to be changed, and how to explicitly do this.
can we apply KNN algorithm on this?
i need this file to be inform with image segmentation
I m trying with this. But, im getting error. While getting histogram for Blue, the captions gets reversed just like mirrored image.
How to get rid of it...???
Hi, just want to ask im a newbie and I find matlab really amazing.
Im planning to do a color measurement device using matlab script. How am I going to start? Or, what should I first consider? Thanks!!!
how can I detecting the green region?
Good job. Thx 4 sharing! But I'm wondering how to change the color as I want... Is there any way to compute it? For example, how about the red one?
mischa: why the low rating? You can see how I do it simply by looking at my code. If you don't want fixed, hard coded values, then use my thresholding app to interactively select them.
cesar: You could use my interactive thresholding application (also in my File Exchange)
Hi, how can i compute the hi and low hsv thresholds for any color? i was trying with paint but i couldn't determine those numbers
I compute thresholds, and ultimately find a mask for the yellow objects.how do you do it?
hi....for bruno....the green hue thresholdlow=0.22..........
of course this values from my opinion...if you want to find yours:
1-go to paint.
2-select any color double clicks.
3-define customized colors.
you will find 2 systems-RGB-HSV...u will select your own threshold.......but i have always the same problem with the light intensity....good luck
Thanks for your work. How you know what value to put in "Assign the low and high thresholds for each color band"? I want to detect Green. How can i know what values i have to use? Thanks
Would you be able to help on me on the matlab coding for the object tracking of the using Hue value ? As i am new to matlab i couldnt find much details on it .
My object is to perform color calibration in order to define the upper and lower limit of the HUE value for to detect the color at any light intensity .
than you so much for answer me....i want to thank you very much for this good work....but i want to know the algorithgm that you use....thanx
neo: You might want to look at my "Delta E" color detection submission. This lets you define (by outlining it) the color that you want to find.
hi guys i want to know...the diffrent steps of this program or the algorytm....thanx guysssss....n_n
Hi guys !!! I need to capture a primary color card with my webcam, then I need to determine what color is it ??? Can someone of you Help me please with the MatLab code ???? TKS A lot !!!
thanx for this good works....but the others colors for exp the yelow and the magenta....how can i detect thissss...thanx againnnn..
Alain: You have to change the thresholds in the section that says "Assign the low and high thresholds for each color band." Red would have hue values less than about 0.1 and greater than about 0.9.
How can I change the color detection to red?
a good work..
Updated code to use line() instead of stem(), because MATLAB R2015b release did not like how I had used stem(), even though old releases were happy with it.
Updated to handle bug in bar() introduced between R2010a and R2014b. Also added ability to choose color to find.