Friday 29 July 2011

        1.MATLAB PROJECT FOR MOTION DETECTION

Hey everyone. This is a simple project for a kind motion detection using MATLAB. This method is based on background subtraction method. In this method the previous frame of the video is taken as the background for the present frame, and we only see those pixels whose value differs from the previous frame by at least a certain value called threshold value.  So with this method we could  only see the part of the video which is constantly moving and does not stop.The program has been written in such a way that the background (which are at rest ) seems black and the moving part white. Since its easier to work with a gray level image, so here in this project i  process the gray scaled frames. However you can change the program in your way. Good luck. 

I hope this is pretty helpful for those who has just started processing the images and want to go for tracking an object and detecting motions. Please don't forget to comment if you liked it. And be remember to share your problems and difficulties with me. And u can also share your programs with us. Just fill free to write anything.
 Here is the program:
clear
clc
frames=mmreader('video1.avi');
numFrames =frames.NumberOfFrames;
thresh = 50;
first = read(frames,1); % reading the 1st frame as background frame
first_gray = rgb2gray(first); % convert this 1st frame to greyscale
% -------------------------------------------------------------------
first_frame_size = size(first_gray);
width = first_frame_size(2);
height = first_frame_size(1);
others = zeros(height, width);
% ------------------------------------------------------------------
for i = 2:numFrames/15
    new = read(frames,i); % read in frame
    new_gray = rgb2gray(new); % convert frame to grayscale
    new_diff = abs(double(new_gray) - double(first_gray));
    for j=1:width
        for k=1:height
            if ((new_diff(k,j) > thresh))
            others(k,j) = new_gray(k,j);
            else
            others(k,j) = 0;
            end
        end
    end
first_gray = new_gray;
subplot(1,2,1),imshow(new);
title('original image');
%subplot(1,3,2),imshow(new_gray)
subplot(1,2,2),imshow(uint8(others));
title('moving part');
end