opencvimage-processingopencv3.0image-stabilization

Which of types Mat or vector<Point2f> is better to use with function estimateRigidTransform()?


As known, we can pass to the function estimateRigidTransform() two parameters with one of two types: Mat estimateRigidTransform(InputArray src, InputArray dst, bool fullAffine)

  1. cv::Mat frame1, frame2;
  2. std::vector<cv::Point2f> frame1_features, frame2_features;

I.e., for example, to implement video-stabilization (shake remove) we can use one of two approach:

  1. with cv::Mat: video stabilization using opencv
cv::Mat frame1 = imread("frame1.png");
cv::Mat frame2 = imread("frame2.png");
Mat M = estimateRigidTransform(frame1, frame2, 0);
warpAffine(frame2, output, M, Size(640,480), INTER_NEAREST|WARP_INVERSE_MAP);
  1. with std::vector<cv::Point2f> features;
vector <uchar> status;
vector <float> err;

std::vector <cv::Point2f> frame1_features, frame2_features;
cv::Mat frame1 = imread("frame1.png");
cv::Mat frame2 = imread("frame2.png");
goodFeaturesToTrack(frame1 , frame1_features, 200, 0.01, 30);
goodFeaturesToTrack(frame2 , frame2_features, 200, 0.01, 30);
calcOpticalFlowPyrLK(frame1 , frame2, frame1_features, frame2_features, status, err);

std::vector <cv::Point2f> frame1_features_ok, frame2_features_ok;
for(size_t i=0; i < status.size(); i++) {
 if(status[i]) {
  frame1_features_ok.push_back(frame1_features[i]);
  frame2_features_ok.push_back(frame2_features[i]);
 }
}

Mat M = estimateRigidTransform(frame1_features_ok, frame2_features_ok, 0);
warpAffine(frame2, output, M, Size(640,480), INTER_NEAREST|WARP_INVERSE_MAP);

Which of these approach is better to use, and why?

I.e. which of types Mat or vector<Point2f> is better to use with function estimateRigidTransform()?


Solution

  • In the first case OpenCV will perform implicitly a calcOpticalFlowPyrLK() inside the function estimateRigidTransform(). See the implementation in lkpyramid.cpp @ line 1383.

    This is the only difference between the two methods. If finding correspondences between frame1 and frame2 matters then use version #2 otherwise #1.