OpenCV optical flow

Hi, I started writing some opencv addon code.
1st, I wrote code using opticalFlowLK and put these on Google Code repository.
Its not unfinished, but It will be useful for openFrameWorks geeks !

I have some experience using openCV for working.
So I’ll add another optical flow (opticalFlowBM) and more computer vision exantion code.

pls check it.

Codes are here.

if you familiar with SVN, you can check out this command.

svn checkout of-computervision

*excellent* work.
will port it in the direction of the Mac and try to post when it’s ready.

thanks again,


Thank you for your attention !
If finished your porting for mac and get google code account, could I add you as a member of committer ? ,or please send me its code. How about this plan ?

I’m looking forward to your working !

Today just implemented opticalFlow BM.
check up it.

great stuff - looking forward to checking it out.

thanks again for your effort with this


btw, i accidentally pulled the screen off (!!) my development laptop at the weekend so i’ll need to you know, buy a new computer before getting much further! :slight_smile:

Hey All,

While I was waiting to fix my libdc1394 problems (see this post) I downloaded takashi’s code and converted it to the latest version of OF (added all the helper classes to the ofxOpenCv addon, and converted the syntax with a few search and replaces), as well as converting it to OSX.

Please find the examples and the updated addon here:

(I chopped out the .app files and also the extraneous addons)

Thanks for posting it all in the first place Takashi!



great class!

just for boosting the performance with the camera I’ve changed following in takashis class:
(The framerate of my application raised from ~40fps to ~45fps.)

void update()  
         if(bPauseVideo ) return;  
           //do not calculate if there is no new frame  
           if (!camera.isFrameNew()) return;  

this is awesome.

cvFindTrackPointExample is the coolest thing ive ever seen.

is there any more information available about what exactly its doing?


I’m working with optical flow a while ago. And I used to use this class for motion segmentation. I found it is useful if we can extract the ‘vector’ information. By then, we can use simple thresholding to wipe out a range of flow vectors which has very big or very small magnitude. Or maybe we are interested to a specific range of angles.

I also take out the ‘flow parameters’ for the Lucas-Kanade optical flow in opencv, so maybe, we can initialize (load) the parameters from an xml file. I will give a try on this.

Please check out the example on LK optical flow in here: Maybe you would like to compare it to the code implemented by takashi. (mine is slower though… since I implemented the pyramid version of LK)



I come from a Flash background and I have a stupid question, but please could someone just get me started on how to reference the vector information in order to get actual pixel changes?

From the CvOpticalFlowBM example here is the update() function:

void testApp::update(){  
        pastImg = grayImg;  
        cameraImg.setFromPixels(vidGrabber.getPixels(), cw, ch);  
        //printf("%p %p\n",pastImg.getCvImage() ,grayImg.getCvImage());  
		opticalFlow.calc(pastImg, grayImg,10);  

Please could someone shed some light on how to get changes in the pixel data of the opticalFlow object so that I can visualise them in the cameraImg object?

My initial idea was to use the vector info and ‘move’ an overlaid object around the camera window using a bit of inertia.

If I need to set up listeners and broadcast events or do something else - just a brief insight would be fantastic.

Many many thanks - awesome piece of work.

Hi Rick, no listeners or broadcasts required. You can use cvGetReal2D to get the velocity at any coordinate:

float horizontalVelocityAtXY = cvGetReal2D( opticalFlow.vel_x, y, x );  
float verticalVelocityAtXY = cvGetReal2D( opticalFlow.vel_y, y, x );  

so you can do a for loop and look at the velocities at every pixel, or every other pixel etc.
P.S. the range of x,y is the same range as the dimensions of your input image (capture dimentions).