Code covered by the BSD License  

Highlights from
Tracking red color objects using matlab

4.7 | 52 ratings Rate this file 530 Downloads (last 30 days) File Size: 2.27 KB File ID: #28757
image thumbnail

Tracking red color objects using matlab



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 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 (80)
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

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

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

Updated the code guys. Also uploaded a video.

Comment only
02 Nov 2010 A. Bhargav Anand

@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

@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

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