Motion Detection in LIVE Video
06/25/2013
3,910
Motion Detection in LIVE Video

First of all I want to give a small introduction of this project but in brief. We are going to detect the motion of any moving object using our well known MATLAB toolboxes. I use in my machine MATLAB R2012a, which includes Image Acquisition Toolbox and Computer Vision Toolbox. Computer Vision Toolbox indeed is a very useful tool in the field of image processing. Though I used the Image Acquisition Toolbox for acquiring the live image stream from the webcam, but anyone can use other MATLAB commands to do the same. So this particular toolbox is not a big deal in this case.

In my sample program I used Optical Flow Method to detect the moving objects. Here I am giving the whole code. All the code lines are self explanatory. So I didn’t thought of including any comment after each of the line. Now if you still have any further problem of understating the code and need any kind of help, do not hesitate to write me. Will be uploading more MATLAB codes. All the best.

MATLAB CODE:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Program Name : Motion Detection in LIVE Video %
% Author : Arindam Bose %
% Version : 1.0 %
% Description : This program detects any moving objects in LIVE Video %
% and put a red mark in the moving pixels. %
% Copyright : © Arindam Bose, All rights reserved. %
% License : Open Source, Freeware %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ...
'ROI', [1 1 640 480], ...
'ReturnedColorSpace', 'rgb');
optical = vision.OpticalFlow('OutputValue', 'Horizontal and vertical components in complex form');
maxWidth = imaqhwinfo(vidDevice,'MaxWidth');
maxHeight = imaqhwinfo(vidDevice,'MaxHeight');
shapes = vision.ShapeInserter;
shapes.Shape = 'Lines';
shapes.BorderColor = 'Custom';
shapes.CustomBorderColor = [255 0 0];
r = 1:5:maxHeight;
c = 1:5:maxWidth;
[Y, X] = meshgrid(c,r);
hVideoIn = vision.VideoPlayer;
hVideoIn.Name = 'Original Video';
hVideoIn.Position = [30 100 640 480];
hVideoOut = vision.VideoPlayer;
hVideoOut.Name = 'Motion Detected Video';
hVideoOut.Position = [700 100 640 480];
nFrames = 0;
while (nFrames < Inf)
rgbData = step(vidDevice);
optFlow = step(optical,rgb2gray(rgbData));
optFlow_DS = optFlow(r, c);
H = imag(optFlow_DS)*50;
V = real(optFlow_DS)*50;
lines = [Y(:)'; X(:)'; Y(:)'+V(:)'; X(:)'+H(:)'];
rgb_Out = step(shapes, rgbData, lines');
step(hVideoIn, rgbData);
step(hVideoOut, rgb_Out);
nFrames = nFrames + 1;
end
release(hVideoOut);
release(hVideoIn);
release(vidDevice);

In the code I have configured my webcam for my laptop settings, Please use your configuration for your webcam. Here I have enclosed the screenshots of the project.

Motion-Detection-1024x575

Look at my moving fingers which was detected by the program which are marked by red, but not my face which was stationary.

Hope this helps a lot. Thanks… More Programs are coming soon…

About the author

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

One comment
Leth Hashem
04/24/2017

Good job my bro!
I wish you the best.

Leave a Reply

Twitter Feed
%d bloggers like this: