Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Motion Detection in Matlab

Status
Not open for further replies.

akoangsimula

Member level 5
Joined
Aug 3, 2010
Messages
84
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Location
Batangas - Malvar
Activity points
1,763
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..
 

Hi,

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

Hi,
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.

Hope it helps.
/SC
 
yes,,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
 

What is your profile knowledge of this tool ?

+++
 

Hi,

ok here you go:

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.

/SC
 
  • Like
Reactions: labz

    labz

    Points: 2
    Helpful Answer Positive Rating
    V

    Points: 2
    Helpful Answer Positive Rating
akoangsimula said:
SUPER thank you..
it helps me a lot..
i will do it step by step.
by the way if i have question can i ask you?
this is our final thesis by the way
:)
GodBless

Hi,

Yes you can ask anything you want to but here in this thread and i would be more than happy to help you.

/SC
 

hi , im currently working on a project that includes image processing. details of the project is , i have to connect my usb camera with laptop and then track humans in real time with the help of two servo motors . im doing this all on matlab. the motors are connected to arduino board.
can any one help me with coding in matlab ?
 

Hi,

Yes you can ask anything you want to but here in this thread and i would be more than happy to help you.

/SC


hi..
im having a hard time doing my project,,the scenario is this:
in a close room i will choose the person that will follow by the camera..how it done in matlAB?
 

Hi,

ok here you go:

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.

/SC
please help me asap .. its my project and i did not get it :( :(
 

The proper procedure required to accomplish the task was sufficiently explained above.



+++
 

i did not get that from where the idiff(i,j) .. from where come the i,j if only the difference value of inew-iold store in idiff only?

- - - Updated - - -

The proper procedure required to accomplish the task was sufficiently explained above.



+++
i did not get that from where the idiff(i,j) .. from where come the i,j if only the difference value of inew-iold store in idiff only?
 

The proper procedure required to accomplish the task was sufficiently explained above.



+++

it gave me an error at this line
ERROR:
Subscript indices must either be real positive integers or logicals.

Error in imp (line 4)
I=frame2im(video(j));% extract the jth frame from video to image I
 

I can suggest you perform a search on link bellow, applying some variants keywords on query:



+++
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top