File Exchange

image thumbnail


version (23.3 KB) by Image Analyst
Detect colored regions in an image via thresholding in HSV color space.


Updated 19 Dec 2015

View Version History

View License

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
Requires the Image Processing Toolbox. Tested with MATLAB release R2014a.

Cite As

Image Analyst (2021). SimpleColorDetectionByHue() (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (82)

harris harris

very nice!

stelios mappouras

Lia Marchi

Hasanien Kuba

Mareesh waran

what are the algorithms are used in this demo


James Allen

John Cashin

Thank you so much for uploading this, very helpful.

Farnaz Gh

Charulata Raut

hajira ashiq

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

ashok janwale

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.


Image Analyst

Oshinowo, ask your question in the Answers forum, not here.

Oshinowo Abayomi

Please I need a code to detect nitrogen deficiency in crop leaves

Image Analyst

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.

Ahmed Grera

Ahmed Grera

Chloe Hind

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?


ashwini gat

hello sir,
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 i can use hue parameter for this....

Mohammed Hasan

Sri Widya


Hi Image Analyst,
Congratulations for the amazing work.
I have a doubt.. Is there any way to plot the HSV wrt to wavelength?

Kent Yeap

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?

Trishia Chemaly

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?

Ashton Krajnovich

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.

Image Analyst

Moj, ask your question in the Answers forum, not here. Disha, you must have changed something. Post your code in the Answers forum.

Nikita Waghwani

Disha Doshi

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.

di guo

Adam Nustian

Are you available to review a paper on Log. Image Processing for an international conference?


Image Analyst

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.

Jack Gilmore

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.
Thank you!
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 (pointed by the arrow).

TQ in advanced.

Image Analyst

Anyone can contact me through the Answers forum, not by email.

Hi Sir,
How do I can contact you? Can you give me your email address?

Image Analyst

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?



Image Analyst

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.

Venkatesh A

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.

Ning Wu

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.

kinjal shah

can we apply KNN algorithm on this?
KNN classification

wisam ali

i need this file to be inform with image segmentation

Muhammad USman

Junyu Li

Jacques Fourie



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!!!

Rishi Pal Singh

Tarmizi Mohamad

hi bro,
how can I detecting the green region?

Ibrahim Qdemat

Ibrahim Qdemat

Anselm Griffin

Anselm Griffin

Matt Wang

Matt Wang

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?

Image Analyst

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.

Image Analyst

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

mishca nelson

I compute thresholds, and ultimately find a mask for the yellow 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

Image Analyst

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

German Espinal

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 can i detect thissss...thanx againnnn..

Image Analyst

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..

MATLAB Release Compatibility
Created with R2015b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: Image Segmentation Tutorial, SimpleColorDetection()

Community Treasure Hunt

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

Start Hunting!