我正在尝试计算视频中两个连续帧之间的仿射变换。所以我找到了特征并在两帧中得到了匹配点。

    FastFeatureDetector detector;

    vector<Keypoints> frame1_features;
    vector<Keypoints> frame2_features;

    detector.detect(frame1 , frame1_features , Mat());
    detector.detect(frame2 , frame2_features , Mat());

    vector<Point2f> features1;  //matched points in 1st image
    vector<Point2f> features2;  //matched points in 2nd image

    for(int i = 0;i<frame2_features.size() && i<frame1_features.size();++i )
    {

            double diff;
            diff = pow((frame1.at<uchar>(frame1_features[i].pt) - frame2.at<uchar>(frame2_features[i].pt)) , 2);

            if(diff<SSD)    //SSD is sum of squared differences between two image regions
            {
                feature1.push_back(frame1_features[i].pt);
                feature2.push_back(frame2_features[i].pt);
            }
    }

    Mat affine = getAffineTransform(features1 , features2);

最后一行给出了以下错误:
    OpenCV Error: Assertion failed (src.checkVector(2, CV_32F) == 3 && dst.checkVector(2, CV_32F) == 3) in getAffineTransform

有人可以告诉我如何使用两帧之间的一组匹配点来计算仿射变换吗?

最佳答案

您的问题是图像之间恰好需要 3 个点对应。
如果您有 3 个以上的对应关系,则应优化转换以适应所有对应关系(异常值除外)。
因此,我建议查看 findHomography() -function ( http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#findhomography )。
它计算对应关系之间的透视变换,并且至少需要 4 个点对应关系。
因为您有 3 个以上的对应关系,并且仿射变换是透视变换的一个子集,所以这应该适合您。
该函数的另一个优点是它能够检测异常值(不适合变换和其他点的对应关系),并且不考虑这些异常值进行变换计算。
总而言之,使用 findHomography(features1 , features2, CV_RANSAC) 而不是 getAffineTransform(features1 , features2)
我希望我能帮助你。

关于c++ - 将 vector<Point2f> 传递给 getAffineTransform,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25304049/

10-13 08:33