title: Rotation and Position Tracking with OpenCV and Optical Flow
body: <p>I would like to track the rotation and translation of an object in OpenCV using Optical Flow. So far I've got something like this:</p>
  
  <ol>
  <li>Call goodFeaturesToTrack to find initial features   </li>
  <li>Call calcOpticalFlowPyrLK to track the movement of feature points   </li>
  <li>Call findHomography to find how the points in image A moved to image B </li>
  <li>Call perspectiveTransform to move points based on the homography </li>
  <li>Call solvePnPRansac to find the rotation matrix and translation
  vector</li>
  </ol>
  
  <p>At this point I'm trying to take the difference between the rotation and translation Mat between images and add them to an initial Rotation and Translation Matrix. </p>
  
  <pre><code>cv::solvePnPRansac(pattern.points3d, _points2d, calibration.getIntrinsic(), calibration.getDistorsion(), raux, taux);
  raux.convertTo(Rvec, CV_32F);
  taux.convertTo(Tvec, CV_32F);
  
  cv::Mat_&lt;float&gt; rotMat(3, 3);
  cv::Rodrigues(Rvec, rotMat);
  cv::Mat_&lt;float&gt; transDiff   = _prevTranslation - Tvec;
  cv::Mat_&lt;float&gt; rotDiff     = _prevRotation    - rotMat;
  _absRotation                += rotDiff;
  _absTranslation             += transDiff;
  </code></pre>
  
  <p>The problem with this approach is translation vector doesn't follow the images. The vector tends to stay in the range
  [0.02 0.2 -1.5]</p>
  
  <p>It doesn't stray far from this position.</p>
  
  <p>Thanks.</p>