Motion Detection in Matlab

You can post your problem related to MATLAB Projects here. We will try our best to help you out.
Post Reply
Robinson
Posts:12
Joined:Fri Mar 03, 2017 11:06 am
Motion Detection in Matlab

Post by Robinson » Fri Mar 03, 2017 12:16 pm

hello guys!!..

can anyone teach me how motion detection done in matlab??
thanks..

or is there anyone who can give his or her code for that,,

Thank You..

Junaid_Shahid
Posts:110
Joined:Tue Feb 14, 2017 9:00 am

Re: Motion Detection in Matlab

Post by Junaid_Shahid » Fri Mar 03, 2017 12:16 pm

Robinson wrote:
Fri Mar 03, 2017 12:16 pm
hello guys!!..

can anyone teach me how motion detection done in matlab??
thanks..

or is there anyone who can give his or her code for that,,

Thank You..
You have to detect motion in some video or how? kindly elaborate your problem

Robinson
Posts:12
Joined:Fri Mar 03, 2017 11:06 am

Re: Motion Detection in Matlab

Post by Robinson » Fri Mar 03, 2017 12:17 pm

Junaid_Shahid wrote:
Fri Mar 03, 2017 12:16 pm
Robinson wrote:
Fri Mar 03, 2017 12:16 pm
hello guys!!..

can anyone teach me how motion detection done in matlab??
thanks..

or is there anyone who can give his or her code for that,,

Thank You..
You have to detect motion in some video or how? kindly elaborate your problem
the scenario is..
i will use a camera to human and then detect the human in the scene,,
by image subtraction..

how it process in matlab?

Junaid_Shahid
Posts:110
Joined:Tue Feb 14, 2017 9:00 am

Re: Motion Detection in Matlab

Post by Junaid_Shahid » Fri Mar 03, 2017 12:18 pm

Robinson wrote:
Fri Mar 03, 2017 12:17 pm
Junaid_Shahid wrote:
Fri Mar 03, 2017 12:16 pm
Robinson wrote:
Fri Mar 03, 2017 12:16 pm
hello guys!!..

can anyone teach me how motion detection done in matlab??
thanks..

or is there anyone who can give his or her code for that,,

Thank You..
You have to detect motion in some video or how? kindly elaborate your problem
the scenario is..
i will use a camera to human and then detect the human in the scene,,
by image subtraction..

how it process in matlab?
This is called block based matching technique. You have to divide the whole image in blocks. Then you have to compare sucessive blocks for any change with respect to some threshold which you ll set according to your application. If there is difference between successive blocks then motion is present otherwise not. I have done this thing couple of years ago.

Robinson
Posts:12
Joined:Fri Mar 03, 2017 11:06 am

Re: Motion Detection in Matlab

Post by Robinson » Fri Mar 03, 2017 12:19 pm

Junaid_Shahid wrote:
Fri Mar 03, 2017 12:18 pm
Robinson wrote:
Fri Mar 03, 2017 12:17 pm
Junaid_Shahid wrote:
Fri Mar 03, 2017 12:16 pm


You have to detect motion in some video or how? kindly elaborate your problem
the scenario is..
i will use a camera to human and then detect the human in the scene,,
by image subtraction..

how it process in matlab?
This is called block based matching technique. You have to divide the whole image in blocks. Then you have to compare sucessive blocks for any change with respect to some threshold which you ll set according to your application. If there is difference between successive blocks then motion is present otherwise not. I have done this thing couple of years ago.
es,,i think its image subtraction..im planning to use that..but my problem is the algorithm that i will use in matlab..can you help me to make a code here..thank you

Junaid_Shahid
Posts:110
Joined:Tue Feb 14, 2017 9:00 am

Re: Motion Detection in Matlab

Post by Junaid_Shahid » Fri Mar 03, 2017 12:20 pm

Robinson wrote:
Fri Mar 03, 2017 12:19 pm
Junaid_Shahid wrote:
Fri Mar 03, 2017 12:18 pm
Robinson wrote:
Fri Mar 03, 2017 12:17 pm


