Optical Flow Arrow Tips Pointing the Wrong Way

This is an extremely trivial question but it kind of bugs me. I modified the optical flow algorithm here to use the entire video and display it: http://robots.stanford.edu/cs223b05/notes/CS%20223-B%20T1%20stavens_opencv_optical_flow.pdf

I extract the optical flow correctly and it shows the arrows and everything but when the motion is from left to right the arrows are pointing to left rather than to right. Actually arrows are always pointing to left and it doesn't change. I tried changing the angle equation below, it doesn't change anything. I posted the original arrow calculation and drawing code below and I hope someone can help.

CvPoint p,q; /*  "p" is the point where the line begins.
/*  "q" is the point where the line stops.
/*  "CV_AA" means antialiased drawing.
/*  "0" means no fractional bits in the center cooridinate or radius.
*/
p.x = (int) frame1_features[i].x;
p.y = (int) frame1_features[i].y;
q.x = (int) frame2_features[i].x;
q.y = (int) frame2_features[i].y;
double angle;
angle = atan2( (double) p.y - q.y, (double) p.x - q.x );
double hypotenuse;  hypotenuse = sqrt( square(p.y - q.y) + square(p.x - q.x) )
q.x = (int) (p.x - 3 * hypotenuse * cos(angle));
q.y = (int) (p.y - 3 * hypotenuse * sin(angle));
cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 );
/* Now draw the tips of the arrow.  I do some scaling so that the
* tips look proportional to the main line of the arrow.
*/
p.x = (int) (q.x + 9 * cos(angle + pi / 4));
p.y = (int) (q.y + 9 * sin(angle + pi / 4));
cvLine( frame, p, q, line_color, line_thickness, CV_AA, 0 );
p.x = (int) (q.x + 9 * cos(angle - pi / 4));
p.y = (int) (q.y + 9 * sin(angle - pi / 4));
cvLine( frame, p, q, line_color, line_thickness, CV_AA, 0 );
}

edit retag close merge delete

the code seems right could u provide the video on which u applied optical flow

( 2012-12-05 14:11:04 -0600 )edit

The video is called 861-13_70.mov in UCF Crowd Dataset

( 2012-12-13 07:08:51 -0600 )edit

Isn't the mistake in your atan2 call ? If your vector is PQ, then you should have some Q-P almost everywhere.

( 2012-12-28 14:47:44 -0600 )edit

Sort by » oldest newest most voted

the code is ok, i was facing the same problem and the cause was that i was passing wrong images in to the calcOpticalFlowPyrLK() function. According to the documentation the images should be 8-bit (i.e. grayscale) both of them, so check if this is the problem or not. In my case i was passing rgb images, while the input points were from a binary.

more