将vector<Point2f>传递给getAffineTransform函数

4
我正在尝试计算视频中两个连续帧之间的仿射变换。因此,我已经找到了这两帧中的特征点,并得到了它们的匹配点。
    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吗? - Christophe
不,这不止三个。 - Lakshya Kejriwal
那就是问题所在:它期望每个点都恰好有3个。 - Christophe
那么如何计算超过3个点的方式呢? - Lakshya Kejriwal
2个回答

3
你的问题是需要在图像之间精确匹配3个点。 如果你有超过3个匹配点,你应该优化变换以适应所有匹配点(除了异常值)。 因此,我建议你看一下findHomography()函数(http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#findhomography)。 它计算匹配点之间的透视变换,并且至少需要4个匹配点。 因为你有超过3个匹配点,而仿射变换是透视变换的子集,所以这对你来说应该是合适的。 该函数的另一个优点是能够检测异常值(不符合变换和其他点的匹配点),并且这些点不会被考虑在变换计算中。 总之,使用findHomography(features1 , features2, CV_RANSAC)代替getAffineTransform(features1 , features2)。 希望我能帮到你。

findHomography() 返回的矩阵是否包含了缩放、旋转和平移? - Lakshya Kejriwal
是的,它会包含它们! - Dennis

2

是的,两个向量的大小都超过了3。现在我该怎么做? - Lakshya Kejriwal
好的,你需要将每个向量缩小为包含三个点。从你提供的代码中我看不出需要改变什么。要么你自己修复它,要么更新你的问题并提供更多的代码,这样我才能看一下。 - Øystein W.
我更新了代码。基本上,我想要得到两个图像之间的变换矩阵(缩放+旋转+平移)。 - Lakshya Kejriwal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接