View License

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

» Watch video

Highlights from

4.5 | 25 ratings Rate this file 391 Downloads (last 30 days) File Size: 23.3 KB File ID: #28512 Version: 1.2
image thumbnail



Image Analyst (view profile)


19 Aug 2010 (Updated )

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

| Watch this File

File Information

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.


Image Segmentation Tutorial and Simple Color Detection inspired this file.

Required Products Image Processing Toolbox
MATLAB release MATLAB 8.6 (R2015b)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (59)
04 Mar 2017 Vaishnav1 Sreekrishnan

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

Comment only
04 Mar 2017 Vaishnav1 Sreekrishnan

28 Feb 2017 Kent Yeap

Thank you, this code really helped me a lot ! Keep up the good work!

27 Feb 2017 Ujjwal

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?

15 Feb 2017 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?

Comment only
11 Oct 2016 Ashton Krajnovich

Hi Image Analyst,

For what reason would I want to perform color detection in HSV color space as opposed to RGB?


Comment only
07 Oct 2016 surya prasad potnuru

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

Comment only
06 Oct 2016 Awais

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.

Comment only
29 Sep 2016 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.

Comment only
29 Sep 2016 Nikita Waghwani

19 Sep 2016 Disha Doshi

I am getting the error "too many input arguments"

Comment only
25 Aug 2016 Moj

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.

Comment only
08 Aug 2016 di guo

di guo (view profile)

18 Jun 2016 Adam Nustian

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


Comment only
09 Jun 2016 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.

Comment only
08 Jun 2016 Jack Gilmore

doesnt work, code for choosing which color to find has problems

Comment only
05 May 2016 David

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

03 Apr 2016 Mohamad Faizal Ab Jabal

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.

Comment only
01 Apr 2016 Image Analyst

Image Analyst (view profile)

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

Comment only
27 Mar 2016 Mohamad Faizal Ab Jabal

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

02 Feb 2016 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.

Comment only
02 Feb 2016 charuleelaa vanilavarasu

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?

Comment only
25 Jan 2016 Tejas

Tejas (view profile)


Comment only
20 Dec 2015 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.

Comment only
19 Dec 2015 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

31 Oct 2015 ivel78

ivel78 (view profile)

Nice, really helpful.

13 Aug 2015 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.

Comment only
20 Feb 2015 kinjal shah

can we apply KNN algorithm on this?
KNN classification

19 Nov 2014 wisam ali

i need this file to be inform with image segmentation

Comment only
01 Nov 2014 Muhammad USman

07 Oct 2014 Junyu Li

20 Jun 2014 Jacques Fourie

12 Feb 2014 XIAOMIN

09 Feb 2014 Shrikanth

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

Comment only
02 Dec 2013 Vanessa

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

Comment only
10 Nov 2013 Rishi Pal Singh

01 May 2013 Tarmizi Mohamad

hi bro,
how can I detecting the green region?

04 Oct 2012 Ibrahim Qdemat

04 Oct 2012 Ibrahim Qdemat

18 Aug 2012 Anselm Griffin

18 Aug 2012 Anselm Griffin

22 Jun 2012 Matt Wang

22 Jun 2012 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?

Comment only
25 May 2012 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.

Comment only
25 May 2012 Image Analyst

Image Analyst (view profile)

cesar: You could use my interactive thresholding application (also in my File Exchange)

Comment only
08 May 2012 cesar pulgarin ortiz

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

Comment only
02 Mar 2012 mishca nelson

I compute thresholds, and ultimately find a mask for the yellow do you do it?

14 Jul 2011 Jason

Jason (view profile)

12 Jun 2011 neo

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

Comment only
09 Jun 2011 Bruno

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

Comment only
09 Jun 2011 anju

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 .

Comment only
06 Jun 2011 neo

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

Comment only
05 Jun 2011 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.

Comment only
31 May 2011 neo

neo (view profile)

hi guys i want to know...the diffrent steps of this program or the algorytm....thanx guysssss....n_n

Comment only
22 May 2011 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 !!!

Comment only
11 May 2011 neo

neo (view profile)

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

Comment only
22 Apr 2011 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.

Comment only
10 Apr 2011 Alain

Alain (view profile)

How can I change the color detection to red?

14 Feb 2011 Nasser

Nasser (view profile)

a good work..

27 Oct 2014 1.1

Updated to handle bug in bar() introduced between R2010a and R2014b. Also added ability to choose color to find.

19 Dec 2015 1.2

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.

Contact us