将vector <point2f>传递给getAffineTransform </point2f>

时间:2014-08-14 08:59:16

标签: c++ opencv transformation

我试图计算视频中两个连续帧之间的仿射变换。所以我找到了这些功能并得到了两帧中的匹配点。

    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

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

2 个答案:

答案 0 :(得分: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) 我希望我能帮到你。

答案 1 :(得分:2)

当我从你的代码和断言中读到时,你的向量有问题。

int checkVector(int elemChannels,int depth) //

如果矩阵是1通道(N x ptdim)或ptdim通道(1 x N)或(N x 1),则此函数返回N;否则为负数。

根据文件; http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html#getaffinetransform:从三对对应点计算仿射变换。

你的一个或两个向量中似乎有多于或少于三个点。

相关问题