the scenario is..
i will use a camera to human and then detect the human in the scene,,
by image subtraction..

how it process in matlab?
This is called block based matching technique. You have to divide the whole image in blocks. Then you have to compare sucessive blocks for any change with respect to some threshold which you ll set according to your application. If there is difference between successive blocks then motion is present otherwise not. I have done this thing couple of years ago.
es,,i think its image subtraction..im planning to use that..but my problem is the algorithm that i will use in matlab..can you help me to make a code here..thank you
1- First of all you have to make sure that Video is read by Matlab. I do not know in which format you will make video with your camera. But I have done it with 'avi' format. In matlab command of 'aviread' will be used to read the video. you can search matlab help to find commands for other video formats. Lets for example you have saved your videofile(avi format) with name of 'filename' on your computer. Then how you will load it in matlab for further processing,here are some commands which will help you(you can see their details with matlab help)

video=aviread(’filename.avi’): read avi video into Matlab;
movie(video): play the movie with the file name ’video’
length(video): total number of frames in the video:
I=frame2im(video(j)): extract the jth frame from video to image I
size(I): determine the size of I, i.e., number of rows, columns, color or gray image
rgb2gray: convert an rgb image to a gray-scale image.
imwrite(I,filename,’bmp’): e.g. filename=’frame5’, then image named as ’frame5.bmp’ will be saved to the harddisk.

What you have achieved uptill now,you have loaded your 'Video'in matlab,played it via matlab. Then you have divided the whole video into frames and extracted each frame into image and saved it. Now you are ready to do further processing on these images to check the presence on motion.

2- Now load 2 consecutive images that you have saved in Step 1. Assigned the 2 images as 'Iold' and 'Inew' names, respectively.

Compute the difference between the 2 consecutive image frames (Idiff = Inew − Iold). Apply a fixed threshold to the difference image (i.e. Initially set a threshold value, e.g. th=50/255(or you can set any other threshold according to your needs)for image value ranges [0,1]). Set zeros to those pixels whose values are below the threshold (i.e. if |Idiff(i, j)| < threshold, then Idiff(i, j) = 0.0).

Divide the image area into rectangular blocks (size 16×16 or any size you want).Within each block indexed as (j, i), if there is one 'Idiff' value within the block containing the motion, then the block is assigned as a motion block, otherwise, it is a non motion block. Repeat this process over all blocks. Generate a
map image (same size as the original image) for indicating motion blocks and name it as ”Imotion”:for those pixels within the motion blocks, assign image values as 1.0, otherwise 0.0.

To search the whole image you can use the follwoing syntaxthe pixels within the block, having the block indexes (j,i), are
[(j − 1) ∗ blocksize + 1 : j ∗ blocksize, (i − 1) ∗ blocksize + 1 : i ∗ blocksize].
Check sum(sum(Idiff[ (j-1)*blocksize+1:j*blocksize, (i-1)*blocksize+1: i*blocksize]) for the motion inside the block.
Matlab functions useful to read image fiels are,

imread: read an image from a file
mat2gray: convert an image, from the pixel value range: [Imin,Imax] to a matrix image with a range [0.0 1.0].(Note: converting between image formats is very important for Matlab image processing.)When usng mat2gray, the image pixels extracted from video are usually 8 bits unsigned char (range between 0-255), while most Matlab processing requires pixels in float/double format. There are many other functions for image format conversion, e.g. ind2gray, gray2ind, rgb2gray. You can use Matlab’s help to find out more.

Now to check:Display 3 images: the 2 (original) consecutive images, and the motion image ”Imotion”. Observe the motion blocks, and change the threshold value th until the motion blocks indeed include all prominent changes in the image frames.

Plot ”Imotion” obtained in above step and observe whether or not the motion blocks cover most motion areas in the original image: if not, then change the threshold th value until ”Imotion” shows satisfactory motion areas. Its an hit an trial method. you have to do it untill u r satisifed with results.

So you have detected any motion in your Video using Matlab through Image subtraction(block based) technique.

Hope it help you.
Source:
MOTION DETECTION IN MATLAB

Post Reply