Code covered by the BSD License  

Highlights from
Simple color detection by hue

4.3 | 15 ratings Rate this file 273 Downloads (last 30 days) File Size: 6.56 KB File ID: #28512
image thumbnail

Simple color detection by hue


Image Analyst


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 ("Blobs Demo") and Simple Color Detection inspired this file.

Required Products Image Processing Toolbox
MATLAB release MATLAB 8.3 (R2014a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (31)
19 Nov 2014 wisam ali

wisam ali

i need this file to be inform with image segmentation

Comment only
01 Nov 2014 Muhammad USman

Muhammad USman

07 Oct 2014 Junyu Li

Junyu Li

20 Jun 2014 Jacques Fourie

Jacques Fourie

12 Feb 2014 XIAOMIN


09 Feb 2014 Shrikant


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

Rishi Pal Singh

01 May 2013 Tarmizi Mohamad

Tarmizi Mohamad

hi bro,
how can I detecting the green region?

04 Oct 2012 Ibrahim Qdemat

Ibrahim Qdemat

04 Oct 2012 Ibrahim Qdemat

Ibrahim Qdemat

18 Aug 2012 Anselm Griffin

Anselm Griffin

18 Aug 2012 Anselm Griffin

Anselm Griffin

22 Jun 2012 Matt Wang

Matt Wang

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

Comment only
25 May 2012 Image Analyst

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.

Comment only
25 May 2012 Image Analyst

Image Analyst

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

Comment only
08 May 2012 cesar pulgarin ortiz

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

mishca nelson

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

14 Jul 2011 Jason


12 Jun 2011 neo


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


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


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


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

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


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

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


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

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


How can I change the color detection to red?

14 Feb 2011 Nasser


a good work..

27 Oct 2014

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

Contact us