# Amount of movement

I’m curious about this: how can i detect the amount of movement of somebody on a video capture. I mean, if somebody is moving slowly, you have “low movement” but if she is, for example, shaking her arms, you have “big movement”.

Did anybody get into this before?

Of course, it is not as simple as count the number of blobs on the screen, 'cause it won’t give me the idea of how much are they changing.

Tnx, F.[/i]

One of the easiest ways to do this is to store the previous frame and then when you get your new frame go through it and for each pixel compare it to the pixel of the previous frame.

When the difference is more than a set threshold value you can then count it as a changed pixel. The total number of changed pixels can be used as an indication of the amount of motion/movement in the frame.

something like this (using a modified videoGrabber example):

``````

//in testApp.h
unsigned char * oldPixels;
int motionCount;
int threshold;

//in testApp.cpp setup function
oldPixels = new unsigned char[320*240*3];

//in testApp.cpp update function
vidGrabber.grabFrame();

if(vidGrabber.isFrameNew())
{
motionCount = 0;
threshold = 90;
int totalPixels = 320*240*3;
unsigned char * pixels = vidGrabber.getPixels();

for (int i = 0; i < totalPixels-3; i+= 3){
//calculate the total pixel diff for red green and blue added together
int diff = abs(pixels[i] - oldPixels[i])
+ abs(pixels[i+1] - oldPixels[i+1])
+ abs(pixels[i+2] - oldPixels[i+2]);
if(diff >= threshold)motionCount++;
}

//copy the current image to the old image
memcpy(oldPixels, pixels, totalPixels);
printf("motionCount is %i \n", motionCount);
}

``````

This example uses a color image - but obviously you can do it with a greyscale image and also a binary thresholded image after a background subtraction as well.

Theo

Yeah… perhaps that’s a good start point. The problem is that i need to measure the “amount” of movement. With the retina approach i just know if there are differences, but i don’t know if the people are moving a lot or not. Anyway, it will give me information. Thanks.

No actually - the above method gives you a very good indication of the amount of movement in the scene.

If the motionCount variable is low then you have little movement and the higher the motionCount is the more movement in the scene.

Anyway try it out and see if it works for you

: )

Theo

Sorry, Theo, i misunderstood your example. It works great!

Thanks, F.

nice!!

I will just quickly add that you might want to take a look at the opencv code, specifically the cvGrayscaleImage class which has some features that might be helpful for motion detection, specifically:

absDiff(), calculate the absolute difference between two images
= operator, to copy the currentFrame into a previous frame
countNonZeroInRegion, which counts non-black pixels

– zach