How to Detect and Track Red Objects in Live Video in MATLAB

Hi, Everyone, In this page, I want to show you how you can detect and track red objects in live video. To detect the red color in every single frame we need to know different approaches.

The popular approach is to convert the whole RGB frame into corresponding HSV (Hue-Saturation-Value) plane and extract the pixel values only for RED. So choose a range in which different shades of red exists. in this way you can detect almost all distinguishable colors in a frame.

But this approach is a bit difficult in real life problem especially in Live video due to ambient light. One more simple solution exists there if you decide to detect only red or green or blue color. So this approach is not versatile for all colors, but its simpler than anything and you can easily eliminate the ambient light problem using it. So I am gonna use this approach to detect red color.

How to detect red color:
Suppose our input video stream is handled by vidDevice object.

Step 1: First acquire an RGB Frame from the Video.
MATLAB Code: rgbFrame = step(vidDevice);


Step 2: Extract the Red Layer Matrix from the RGB frame.
MATLAB Code: redFrame = rgbFrame(:,:,1);


Step 3: Get the grey image of the RGB frame.
MATLAB Code: grayFrame = rgb2gray(rgbFrame);


Step 4: Subtract the grayFrame from the redFrame.
MATLAB Code: diffFrame = imsubtract(redFrame, grayFrame);


Step 5: Filter out unwanted noises using Median Filter
MATLAB Code: diffFrame = medfilt2(diffFrame, [3 3]);


Step 6: Now convert the diffFrame into corresponding Binary Image using proper threshold value. Change its value for different light conditions. Suppose in my code I have used its value as 0.15.
MATLAB Code: binFrame = im2bw(diffFrame, 0.15);


Step 7: Now you are all done. Your Red color has been detected. Now you can put any blob statistics analysis on this image. You can calculate the centroid, area or bounding box of those blobs.

The same algorithm I have introduced in my code. Here I am giving the code of my project.


% Program Name : Red Object Detection and Tracking
% Author : Arindam Bose
% Version : 1.05
% Description : How to detect and track red objects in Live Video
%% Initialization
redThresh = 0.15; % Threshold for red detection
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ... % Acquire input video stream
'ROI', [1 1 640 480], ...
'ReturnedColorSpace', 'rgb');
vidInfo = imaqhwinfo(vidDevice); % Acquire input video property
hblob = vision.BlobAnalysis('AreaOutputPort', false, ... % Set blob analysis handling
'CentroidOutputPort', true, ...
'BoundingBoxOutputPort', true', ...
'MinimumBlobArea', 800, ...
'MaximumBlobArea', 3000, ...
'MaximumCount', 10);
hshapeinsRedBox = vision.ShapeInserter('BorderColor', 'Custom', ... % Set Red box handling
'CustomBorderColor', [1 0 0], ...
'Fill', true, ...
'FillColor', 'Custom', ...
'CustomFillColor', [1 0 0], ...
'Opacity', 0.4);
htextins = vision.TextInserter('Text', 'Number of Red Object: %2d', ... % Set text for number of blobs
'Location', [7 2], ...
'Color', [1 0 0], ... // red color
'FontSize', 12);
htextinsCent = vision.TextInserter('Text', '+ X:%4d, Y:%4d', ... % set text for centroid
'LocationSource', 'Input port', ...
'Color', [1 1 0], ... // yellow color
'FontSize', 14);
hVideoIn = vision.VideoPlayer('Name', 'Final Video', ... % Output video player
'Position', [100 100 vidInfo.MaxWidth+20 vidInfo.MaxHeight+30]);
nFrame = 0; % Frame number initialization
%% Processing Loop
while(nFrame < 500)
rgbFrame = step(vidDevice); % Acquire single frame
rgbFrame = flipdim(rgbFrame,2); % obtain the mirror image for displaying
diffFrame = imsubtract(rgbFrame(:,:,1), rgb2gray(rgbFrame)); % Get red component of the image
diffFrame = medfilt2(diffFrame, [3 3]); % Filter out the noise by using median filter
binFrame = im2bw(diffFrame, redThresh); % Convert the image into binary image with the red objects as white
[centroid, bbox] = step(hblob, binFrame); % Get the centroids and bounding boxes of the blobs
centroid = uint16(centroid); % Convert the centroids into Integer for further steps
rgbFrame(1:20,1:165,:) = 0; % put a black region on the output stream
vidIn = step(hshapeinsRedBox, rgbFrame, bbox); % Instert the red box
for object = 1:1:length(bbox(:,1)) % Write the corresponding centroids
centX = centroid(object,1); centY = centroid(object,2);
vidIn = step(htextinsCent, vidIn, [centX centY], [centX-6 centY-9]);
vidIn = step(htextins, vidIn, uint8(length(bbox(:,1)))); % Count the number of blobs
step(hVideoIn, vidIn); % Output video stream
nFrame = nFrame+1;
%% Clearing Memory
release(hVideoIn); % Release all memory and buffer used
clear all;

To download the source code from MATLAB CENTRAL Community Click Here: How to Detect and Track Red Colored Object in LIVE Video

You can watch the video tutorial also:

More tutorials are coming. If you have any idea, share with me. Thanks a lot.


3 thoughts on “How to Detect and Track Red Objects in Live Video in MATLAB

  1. help,

    ??? Undefined variable “imaq” or class “imaq.VideoDevice”.

    Error in ==> teste at 9
    vidDevice = imaq.VideoDevice(‘winvideo’, 1, ‘YUY2_640x480’, … %
    Acquire input video stream


  2. Hello Arindam Bose, this post is excellent, i want to know if this process or technique for detecting the red color in the image have a name specific, and where i can find more information about this process or .technique. thanks and Greetings from Colombia.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.