4.2 | 11 ratings Rate this file 108 Downloads (last 30 days) File Size: 2.6 KB File ID: #40154 Version: 1.4
image thumbnail

How to Detect and Track Red, Green and Blue Colored Object in LIVE Video


Arindam Bose (view profile)


04 Feb 2013 (Updated )

This code can detect and track Red, Green and Blue colored objects in LIVE video.

| Watch this File

File Information

This code can detect and track Red, Green and Blue colored objects in LIVE video. In this code objects can be segmented using color detection and segmentation.
For detail documentation: http://arindambose.com/blog/?p=82

For video tutorial:


This file inspired Detecting Yellow Color Using Thresholding.

Required Products Image Acquisition Toolbox
Computer Vision System Toolbox
MATLAB release MATLAB 7.14 (R2012a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (27)
10 Jul 2015 Abu Shuvom  
21 Feb 2015 ABHISHEK

How can I connect this code to UART for serial communication to AVR..?
Can i follow using robot the path of red green & blue color object by using this interface..??

19 Feb 2015 Atif Anwer

The line "step(hshapeinsBox, rgbFrame, bboxRed, single([1 0 0])); % Instert the red box" wasnt working for me for some reason. However i do recommend an alternate that worked which did work fine for me :

insertShape(rgbFrame, 'Rectangle', bboxRed, 'Color', 'red'); % Insert the red box

23 Dec 2014 Arindam Bose

Arindam Bose (view profile)

@Nathan: Please change the threshold accordingly. It depends on the ambient color

Comment only
28 Sep 2014 Nathan

Nathan (view profile)

Sometimes, Matlab detects blue as green color. :( please help

02 Sep 2014 koushik  
20 Mar 2014 chaitanya

Excellent work sir, i saw your video as well but when i am running code i am getting error as :

Undefined variable "imaq" or class "imaq.VideoDevice".

Error in RGB_Detection_and_Tracking (line 14)
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ... % Acquire input video stream

can you please help
thanks in advance

27 Dec 2013 daher

daher (view profile)

hi, how to tag and track the first identified colored object, for example, in a video when other objects will appear with the same color without referring to geometry

Comment only
28 Nov 2013 Verena

Verena (view profile)

Hello, thanks you so much!

@CHANDAN Nagaraja did you find any solution for your problem? I have the same problem ;)

Comment only
14 Nov 2013 CHANDAN Nagaraja

Thank you so much.
I was wondering can we store all the X,Y vales of red object as output in MATlab code? Do you know code for it?

Comment only
12 Nov 2013 Loai

Loai (view profile)

hello, first I wanted to thank u and i wanted to ask what should i do if i want it to follow a combination of RGB and return its area

Comment only
05 Oct 2013 Muhmmmad usman

Hello dear! I am getting error during running code. please help me with this. Below i have written down that error. your help will be highly appreciated. Thanks
??? Undefined variable "imaq" or class "imaq.VideoDevice".

Comment only
07 Sep 2013 Renato Henz

Hello everyone! I'm getting this error while trying to run the program:

??? Undefined variable "imaq" or class "imaq.VideoDevice".

Error in ==> RGB_Detection_and_Tracking at 14
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ... % Acquire input video

Does anyone know what might be happening, or how can I fix it?
Thanks a lot!

Comment only
14 Jul 2013 ADNAN

ADNAN (view profile)

gud job

30 May 2013 Elizabeth

Hi, I was wondering if it would be possible to change the input from LIVE video to recorded video?

Comment only
11 Mar 2013 swapna chougule

@Arindam Bose thank you

Comment only
11 Mar 2013 Arindam Bose

Arindam Bose (view profile)

@swapna chougule: The number of red pixels are given in terms of area of corresponding blob. Look into area property of each blob.

Comment only
08 Mar 2013 swapna chougule

how to get number of pixels of red color using in this code??

Comment only
24 Feb 2013 mohammed ghazal

ok thnx bro

Comment only
23 Feb 2013 Arindam Bose

Arindam Bose (view profile)

@mohammed ghazal: Red, Green and Blue are the basic colors. The simple code segment which subtracts the intensity image from the corresponding layer, can be used only for detecting these three basic colors. But if you need to detect more colors you have to convert the image into HSV color map and prepare different threshold filters for different colors in Hue layer.

Comment only
23 Feb 2013 mohammed ghazal

thnx dear my copy of matlab2012b don't have computer vision toolbox installed,so i installed the old version2012a...and it is worked ok...thnx alot
tell me pls how to determine color Threshold ... what is the rule ? i want
to track more colors
redThresh = 0.24; % Threshold for red detection
greenThresh = 0.05; % Threshold for green detection
blueThresh = 0.15; % Threshold for blue detection

22 Feb 2013 Arindam Bose

Arindam Bose (view profile)

@mohammed ghazal The problem is that you don't have computer vision toolbox installed in your MATLAB version. This special toolbox comes with newer
version of MATLAB e.g. R2012a onwards. The Blob analysis handles does the statistical analysis of detected blobs in a logical image. However you can use 'regionprops' command instead of vision.blobAnalysis. It does almost same job. Dig into the help for more information about it.
Anyway you can collect the newer version of MATLAB. MATLAB R2013a has already been released. Always stay updated to experience more.

Comment only
22 Feb 2013 mohammed ghazal

how to solve this error pls?

Undefined variable "vision" or class "vision.BlobAnalysis".

Error in RGB_Detection_and_Tracking (line 19)
hblob = vision.BlobAnalysis('AreaOutputPort', false, ... % Set blob
analysis handling

13 Feb 2013 Arindam Bose

Arindam Bose (view profile)

@Masoud : Because, I used a while loop in the middle of code and set the loop counter to around 300 frames. So theoretically it will run for 10 seconds(30fps). You can change the loop counter to several thousand. I did not make the loop counter Inf because it will cause a little inconvenience. In that case you have to release the camera handle individually by running this code piece "release(vidDevice)".
So its better to have a loop counter. You can change it to several thousands to run it for several minutes. I made it smaller for demonstration.

Comment only
12 Feb 2013 Masoud Ghanbari

Why this program will close after several seconds automatically???

11 Feb 2013 Fiem

Fiem (view profile)

Thanks a lot

Comment only
11 Feb 2013 Fiem

Fiem (view profile)

04 Feb 2013 1.1

Description updated

19 Feb 2013 1.3

1. Memory management enhanced
2. Description changed

21 Feb 2014 1.4

Description Changed

Contact us