Monday, February 22, 2010

Video - Motion Detection

There are many approaches for motion detection in a continuous video stream. All of them are based on comparing of the current video frame with one from the previous frames or with something that we'll call background.

as you can see from my implementation, the results are not very promising, because of the colors in the video stream. the next approach is using image and converting to gray scale.

here the code for background subtraction using video frames:

clear all
close all

clc;

disp('Testing Purposes Only....');

fin = 'sampleVideo.avi';

fout = 'test2.avi';

fileinfo = aviinfo(fin);
nframes = fileinfo.NumFrames;

aviobj = avifile(fout, 'compression', 'none', 'fps',fileinfo.FramesPerSecond);
for i = 2:nframes %1

%Read frames from input video

mov_in = aviread(fin,i);
im_in = frame2im(mov_in);

%Do processing on each frame of the video

%----------------------------------------------------------------------

mov_in2 = aviread(fin,i-1); %or 1 for the first frame
im_in2 = frame2im(mov_in2);

background = imopen(im_in2,strel('disk',15)); %get the background image
I2= imsubtract(im_in,background);

im_out =I2;

%----------------------------------------------------------------------
%Write frames to output video
frm = im2frame(im_out);
aviobj = addframe(aviobj,frm);
%i %Just to display frame number

end;

%Don't forget to close output file
aviobj = close(aviobj);

msgbox('DONE');

return;


results:

5 comments:

  1. hii i am nikita..
    can u help me wid the project which takes live video from webcam and applies background subtraction algorithm....
    thank you....

    ReplyDelete
  2. hey, Aresh T n Nikita,..
    am too working for real time motion detection..
    almost done, but its too slow..
    10 framses per sec :( i need to send the co-ordinates to a file.. if i do that its gonna be even more slower..
    any alternatives ?? any help pls ..

    ReplyDelete
  3. Hi, i highly recommend OPENCV. as i have shifted to opencv aswell.

    ReplyDelete