View License

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

» Watch video

Highlights from
Tracking red color objects using matlab

4.6 | 80 ratings Rate this file 304 Downloads (last 30 days) File Size: 2.27 KB File ID: #28757 Version: 1.1
image thumbnail

Tracking red color objects using matlab


A. Bhargav Anand (view profile)

  • 1 file
  • 4.63514


18 Sep 2010 (Updated )

This program just tracks all red color objects and draws a bounding box around them.

| Watch this File

File Information

This program just tracks all red color objects and draws a bounding box around them. This works on the difference between frames concept. Every frame in the video is returned as an rgb image on which we can do image processing stuff.


This file inspired Video Stream Surf and Virtual Painting.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (116)
16 Feb 2017 kuldip patel

thanks it is work properly

07 Feb 2017 David Pereira

Error in redObjectTrack (line 20)
data= getsnapshot(vid);

any ideas plz?

Comment only
07 Feb 2017 José Pereira

I'm currently using matlab 2016. However, this code keeps showing up:
Error in redObjectTrack (line 7)
vid = videoinput('winvideo',1);

I have all the adaptors. Does anyone have a hint? thank you

Comment only
16 Jan 2017 cherukuwada suneetha

05 Jan 2017 Dai Wei-Zhu

How can I catch else color object(modify where)?

Comment only
06 Dec 2016 Somayah Alkandery

19 Nov 2016 yaser ku

pleas can send me the slide of this program

Comment only
19 Nov 2016 MD Rijwan

Thanks a lot Brother...I was searching for it from past many days..

Comment only
04 Nov 2016 Job

Job (view profile)

% Error in getCameraInfo (line 4)

% What worked for me is to change this code:
% camera_id = camera_info.DeviceInfo.DeviceID(end);
% resolution = char(camera_info.DeviceInfo.SupportedFormats(end));

camera_id = camera_info.DeviceInfo.DeviceID;
resolution = camera_info.DeviceInfo.DefaultFormat;

Comment only
24 Oct 2016 Divyanshu Bhati

16 Oct 2016 ahmed aghmadi

please i try this program but i have an error. it's :
??? Error using ==> imaqdevice.start at 91
Multiple VIDEOINPUT objects cannot access the same device simultaneously.

Error in ==> redObjectTrack at 15

>>>> please help me

16 Aug 2016 Isuru Hindurangala

thank you

27 Jun 2016 Gul Rukh Khan

Thanks for your good work.
Highly appreciated.

23 Jun 2016 Marco Grasso

17 May 2016 Muhammad Wahid

thanks a lot

Comment only
13 May 2016 badri INFO


09 May 2016 crhistian acosta cortez

I have 2 errors:

Error using end
Incorrect cell or structure reference involving "end".
Most likely cause is a reference to multiple elements of a cell or structure followed by additional subscript or structure

Error in getCameraInfo (line 4)
camera_id = camera_info.DeviceInfo.DeviceID(end);

Error in redObjectTrack (line 2)
[camera_name, camera_id, format] = getCameraInfo(a);

26 Apr 2016 ali hussn


29 Feb 2016 Divyesh Panchal

Its very good, I have complete RED, GREEN and Blue detection by minimum changes in your program but..
1.How can we detect this all color in single window, though i used 'hold on' its not working properly.
2.what changes i should make if i want to detect other color like yellow?
provide help please.

30 Dec 2015 Aviv Yaacobi

Hello Bhargav,
I want to use your code on an mp4 file.
So i use the command "VideoReader" to read this file and the Matlab tool show me an Error - "Error using VideoReader/set
The name 'FramesPerTrigger' is not an accessible property for an instance of class 'VideoReader'."
How can you help me with this problem.

11 Dec 2015 hamada brahim


08 Nov 2015 KDN

KDN (view profile)

Hello Bhargav ,
It's nice work. Can you help me to detect brightness of red color and display it on screen, what if I have to switch from red to white.

09 Oct 2015 Rajish Rajagopal

thanks a lot

07 Aug 2015 Alireza Babaloo

thanks it works perfect

07 Aug 2015 kotheinphay myint

thanks a lot

15 Jul 2015 edofersan

Thanks a lot.

21 Jun 2015 abirov

abirov (view profile)

Thanx for code, that is amazing .
Is it possible to teach camera for color tone from camera than track it ?

15 Jun 2015 safwatunnisa' amjaduzzahwi

15 Jun 2015 safwatunnisa' amjaduzzahwi

Hello mr A. Bhargav Anand,
Tq for sharing such an amazing code.
I have run the code but I got this error:

Error event occurred at 06:45:52 for video input object: RGB24_640x480-winvideo-2.
Unable to allocate memory for an incoming image frame. Use IMAQMEM to modify the frame memory limit.
Unable to allocate memory for an incoming image frame. Use IMAQMEM to modify the
frame memory limit.

Error in imaqdevice/getsnapshot (line 63)
[snapshot, metadata] = getsnapshot(imaqgate('privateGetField', obj,

Error in redobjectdetection (line 16)

Error using imaqdevice/getsnapshot (line 65)
A timeout occurred during GETSNAPSHOT.

Error in redobjectdetection (line 16)

what should i do?
plz help me to fix this problem.
TQ :)

18 May 2015 kwstis xoustoulakis

i hv made a robotic arm with 3 servos and a camera at the top and i want my camera to detect red objects and moves my arm in these,so yr code is that i want mr.Anand but i need to add some code yet, that talks to servos.if u can help me i ll apreciate it...! Thanks!

Comment only
24 Apr 2015 Justin Kirk

09 Apr 2015 ilin kang


I'd like to find a red circle.

Also I'd like to make a range of rectangle tool flexibly to find the red circle withing the range.

I'd appreciate your cooperation.
Please reply to

04 Apr 2015 Rajeev Pushp

27 Mar 2015 sondos qasaymeh


12 Mar 2015 myint Nyan

08 Mar 2015 Pablo Salarolli

26 Feb 2015 burak

burak (view profile)

very impressive... ty

19 Jan 2015 helwan ghgs

please Mr.Anand, can you give me your contact email?

My Regards

Comment only
07 Jan 2015 Rizky ramadhan

hello mr bhargav. what is algorhythm in this code ?
thanks for the code its amazing

19 Nov 2014 Manoj Reddy

Hello Mr.Bhargav. Iam getting an error in function file(get camerainfo.m).
the error is
Error in ==> getCameraInfo at
??? Input argument "a" is undefined.
What should I do now?

Comment only
16 Oct 2014 Aisha

Aisha (view profile)

25 Aug 2014 Mazinho

17 Aug 2014 daniel grant

Excellent work.

13 Jun 2014 Sergiu Craciun

The code works perfectly.Thank you a lot ! I have a different tone of red to recognize.Can you please hep me?I want to modify it to recognise all types of red.Thanks

Comment only
09 May 2014 asan masraf

Thanks for your efforts,
I would like to ask you if I want to change tracking object from red color to another color or may be for tracking some specific shapes, how can I do?

Comment only
03 May 2014 Qasim Ali

22 Apr 2014 Büşra

09 Apr 2014 Ahsin Ali Khan

26 Mar 2014 Emre

Emre (view profile)

Hi, that is a great job. In my thesis, I need to find the shortest distance between a red circle center and a line. Do you have an idea about how could i do this? or anyone else?

17 Mar 2014 Michael Notarangelo

Scratch that last comment, did not read all the older comments

Comment only
13 Mar 2014 Syifak Izhar

13 Mar 2014 sky nku

Good job

12 Mar 2014 Surya teja Masanam

Thank u so much !!!!!
It's working . . .

31 Dec 2013 wasfi al-rasheed


27 Dec 2013 daher

daher (view profile)

Hi. great job. i have a question if you dont mind. let us assume that we have to track the first noticed red object, and to keep tracking it whilst other red objects appear.. any idea how to tackle this issue without referring to the geometry of the objects?

Comment only
10 Dec 2013 sfl

sfl (view profile)

14 Nov 2013 CHANDAN Nagaraja

Wow.. Thanks a lot.
I was wondering, can we store all the X,Y values( that comes on webcam screen as we move the red object) in a program as output value..??

Comment only
26 Oct 2013 TRINAYAN

Your code is great and so fast. I am having a problem .When i try to use the centroid coordinates to display a particular its showing the wrong results. I want to use this for a object following robot but when i use a if loop to decide which character to be sent its not sending correctly.

24 Oct 2013 mooud

mooud (view profile)

this is great . thank you
code works perfect . easily can change the red color to green or blue by changing 1 to 2(green) or 3 in :
(data(:,:,1), rgb2gray(data));
i had a problem in objects were far from cam to recognize, which solved by reducing the number in :
diff_im = bwareaopen(diff_im,600);
still don't know how to extract the coordinates and save them ??

04 Sep 2013 zetad yosif

13 Jul 2013 ADNAN

ADNAN (view profile)

gud work

06 Jul 2013 Anas Blue

hi guys how can I save the coordinates from this code into an array ??

Comment only
05 Jul 2013 chairat attawicha

thank you very much for code.

25 May 2013 larry suide

Very good, but the last two lines of get_camera_info can give error; these work for me:


Comment only
25 May 2013 larry suide

27 Apr 2013 haitham HAJJ SHEHADE

i want to install this code on a dsp and as an output the coordinates of the tracked red. what is the fct to output them to the workspace?

20 Apr 2013 SNAKE

SNAKE (view profile)

How can I extract the position of an object (red) , and store it???

17 Apr 2013 vishnu andavar

good one,but how to interrupt it while the loop is on??? stop(vid) doesn't work until 200 frames are taken ,

07 Apr 2013 Jenisten Stark

Hi dude.. this tracking is awesome... But can u help me with some more processing like, reducing the brightness of the red colored object.

11 Feb 2013 Arvin

Arvin (view profile)

how to detect any color other than white?

Comment only
20 Jan 2013 A. Bhargav Anand

A. Bhargav Anand (view profile)

  • 1 file
  • 4.63514

the '-m+' is an argument given to the plot function. Just to display markers on the plot, we pass it as an argument. Here '-m+' refers to use the color magenta and display a 'plus' sign wherever required. You could do 'help plot' for further help. Thanks

Comment only
20 Jan 2013 Siddharth

Hey Bhargav, this code works really well, just a doubt though, what does '-m+' in the plot(bc(1),bc(2), '-m+') function mean?

12 Dec 2012 amila perera

:( Guys..I got this error msg..What might be the problem..?

??? Error using ==> end
Incorrect cell or structure reference involving "end".
Most likely cause is a reference to multiple elements of a cell or
structure followed by additional subscript or structure references.

Error in ==> getCameraInfo at 4
camera_id = camera_info.DeviceInfo.DeviceID(end);

Error in ==> redObjectTrack at 2
[camera_name, camera_id, format] = getCameraInfo(a);

04 Nov 2012 James

James (view profile)

Scratch what I said earlier, haha the numerical value can detect white and Blue, but not green or red or black. haven't tried all the colors but still, great job!

Comment only
04 Nov 2012 James

James (view profile)

Hi! I love your work!
I got curios and messed around with the codes, in this line..
diff_im = imsubtract(data(:,:,1), rgb2gray(data));

change the numerical value 1 to 2 and 3..
the value of 3 can detect color white. while value of 2, I couldn't detect anything.

can you tell me how to detect other colours?

or much better tell me where you learned this. thank you your work is awesome! keep it up! :)

Comment only
14 Oct 2012 Rod

Rod (view profile)

06 Sep 2012 Gioridesky

22 Aug 2012 arsalan sadiq

22 Aug 2012 arsalan sadiq

dude can u plz tell me how to detect other colors also....

Comment only
09 Jul 2012 Chen Mr

pretty good!!

Comment only
31 May 2012 abu sadat

31 May 2012 abu sadat


23 May 2012 Madiake Wasubera

Thanks a lot mate,your prog has helped me a lot,keep doin your thing.

Comment only
22 May 2012 Fuat Cogun

14 May 2012 Madiake Wasubera

maybe it could benefit me ,if you were to tell me where did you leant that.

12 May 2012 Ajay Kumar

08 May 2012 elias argaw

please help
i try your code but i got the below message how i can fix it

??? Undefined function or method 'getCameraInfo'
for input arguments of type 'struct'.

Comment only
25 Apr 2012 Mahdy Jadaliha

Great job. Thank you for sharing.

15 Apr 2012 Peter Russo

Awesome, thanks a lot! I've been trying to make my own that uses just the intensity of the grayscale image, which is giving me lots of problems. By just picking a color, like red, it should be a lot easier. Thanks

03 Apr 2012 Mr Smart

20 Mar 2012 Mohamad Faiz

Hi dude, do you know how to track red color using IP camera?


Hi..Cud u pl tell me how to-
1) track colours other than red.
2) set the threshold such that the object in the front is tracked and not any other object in the back.

