简体   繁体   English

将vector <Point2f>传递给getAffineTransform

[英]Pass vector<Point2f> to getAffineTransform

I'm trying to calculate affine transformation between two consecutive frames from a video. 我正在尝试计算视频中两个连续帧之间的仿射变换。 So I have found the features and got the matched points in the two frames. 所以我找到了这些功能并得到了两帧中的匹配点。

    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);

The last line gives the following error : 最后一行给出以下错误:

    OpenCV Error: Assertion failed (src.checkVector(2, CV_32F) == 3 && dst.checkVector(2, CV_32F) == 3) in getAffineTransform

Can someone please tell me how to calculate the affine transformation with a set of matched points between the two frames? 有人可以告诉我如何使用两帧之间的一组匹配点计算仿射变换?


Your problem is that you need exactly 3 point correspondences between the images. 您的问题是您需要在图像之间准确地进行3点对应。 If you have more than 3 correspondences, you should optimize the transformation to fit all the correspondences (except of outliers). 如果您有超过3个对应关系,则应优化转换以适应所有对应关系(异常值除外)。
Therefore, I recommend to take a look at findHomography() -function ( http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#findhomography ). 因此,我建议看一下findHomography()http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#findhomography )。
It calculates a perspective transformation between the correspondences and needs at least 4 point correspondences. 它计算对应关系之间的透视变换,并且至少需要4个点对应关系。
Because you have more than 3 correspondences and affine transformations are a subset of perspective transformations, this should be appropriate for you. 因为你有3个以上的对应关系,仿射变换是透视变换的一个子集,所以这应该适合你。
Another advantage of the function is that it is able to detect outliers (correspondences that do not fit to the transformation and the other points) and these are not considered for transformation calculation. 该功能的另一个优点是它能够检测异常值(不符合变换和其他点的对应关系),并且这些不被考虑用于变换计算。

To sum up, use findHomography(features1 , features2, CV_RANSAC) instead of getAffineTransform(features1 , features2) . 总而言之,使用findHomography(features1 , features2, CV_RANSAC)而不是getAffineTransform(features1 , features2)
I hope I could help you. 我希望我能帮助你。

As I read from your code and assertion, there is something wrong with your vectors. 当我从你的代码和断言中读到时,你的向量有问题。

int checkVector(int elemChannels,int depth) //

this function returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel (1 x N) or (N x 1); 如果矩阵是1通道(N x ptdim)或ptdim通道(1 x N)或(N x 1),则此函数返回N; negative number otherwise. 否则为负数。

And according to the documentation; 并根据文件; http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html#getaffinetransform : Calculates an affine transform from three pairs of the corresponding points. http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html#getaffinetransform :从三对对应点计算仿射变换。

You seem to have more or less than three points in one or both of your vectors. 你的一个或两个向量中似乎有多于或少于三个点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM