简体   繁体   中英

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. If you have more than 3 correspondences, you should optimize the transformation to fit all the correspondences (except of outliers).
Therefore, I recommend to take a look at findHomography() -function ( 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.
Because you have more than 3 correspondences and affine transformations are a subset of perspective transformations, this should be appropriate for you.
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) .
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); 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.

You seem to have more or less than three points in one or both of your vectors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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