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

Hi there, I am here again presenting a new process to detect and track white objects in LIVE video. In this topic I will show you how to detect white color step by step. So, sit tight. There may be some other opinions by which you can detect white color. So I am inviting all those ideas from this page. Please share me your ideas if you have one.

How to detect white 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);

rgbFrame

rgbFrame

Step 2: Acquire the Red, Green and Blue layer of the RGB Frame.
MATLAB Code: redFrame = rgbFrame(:,:,1);
greenFrame = rgbFrame(:,:,2);
blueFrame = rgbFrame(:,:,3);

redFrame

redFrame

greenFrame

greenFrame

blueFrame

blueFrame

Step 3: Convert these images into binary images using corresponding threshold. In my code I have taken same threshold for all three levels which is 0.8.
MATLAB Code: bwredFrame = im2bw(redFrame, 0.8);
bwgreenFrame = im2bw(greenFrame, 0.8);
bwblueFrame = im2bw(blueFrame, 0.8);

MVP10_bred

bwredFrame

MVP10_bgreen

bwgreenFrame

MVP10_bblue

bwblueFrame

Step 4: Now you are almost done. Now you have to take the common region from them. So do an AND operation on them.
MATLAB Code: binFrame = bwredFrame & bwgreenFrame & bwblueFrame;

binFrame

binFrame

Step 5: Now you are all done. You can filter this image by a median filter, but I think this in not so important at all. Your white color has already 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.

MATLAB CODE

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Program Name : white Object Detection and Tracking
% Author : Arindam Bose
% Version : 1.1
% Description : How to detect and track white objects in Live Video
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Initialization
thresh = 0.8; % Threshold for white 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, ...
'MaximumCount', 10);
hshapeinsWhiteBox = vision.ShapeInserter('BorderColor', 'White'); % Set white box handling
htextins = vision.TextInserter('Text', 'Number of White Object(s): %2d', ... % Set text for number of blobs
'Location', [7 2], ...
'Color', [1 1 1], ... // white color
'Font', 'Courier New', ...
'FontSize', 12);
htextinsCent = vision.TextInserter('Text', '+ X:%6.2f, Y:%6.2f', ... % set text for centroid
'LocationSource', 'Input port', ...
'Color', [0 0 0], ... // black color
'FontSize', 12);
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 < 200)
rgbFrame = step(vidDevice); % Acquire single frame
rgbFrame = flipdim(rgbFrame,2); % obtain the mirror image for displaying
bwredFrame = im2bw(rgbFrame(:,:,1), thresh); % obtain the white component from red layer
bwgreenFrame = im2bw(rgbFrame(:,:,2), thresh); % obtain the white component from green layer
bwblueFrame = im2bw(rgbFrame(:,:,3), thresh); % obtain the white component from blue layer
binFrame = bwredFrame & bwgreenFrame & bwblueFrame; % get the common region
binFrame = medfilt2(binFrame, [3 3]); % Filter out the noise by using median filter
[centroid, bbox] = step(hblob, binFrame); % Get the centroids and bounding boxes of the blobs
rgbFrame(1:15,1:215,:) = 0; % put a black region on the output stream
vidIn = step(hshapeinsWhiteBox, rgbFrame, bbox); % Instert the white box
for object = 1:1:length(bbox(:,1)) % Write the corresponding centroids
vidIn = step(htextinsCent, vidIn, [centroid(object,1) centroid(object,2)], [centroid(object,1)-6 centroid(object,2)-9]);
end
vidIn = step(htextins, vidIn, uint8(length(bbox(:,1)))); % Count the number of blobs
step(hVideoIn, vidIn); % Output video stream
nFrame = nFrame+1;
end
%% Clearing Memory
release(hVideoIn); % Release all memory and buffer used
release(vidDevice);
clear all;
clc;

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

You can watch the video tutorial also:

Hope this will help. More tutorials are coming. If you have any idea, share with me.

About the author

Scientific History Blog Writer • Art enthusiast and Illustrator • Amateur Photographer • Biker and Hiker • Beer Enthusiast • Electrical Engineer • Chicago

Leave a Reply

Twitter Feed
%d bloggers like this: