OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Thu, 03 Jan 2013 08:28:38 -0600Optical Flow Arrow Tips Pointing the Wrong Wayhttp://answers.opencv.org/question/4792/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.
Thanks in advance
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 );
}
Sun, 02 Dec 2012 06:50:44 -0600http://answers.opencv.org/question/4792/optical-flow-arrow-tips-pointing-the-wrong-way/Comment by Emmanuel for <p>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: <a href="http://robots.stanford.edu/cs223b05/notes/CS%20223-B%20T1%20stavens_opencv_optical_flow.pdf">http://robots.stanford.edu/cs223b05/notes/CS%20223-B%20T1%20stavens_opencv_optical_flow.pdf</a></p>
<p>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.</p>
<p>Thanks in advance</p>
<pre><code>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 );
}
</code></pre>
http://answers.opencv.org/question/4792/optical-flow-arrow-tips-pointing-the-wrong-way/?comment=5557#post-id-5557Isn't the mistake in your atan2 call ? If your vector is PQ, then you should have some Q-P almost everywhere.Fri, 28 Dec 2012 14:47:44 -0600http://answers.opencv.org/question/4792/optical-flow-arrow-tips-pointing-the-wrong-way/?comment=5557#post-id-5557Comment by gpsinghsandhu for <p>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: <a href="http://robots.stanford.edu/cs223b05/notes/CS%20223-B%20T1%20stavens_opencv_optical_flow.pdf">http://robots.stanford.edu/cs223b05/notes/CS%20223-B%20T1%20stavens_opencv_optical_flow.pdf</a></p>
<p>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.</p>
<p>Thanks in advance</p>
<pre><code>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 );
}
</code></pre>
http://answers.opencv.org/question/4792/optical-flow-arrow-tips-pointing-the-wrong-way/?comment=4931#post-id-4931the code seems right could u provide the video on which u applied optical flowWed, 05 Dec 2012 14:11:04 -0600http://answers.opencv.org/question/4792/optical-flow-arrow-tips-pointing-the-wrong-way/?comment=4931#post-id-4931Comment by aliciadominica for <p>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: <a href="http://robots.stanford.edu/cs223b05/notes/CS%20223-B%20T1%20stavens_opencv_optical_flow.pdf">http://robots.stanford.edu/cs223b05/notes/CS%20223-B%20T1%20stavens_opencv_optical_flow.pdf</a></p>
<p>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.</p>
<p>Thanks in advance</p>
<pre><code>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 );
}
</code></pre>
http://answers.opencv.org/question/4792/optical-flow-arrow-tips-pointing-the-wrong-way/?comment=5143#post-id-5143The video is called 861-13_70.mov in UCF Crowd DatasetThu, 13 Dec 2012 07:08:51 -0600http://answers.opencv.org/question/4792/optical-flow-arrow-tips-pointing-the-wrong-way/?comment=5143#post-id-5143Answer by theodore for <p>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: <a href="http://robots.stanford.edu/cs223b05/notes/CS%20223-B%20T1%20stavens_opencv_optical_flow.pdf">http://robots.stanford.edu/cs223b05/notes/CS%20223-B%20T1%20stavens_opencv_optical_flow.pdf</a></p>
<p>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.</p>
<p>Thanks in advance</p>
<pre><code>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 );
}
</code></pre>
http://answers.opencv.org/question/4792/optical-flow-arrow-tips-pointing-the-wrong-way/?answer=5677#post-id-5677the code is ok, i was facing the same problem and the cause was that i was passing wrong images in to the [calcOpticalFlowPyrLK()](http://docs.opencv.org/modules/video/doc/motion_analysis_and_object_tracking.html) 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.Thu, 03 Jan 2013 08:28:38 -0600http://answers.opencv.org/question/4792/optical-flow-arrow-tips-pointing-the-wrong-way/?answer=5677#post-id-5677