View License

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

» Watch video

Highlights from

4.7 | 15 ratings Rate this file 159 Downloads (last 30 days) File Size: 6.61 KB File ID: #26420 Version: 1.3
image thumbnail



Image Analyst (view profile)


20 Jan 2010 (Updated )

Script to do simple color detection in RGB color space. Finds red objects in the image.

| 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 canoe 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 red, green, and blue color bands. I compute thresholds, and ultimately find a mask for the red objects. 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 red objects showing and everything else blacked out. This script can be adapted to find other colors (white, green, blue, yellow, etc.)

If you want to do any measurements of the red objects, I refer you to my blob measurement demo file at

Requires the Image Processing Toolbox. Tested with R2008b and R2010a.


This file inspired Colour Based Segmentation, Simple Color Detection By Hue(), and Extract Red Objects From Aerial Image.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (24)
07 Feb 2017 Aman Dalmia

16 Jan 2017 M Hq

M Hq (view profile)

After the histogram part the code shows an error: Index exceeds matrix dimensions. No matter how big or small file size I choose this error exists. I wonder why?

Comment only
09 Nov 2016 Talha Ahmed

28 Dec 2014 helwan ghgs

very clear

21 Jun 2014 IPlover

Why does it show that you don't have IPT even though i have IPT. I've checked using ver too that i have IPT.

Anyways, after continuing it shows out of memory later. I've tried using matlabpool but it didn't have much effect.

Comment only
02 Jun 2014 Sharon Veerayah

Excellent demo .
tried using it on my images for my spectral analysis

Thanks for the detailed demo

26 Feb 2014 Michala

Michala (view profile)

Great for learning the thresholding and the masking of RGB and grayscale images.

11 Feb 2014 Shrikanth

Sir, in this demo, when i pick my own image some problem occurs.
1. When it computes histogram for blue, axes labels gets mirrored..!!! How to solve this.
(I am using Matlab R2012a)

In this, all three histograms are merged by line in a single subplot. Can we do it with bars also for the same means plotting all three histograms in same subplot with bars of individual color... how..??

Thank you for your help.

Comment only
27 Jan 2014 Vishal Raveendranathan

31 Dec 2013 Onesmo

Onesmo (view profile)

09 Dec 2013 James

James (view profile)

Hi IA,
Thanks for a great demo! I used it with my own test image of some color bars to study the various methods you used. Thanks again.

29 Aug 2012 pavan

pavan (view profile)

20 May 2012 Louis Kok

26 Mar 2012 Gaurav

Gaurav (view profile)

Nice Work Sir!!
Can i know what changes should i do to extract only blue color?

Comment only
02 Mar 2012 mishca nelson

excuse me sir, may i know how do you compute threshold?

Comment only
24 Jan 2012 Indika Kulatunga

Thank you Image Analyst for your help.

05 Nov 2011 Image Analyst

Image Analyst (view profile)

If anyone wants to ask me a question, click my name above, then click the "Contact Author" link.

Comment only
22 Oct 2011 divya

divya (view profile)

i like your work
sir i want to detect two colors blue and red in the same image..i am not able two do that..please tell me how to do that..

Comment only
22 May 2011 German Espinal

Buenas noches

Soy German Espinal, un estudiante de semestre 8 de la Universidad de Cundinamarca.

Actualmente estoy desarrollando un proyecto para la materia de Inteligencia Artificial y me urge un codigo Matlab para terminarlo.

Yo he encontrado muchos buenos videos y material sobre Matlab y creo que gracias a ellos he aprendido mucho.

Basicamente lo que necesito es:

Poner una tarjeta o un carton pintado de un color (puede ser primario o no) frente a la webcam de mi computadora
Capturar esta imagen si es necesario
Decir que color es ese : rojo, amarillo, verde, azul, blanco, negro, etc.

Parece simple verdad ? He buscado mucho, pero no se si debo capturar la imagen, hacerle algun tratamiento, trabajar en RGB o en que codigo de color, como capturar ese codigo y compararlo para poder dar el resultado.

Mil gracias !

Germán Espinal

Comment only
28 Jul 2010 Azy

Azy (view profile)

05 Feb 2010 Ravish

Ravish (view profile)

05 Feb 2010 Husam Aldahiyat

20 Jan 2010 Image Analyst

Image Analyst (view profile)

The Mathworks has a LAB color space classifier demo at
It's also worth looking at.

Comment only
20 Jan 2010 Biplab

Biplab (view profile)

10 May 2010 1.1

Added measurements of area, mean Red value, mean Green value, and mean Blue value for each red blob that was found.

12 Aug 2010 1.2

Expanded screenshot, and fixed small bug that happened when you were using your own image instead of demo images.

19 Aug 2010 1.3

Now allows non-indexed monochrome images (simple gray level images with no stored colormap), and gracefully handles cases where there are no red blobs at all.

Contact us