Ask Your Question
0

Calculate affine transformation matrix

asked 2016-06-11 00:38:53 -0600

kevgeo gravatar image

updated 2016-06-11 05:15:43 -0600

I want to build a function that can estimate geometric transform i.e calculate the transformation matrix required to convert first image into second image.

So my second image is obtained in two steps,

  1. Resize the first image to 0.7 in both x & y directions
  2. Rotate the resized image at an angle of 31 degrees.

So I need use the getAffineTransform function to calculate the transformation matrix, but the function doesn't take Mat objects as parameters. So i decided to find the best matched features and stored in a Dmatch object.

But now I am confused as to how to pass this as parameters to the getAffineTransform function as it requires Point2f type? Or is it possible to use Mat object as parameters to this function?

//queryidx- keypoints1
//traindidx-  keypoints2
vector<Point2f> a,b;

for(int i=0;i<good_matches.size();i++)
{
a.push_back( keypoints_1[good_matches[i].queryIdx].pt );
b.push_back( keypoints_2[good_matches[i].trainIdx].pt );
}

Mat M = getAffineTransform( Mat(a), Mat(b)  );
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-06-11 01:14:00 -0600

berak gravatar image

updated 2016-06-11 05:27:20 -0600

since you already know the angle, you could easily use getRotationMatrix2D:

double angle = 31.0;
double scale = 1.0;
Point2f center(img.cols/2, img.rows/2);
Mat M = getRotationMatrix2D(center, angle, scale);

if you want to derive it from (exactly 3) pairs of Points via getAffineTransform, use vector<Point2f>, which can be casted to a cv::Mat:

vector<Point2f> a,b;
a.push_back(keypoints_a[i].pt); // extract the point from the keypoints;
b.push_back(keypoints_b[j].pt); // you will need **exactly** 3 points in a and 3 points in b
...
Mat M = getAffineTransform(Mat(a), Mat(b));

finally, rotate it:

Mat rotated;
warpAffine(img, rotated, M, Size());

good luck !

edit flag offensive delete link more

Comments

Hey Berak, thanks a lot for the help. Now my only problem is that I'm getting this execution error -

OpenCV Error: Assertion failed (src.checkVector(2, CV_32F) == 3 && dst.checkVector(2, CV_32F) == 3) in getAffineTransform, file /home/kevmepls/opencv-2.4.9/modules/imgproc/src/imgwarp.cpp, line 3929 terminate called after throwing an instance of 'cv::Exception' what(): /home/kevmepls/opencv-2.4.9/modules/imgproc/src/imgwarp.cpp:3929: error: (-215) src.checkVector(2, CV_32F) == 3 && dst.checkVector(2, CV_32F) == 3 in function getAffineTransform

Aborted (core dumped)

Do you know why it is causing error? Also I used KeyPoint instead of Point2ffor vectors 'a' and 'b' as my keypoints was of this datatype. source code

kevgeo gravatar imagekevgeo ( 2016-06-11 04:36:23 -0600 )edit

if i understand you right, - no, you have to extract the Point2f from the Keypoints, you can't use the Keypoints "as is" (and that's what the error is complaining about)

berak gravatar imageberak ( 2016-06-11 04:42:17 -0600 )edit

I just corrected it to Point2f, but I'm still getting the same error. You can check out the last lines of my code which does this.

kevgeo gravatar imagekevgeo ( 2016-06-11 04:46:57 -0600 )edit

please put your code here(append to your question), not somewhere else ;)

berak gravatar imageberak ( 2016-06-11 04:51:01 -0600 )edit

oops sorry, just edited.

kevgeo gravatar imagekevgeo ( 2016-06-11 05:16:05 -0600 )edit

don't be sorry ;)

and the answer is simple, getAffineTransform() wants exactly 3 points each, not more not less.

berak gravatar imageberak ( 2016-06-11 05:22:15 -0600 )edit
1

Oh alright, it works now. Thanks a lot berak, can't thank you enough :)

kevgeo gravatar imagekevgeo ( 2016-06-11 05:34:01 -0600 )edit

had some fun doing this, so - pleasure !

berak gravatar imageberak ( 2016-06-11 05:36:36 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-11 00:38:53 -0600

Seen: 4,516 times

Last updated: Jun 11 '16