View License

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

» Watch video

Highlights from
How to Detect and Track Red Colored Object in LIVE Video

4.2 | 12 ratings Rate this file 88 Downloads (last 30 days) File Size: 2.22 KB File ID: #40022 Version: 1.5
image thumbnail

How to Detect and Track Red Colored Object in LIVE Video


Arindam Bose (view profile)


28 Jan 2013 (Updated )

This Video shows how to detect and track red colored object in LIVE video using MATLAB.

| Watch this File

File Information

This code shows how to detect and track red colored object in LIVE video using MATLAB. Not only red, but also green and blue object can be detected using this algorithm.
For deteil documentation please refer to:
For video tutorial please refer to:

Required Products Image Acquisition Toolbox
Computer Vision System Toolbox
MATLAB release MATLAB 7.14 (R2012a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (24)
30 Oct 2016 Akhmad Dias

03 Jul 2016 nmc

nmc (view profile)

Do you happen to know how to track the first identified colored object in a video when other objects will appear with the same color without referring to geometry?

Comment only
26 Apr 2016 Hammad Malik

Tracking isn't working much need some improvements.

25 Apr 2016 kaz

kaz (view profile)

got my answer down below thank you sir

25 Apr 2016 kaz

kaz (view profile)

sir the video stops immediately after initialization. it just freezes and show stopped. how to solve this error plz. thank you in advance

Comment only
27 May 2015 Suhas Kale

i am using matlab 2010,2013 but following error occurs.
plz help me out to sort this error.

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

Error in ==> MATLABProjectCode at 27
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ... % Acquire input video stream

Comment only
07 May 2015 vir

vir (view profile)

sehr gut!

22 Feb 2015 Tomas Sab

very minimalistic and optimized. Works great for simple needs

24 Jan 2015 freedomYo

30 Nov 2014 Kyle

Kyle (view profile)

This is an extremely impressive and effective code. Don't give a bad rating just because you're experiencing errors. Make sure you've installed all the necessary toolboxes and add-ons, and that your resolution value in VideoDevice is accurate

01 Oct 2014 Abhijna

Thank you.. was Helpful

02 Apr 2014 Md Towfique Ahmmed

>> redObjectTrack
Undefined function or variable 'video'.

Error in redObjectTrack (line 18)
data = getsnapshot(video);
What is the problem?please tell

Comment only
21 Dec 2013 haris fauzi

what should i change in the code if i want to detect yellow colour? is it the threshold value? if yes, what is the threshold value for yellow? tq

Comment only
26 Aug 2013 CHETHAN

i am using matlabR2012a and i am geting the following error message

Error: File: test1.m Line: 27 Column: 14
The expression to the left of the equals sign is not a valid target for an

this corrosponds to line in the code after wile loop begins i.e

while(nFrame < 500)
  rgbimage = step(vidDevice); <<<<<<<<<<<<<<<<<<<<<erroe occured here<<<<<<<<<<<
  rgbFrame = flipdim(rgbimage,2);
  diffFrame = imsubtract(rgbFrame(:,:,1),

Comment only
09 Aug 2013 Alap

Alap (view profile)

I have Matlab 2011a and I also have the Image Acquisition Toolbox installed. However I still get the error mentioned by Faisal below. Is there an updated version of the toolbox or some such thing?

Comment only
06 Aug 2013 Arindam Bose

Arindam Bose (view profile)

@Faisal: Install Image acquisition toolbox in your MATLAB version.

Comment only
06 Aug 2013 Lakshmi

iam using matlab 2011a and iam getting the following error msg when i
use ur code.
??? Error using ==> step at 81 (i.e, error(nargchk(2,6,ni)); in step.m file)
Not enough input arguments.

Error in ==> RedObjectDetection at 39
rgbFrame = step(vidDevice); % Acquire single frame

Comment only
03 Aug 2013 Van

Van (view profile)

18 Jun 2013 Faizan Faisal

encountered this error :( Please help
Undefined variable "imaq" or class "imaq.VideoDevice".

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

18 Feb 2013 Arindam Bose

Arindam Bose (view profile)

Please notice the loop counter, I have made it 20 for a shorter demonstration. Please change to several thousands to ensure the suitable run time of the code.

Comment only
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 200 frames. 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.
Make a change in the line:
while(nFrame < 50000)

Comment only
12 Feb 2013 Masoud Ghanbari

after running this the program will stop after seconds...
What's the problem????

11 Feb 2013 Fiem

Fiem (view profile)

01 Feb 2013 Arindam Bose

Arindam Bose (view profile)

In my code I used
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', 'ROI', [1 1 640 480], 'ReturnedColorSpace', 'rgb');
as my video input adapter.

You should check your image acquisition toolbox for video adapters available in your system. Then arrange the line according to it.

Comment only
29 Jan 2013 1.1

Description Updated. Video Tutorial added.

05 Feb 2013 1.2

description updated

21 Feb 2014 1.5

Description Changed

30 Jun 2016 1.5

Description changed

Contact us