File Exchange

image thumbnail


version (20.5 KB) by Image Analyst
Script to do simple color detection in RGB color space. Finds red objects in the image.


Updated 17 Feb 2018

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

Cite As

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

Comments and Ratings (52)

Meriem Bahoussi

Joel Silva



Can you please modify this script for me to find just the green color

Image Analyst

If anyone has any questions about this, please ask in the MATLAB Central Answers forum.

Michael Heneghan

stelios mappouras

Lia Marchi

Oguzhan Cevher

Intan Suraya


How to modify it to detect White Colors?

Joel Borja

What do we put into the Area argument?

Fizza Zahid

siva bala singh k

ShaikS Shahid

Can i know how to segment the conjuctival pallor ( the red portion below sclera ) from an Image.I'm struck here.I applied the above function.Images in which conjuctival pallor is red, it is working fine.But if the portion is slightly red it gets failed.As every eye does not have high redness below it.If there is a way to segment only pallor portion independent of color red , please help me.

Khine zar

Thank you ...............It is helpful

Nicholas Li


Image Analyst

Andres, for many or most color segmentation tasks, the segmentation is best done in another color space, like HSV. This would probably allow you to threshold pink like a hand better than if you were to do it in RGB color space. I do have a demo where I segment images in HSV color space, so look for that.

Ashwin Krishnan

This is amazing! thank you!

Andres Mendez


I wanted to ask you how straightforward it is to adapt for green and blue. I do not know much about this but I like soling problems. However, it does not look so simple to convert it (at least at a first glance).

I also wanted to ask you how to clean images where other objects get attached to the red one. For example, a hand (due to the pink color) gets selected as part of the red object. The red object is very red and homogeneous.


Krishanu Modak

Architha L S

Can I know what changes should I do in this code to extract dark colours(black shades)


Image Analyst

Kaleb and others, I've uploaded a version that fixes the incompatibilities with the newer MATLAB releases.

tibor koroknai

Kaleb Jackson

I'm having the same issue as M Hq and am wondering if there was a solution? I've been looking but i cant see what index could exceed the dimensions or what matrices could be in question. Any help would be great.

Tasneem Al-Tmimi

It was helpful for me thank you

Chen Hong-Yu

Aman Dalmia

M Hq

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?

Talha Ahmed

helwan ghgs

very clear


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.

Sharon Veerayah

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

Thanks for the detailed demo


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


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.



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.


Louis Kok


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

mishca nelson

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

Indika Kulatunga

Thank you Image Analyst for your help.

Image Analyst

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


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

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



Husam Aldahiyat

Image Analyst

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


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

Community Treasure Hunt

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

Start Hunting!