crash when trying to use opencv CalcOpticalFlowLK

Hi all,
I’m trying to use the opencv function CalcOpticalFlowLK but am getting a crash and can’t find why.

I started with the zip supplied here
which takes input from the camera, converts to greyscale, differences with prev frame etc.
I modified the VectorField class to have 2x linear arrays, u & v to store the horizontal and vertical values (instead of a 2 dimensional array of ofxVec2f) - tested it and worked fine - so no problems there.

Then in the update function I replaced the convertMotionHistoryToField function with the opticalflow function of opencv, making my update function:

prev = grey;  
color.setFromPixels(camera.getPixels(), camera.width, camera.height);  
color.mirror(false, true);  
grey = color;  
cvCalcOpticalFlowLK(grey.getCvImage()->imageData, prev.getCvImage()->imageData, cvSize, motionField.u, motionField.v);  

From what i can figure by looking around this should be correct, but I get a crash straight away with no useful info in the crash log… does anyone have any ideas what it could be?

(I’m on mac)

I found somewhere, that actually the void* in the function definition are not actually raw pixel and velocity data, but are IplImage* - and for the velocities they are single channel float. So I changed the code to:

IplImage* u;  
IplImage* v;  
CvSize cameraSize;  
cameraSize.width = camera.width;  
cameraSize.height = camera.height;  
u = cvCreateImage(cameraSize, IPL_DEPTH_32F, 1);   
v = cvCreateImage(cameraSize, IPL_DEPTH_32F, 1);   


prev = grey;  
color.setFromPixels(camera.getPixels(), camera.width, camera.height);  
grey = color;  
grey.mirror(false, true);  
cvCalcOpticalFlowLK(grey.getCvImage(), prev.getCvImage(), win_size, u, v);  

but still no luck! :S
Has anyone have any experience with this function? or similar?

I found this thread and the class there works fine… I don’t know why mine wasn’t working… but these classes saved the day! thanks takashi!