# Analysing Motion Detection and Tracking using MATLAB...

by 4hathacker  |  in Image Processing at  12:53 AM

Hello everyone...!!!

This time I have covered a meaningful introduction to motion detection in videos. Till date I have covered in my articles the very basics of image processing and some more about image acquisition and detection of objects. Its time to move on to motion detection and analysis. I have done inter frame processing and frame extraction during edge detection and time lapsing in videos which will help you to better understand this article.

Motion Detection can be defined as the process of detecting changes in the relative position of an object to its background. The background is defined as per the consecutive frames of video. The estimation of motion is based on a very simple approach i.e., subtract the consecutive frames, such that the values of pixels that changes appear brighter than those that remain constant. But this is not a sophisticated approach.

Using MATLAB, read the test video and subtract consecutive frames and then add the results so obtained. The test video is taken from a random youtube source in which a snooker player shots the ball. The aim is to detect the motion of the ball using subtraction of consecutive frames. The results obtained shows some more specific things in regard with the motion detection and analysis.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%   Name: Nitin Sharma
%%%%   Code: framedetmoving.m
%%%%   MATLAB version: R2014a
%%%%   Toolbox Required: Image Processing Toolbox, Computer
%%%%                                  Vision System Toolbox
%%%%   Utility:     Understanding concepts of motion analysis,
%%%%                    Finding important aspects of brightness constancy,
%%%%                    Problems in motion estimation.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%             Read the test video and video frames

%%%             Showing video frames along each other
montage(vid,'Size',[9 9])
subtracted = zeros(size(vid));

%%%             Subtracting each frame from next one except the first frame
for i = 2:size(vid,4),
subtracted(:,:,:,i) = vid(:,:,:,i) - vid(:,:,:,i-1);
end
montage(subtracted,'Size',[9 9])
result = sum(subtracted,4);
result = mat2gray(result);
figure,imshow(result)

From the above code, the results obtained shows that the brighter pixels belong to the moving object. But the snooker table, the table at other side and the other balls are also brighter. The trajectory of ball obtained is good in terms of visibility but the player who shot the ball is somewhat creating a ghost like appearance.

The reason behind the appearance of bright pixels in all other objects except moving ball is the shaky nature of camera rolling the video stream. The position of other objects is not still in every consecutive frame and hence due to such nature of shaky background other pixels also showed existence of motion. In many cases, the effect of intensity, shadowing and occlusions may effect the motion detection to give erroneous and faulty results. There are sophisticated algorithms for detecting motion of objects which will be covered in the upcoming articles.