31 Jan 2012 Boo

Boo (view profile)

24 Jan 2012 dads PB

Hey Dude if you teach me create red light camera using M-file ? pleasee

16 Jan 2012 atiqah

atiqah (view profile)

08 Jan 2012 nishant


18 Nov 2011 alexander

i'm getting errors from the getcamerainfo function

Error in ==> getCameraInfo at 4
camera_id = camera_info.DeviceInfo.DeviceID(end);

Error in ==> redObjectTrack at 2
[camera_name, camera_id, format] = getCameraInfo(a);

running r2010a 64 bit in windows 7

how might i remedy these errors

Comment only
10 Nov 2011 Sujin Wanchat

17 Oct 2011 Karlo Isaac Ibáñez Viruega

Dude, my ML tells me that an error occurred " Error: File: redObjectTrack.m Line: 2 Column: 2
An array for multiple LHS assignment cannot contain character string." i think its related to the format of my camera : YCbCr422_640x480 does your program run with this format

09 Jul 2011 Muhammad usman khan

hey please tell me to initialize webcam in MATLAB... im new in MATLAB.. what i have to write in your code to run it...
i thnk its cam name n resolution etc.. please give me example of correct syntax and where to read the name of camera and resolution...

Comment only
26 Jun 2011 Atif Anwer

