Motion Detection


I am trying to implement motion detection into my app by comparing previous frames with the current frame. How would I be able to do that in OF?

I am trying to implement the article in this webpage:…-ction.aspx
by Andrew Kirillov.

He’s using a filter sequence as well to eliminate noise. Most of his code extends from the framework for image processing.

Any help would be appreciated.

Maybe this is helpful - motion detection -> vector field.

But you could just check out the motion detection part (which just does an absolute pixel difference between the current frame and previous frame and then sets pixels that have changed more than a certain amount to white and all other pixels to black).

hope that helps

Thanks for the link Theo!

It was very useful. I was going through this and was wondering if this is similar to the optical flow method that is in OpenCV?

Hi guys, I’m currently looking for pretty much the same thing - an optical flow implementation that converts video into a vector field, but the method explained in that link looks brilliant! I tried the zip there but couldn’t compile (on mac), got a missing “cv.h” and hundreds of other errors (probably related).

So I’ve started from scratch, and have the frame differencing with fade etc. working, just need to now run through and create the vector field from the gradients. I was just wondering based on your experience - what gave the best results? Doing the differencing and accumulating at relatively high res, then scaling down a lot, and creating the vectors for each pixel from only one neighboring pixel? Or not scaling down lot, but only creating vectors for every x pixels, and creating the vectors averaging the surrounding x pixels (or is that actually the same math :S).

P.S. On Mac, there is a really good implementation of Lucas Kanade in Quartz Composer as Core Image which does simply video -> vector field, and I was looking for ideally something like this directly in openFrameworks, I don’t think openCV for mac currently has a Lucas Kanade or Horn-Schunk so this seems like a good alternative.

hey - on mac you would just need to duplicate the addonsAll example and then replace the src folder with the src folder in the download zip and the data folder with the data folder in the zip - then it should run fine on mac.


Hi Theo, yes indeed it does! I’ve got the algorithm working in processing brilliantly thanks! (only because i’d started this project in processing a while ago) - i’m porting it to openFrameworks, and developing it at the same time… and this method seems quite light and brilliant! many thanks! It looks like it could quite easily be ported to GLSL as well…

So what I understand about the example is that openCV does all the frame differencing. but that happenes if I just want to get a value of how many pixels changed compared to the previous frame. Is there something implemented allready or would I have to pull this off myself?



openCV will tell you the difference between two frames, you threshold the difference to get a black & white (binary) image. From there you can do gradients & vectors if you want to go more advanced.

If you want to know in your image how many pixels are different, take your threshold image and count the number of white pixels.

The function countNonZeroInRegion() for ofCvGreyscaleImage would do this.

Thanks chris! That seems to be the easiest way for motion detection I could imagine!

Does anyone here know how to isolate a movement vector? For example, I only need to isolate movements only in the positive x direction or negative x direction. I am trying to use make a page flipping interactive setup that flips the page forwards and backwards using hand movements.

For example, a user might want to keep flipping the pages forward but in order to do that, he has to swing his hands from right to left (1 cycle) and back to right to left (2nd cycle), to flip 2 pages. So the problem is in between cycles, how do I know that the user wants to flip the pages forward twice and not backwards?

I’m not sure if i have explained this clearly so bear with me… :lol: