4

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?

4
  • And the size of both vector that you submit is 3 ?
    – Christophe
    Commented Aug 14, 2014 at 9:13
  • No it's more than 3. Commented Aug 14, 2014 at 13:29
  • That's the problem: it expects exactly 3 points in each
    – Christophe
    Commented Aug 14, 2014 at 14:50
  • So what's the way for calculating it for more than 3 points? Commented Aug 14, 2014 at 15:26

2 Answers 2

3


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.

2
  • Will matrix returned by findHomography() contain scale+rotation+translation? Commented Aug 14, 2014 at 15:47
  • Yes, it will contain them!
    – Dennis
    Commented Aug 14, 2014 at 15:54
2

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.

3
  • Yes,the size of both vectors is more than 3. How should I go about it now? Commented Aug 14, 2014 at 13:29
  • Well, you have to reduce each vector to contain three points. I can not see what you need to change from the code you have provided. Either you have to fix it yourself, or update your question with more code so I can have a look. Commented Aug 14, 2014 at 14:10
  • I updated the code. So basically I want the transformation(scale + rotation + translation) matrix between the two images Commented Aug 14, 2014 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.