How to compute Lucas Kanade flow
<p>I am currently working on a project of object tracking and have used c++ , opencv . I have succesfully used Farneback dense optical flow to implement segmentation methods such as k means (using the displacement in each frame) . Now i want to do the same thing with Lucas Kanade sparse method. But the output of this function is : </p>

<p>nextPts – output vector of 2D points (with single-precision floating-point coordinates) containing the calculated new positions of input features in the second image; when OPTFLOW_USE_INITIAL_FLOW flag is passed, the vector must have the same size as in the input. </p>

<p>(as stated in the official site) </p>

<p>My question is <strong>how i am going to get the result to a Mat flow</strong> for example. I have so far tried  : </p>

<p>// Implement Lucas Kanade algorithm </p>

<pre><code>cvCalcOpticalFlowPyrLK(frame1_1C, frame2_1C, pyramid1, pyramid2,
frame1_features, frame2_features, number_of_features,
optical_flow_window, 5, optical_flow_found_feature,
optical_flow_feature_error, optical_flow_termination_criteria,
0);
 // Calculate each feature point's coordinates in every frame 
 CvPoint p,q;
 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;
// Creating the arrows for imshow

angle = atan2((double) p.y - q.y, (double) p.x - q.x);
hypotenuse = sqrt(square(p.y - q.y) + square(p.x - q.x));

/* Here we lengthen the arrow by a factor of three. */

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);

p.x = (int) (q.x + 9 * cos(angle + pi / 4));
p.y = (int) (q.y + 9 * sin(angle + pi / 4));
cvLine(frame1, 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(frame1, p, q, line_color, line_thickness, CV_AA, 0);

allocateOnDemand(&amp;framenew, frame_size, IPL_DEPTH_8U, 3);
cvConvertImage(frame1, framenew, CV_CVTIMG_FLIP);

cvShowImage("Optical Flow", framenew);
</code></pre>

<p>This is the optical flow presentation. Any ideas how I should get a Mat flow similar to the result of Farneback optical flow ? </p>

<p>(<a href="http://docs.opencv.org/2.4/modules/video/doc/motion_analysis_and_object_tracking.html#calcopticalflowfarneback" rel="nofollow">http://docs.opencv.org/2.4/modules/video/doc/motion_analysis_and_object_tracking.html#calcopticalflowfarneback</a> ) </p>

<p><strong>UPDATE</strong> : Very good answer. But now i have problems with showing the kmeans image. With farneback i used : </p>

<pre><code>cv::kmeans(m, K, bestLabels,
                TermCriteria( CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 1.0),
                3, KMEANS_PP_CENTERS, centers);
        int colors[K];
        for (int i = 0; i &lt; K; i++) {
            colors[i] = 255 / (i + 1);
        }

        namedWindow("Kmeans", WINDOW_NORMAL);

        Mat clustered = Mat(flow.rows, flow.cols, CV_32F);

        for (int i = 0; i &lt; flow.cols * flow.rows; i++) {
            clustered.at&lt;float&gt;(i / flow.cols, i % flow.cols) =
                    (float) (colors[bestLabels.at&lt;int&gt;(0, i)]);
        }
        clustered.convertTo(clustered, CV_8U);
        imshow("Kmeans", clustered);
</code></pre>

<p>Any ideas ? ?</p>