Brilliant Job Done! Really helpful in using this as a start for my Vision project :)

14 May 2011 Jorge Lewinnek

I think it would be better if you use the conversion rgb to hsi, and determine the red color with hue, but it is a nice algorithm, and you help me a lot to know how to use my laptop camera.

Comment only
22 Apr 2011 Winston Larson

I had this working like a dream on Windows (this is a wonderful piece of software), but it seems to have trouble on a Mac. The video doesn't move fluidly, but rather captures just a couple of frames. Does anyone have the same issues, or advice? I'm admittedly new to Mac... Thanks!

Comment only
26 Dec 2010 Arun

Arun (view profile)

The speed of your code can be greatly improved if u can keep preview open in background.Also try to replace getsnapshot with peekdata (dont know to what extent it is possible)to reduce execution time(this may require manual triggering)

Comment only
19 Dec 2010 Vladimir

25 Nov 2010 A. Bhargav Anand

A. Bhargav Anand (view profile)

  • 1 file
  • 4.63514

Thanks man :)

Comment only
24 Nov 2010 Hoa Dang

you are wonderful !!!
Thank you alot

18 Nov 2010 chen po sen

very smart and good experimental , thks .

17 Nov 2010 A. Bhargav Anand

A. Bhargav Anand (view profile)

  • 1 file
  • 4.63514

Updated the code guys. Also uploaded a video.

Comment only
02 Nov 2010 A. Bhargav Anand

A. Bhargav Anand (view profile)

  • 1 file
  • 4.63514

@hiren, ika- sorry guys, ive been little busy these dayz, neways ill definitly get back to you guys with an answer. As of now this program was written only to detect the standard colors i.e. Red, Green & Blue colors. I was thinking to extend the whole program by adding a graphical interface and generalizing the whole tracking process.

@akash- you get lotz of stuff on internet dude. juss google about object tracking or motion tracking. Look the documentation of the image processing & video processing toolbox in matlab. Use the matlab help and go through the documentation of important functions that will be helpful. Thas enough fr mastering fundas !!! :)

Comment only
31 Oct 2010 ika

ika (view profile)

can u tell me how to make tracking object if i want track motion object and give annotation from start point untilthe object stop moving,so i can track from fisrt position, for example tracking with line???

thanks before....

Comment only
08 Oct 2010 hiren

hiren (view profile)

hi dude can u tell me how i can detect other colour..
i mean where i can change the code so i can do that thing...

Comment only
08 Oct 2010 hiren

hiren (view profile)

08 Oct 2010 hiren

hiren (view profile)

01 Oct 2010 A. Bhargav Anand

A. Bhargav Anand (view profile)

  • 1 file
  • 4.63514

@akash Dude, download the code, I have documented the whole code. Just go through it, try reading the documentation of every function i have used. And yeah, where did I get this stuff, GOOGLE it man !!!

Comment only
01 Oct 2010 akash

akash (view profile)

wow!!really that was so simple.can u tell me where u learned this will be really helpful

17 Nov 2010 1.1

Previously the program just tracked the red object. Now the program has been updated to display the centroid of the detected object dynamically.

Contact us