File Exchange

image thumbnail


version 1.2 (23.3 KB) by

Detect colored regions in an image via thresholding in HSV color space.



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.

Comments and Ratings (63)


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

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!


Ujjwal (view profile)

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


Awais (view profile)

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

Image Analyst (view profile)

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

Disha Doshi

I am getting the error "too many input arguments"


Moj (view profile)

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

di guo (view profile)

Adam Nustian

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


Image Analyst

Image Analyst (view profile)

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


David (view profile)

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

Image Analyst (view profile)

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

Image Analyst (view profile)

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?


Tejas (view profile)


Image Analyst

Image Analyst (view profile)

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


ivel78 (view profile)

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

Junyu Li



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

hi bro,
how can I detecting the green region?

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

Image Analyst (view profile)

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

Image Analyst (view profile)

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 do you do it?


Jason (view profile)


neo (view profile)

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


Bruno (view profile)

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


anju (view profile)

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 .


neo (view profile)

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

Image Analyst (view profile)

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.


neo (view profile)

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


neo (view profile)

thanx for this good works....but the others colors for exp the yelow and the can i detect thissss...thanx againnnn..

Image Analyst

Image Analyst (view profile)

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.


Alain (view profile)

How can I change the color detection to red?


Nasser (view profile)

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.

MATLAB Release
MATLAB 8.6 (R2015b)

Inspired by: SimpleColorDetection, Image Segmentation Tutorial

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video