3

I'm looking for a way to calculate only the translation and scale between two images using opencv. I've only encountered a rigid transform implementation which bundles the scale information with the rotation, which I'm not interested in.

How would you advise I do it?

Notice, in the following code the two surpassingly equal angles are not, actually angle2 = 2 * angle. which I'm not sure why:

    cv::Mat rigid_mat = cv::estimateRigidTransform(prevMatGray, thisMatGray, false);
    float tx = rigid_mat.at<float>(0,2);
    float ty = rigid_mat.at<float>(1,2);

    float a = rigid_mat.at<float>(0,0);
    float b = rigid_mat.at<float>(0,1);

    float c = rigid_mat.at<float>(1,0);
    float d = rigid_mat.at<float>(1,1);

    float sX = [BTImageProcessing sign:a] * sqrtf(powf(a,2) + powf(b,2));
    float sY = [BTImageProcessing sign:d] * sqrt(powf(c,2) + powf(d,2));
    float alpha = atan2f(c, d);
    float alpha2 = atan2f(-b, a);


+ (int)sign:(float)num{
    return num > 0 ? 1 : -1;
}
vondip
  • 13,809
  • 27
  • 100
  • 156

1 Answers1

5

You can use estimateRigidTransform() to compute an optimal affine transformation [A|b] (a 2x3 floating-point matrix) between two 2D point sets. Next, you should decompose the transformation matrix into scaling and translation, like here (sx, sy, xc, yc) and don't care with rotation.

https://i.stack.imgur.com/rhsh8.png

Community
  • 1
  • 1
Kornel
  • 5,264
  • 2
  • 21
  • 28
  • I tried doing so, but form some reason the decomposed values are incorrect. for example, atan2(c,d) != atan2(-b,a). – vondip Apr 08 '15 at 09:49
  • also, following documentation here it appears as though -sx and sy are flipped. will let you know if that's indeed the case http://docs.opencv.org/modules/video/doc/motion_analysis_and_object_tracking.html#Mat%20estimateRigidTransform%28InputArray%20src,%20InputArray%20dst,%20bool%20fullAffine%29 – vondip Apr 08 '15 at 09:50
  • Don't really understand, an enlargement using a negative scale factor is similar to an enlargement using a positive scale factor, but this time the image is on the other side of the centre of enlargement, and it is upside down. But in general `sign(a) = sign(sx)` and `sign(b) = sign(sy)` due to the nature of the cosine function. – Kornel Apr 08 '15 at 10:19
  • well, the angle aren't a match, so I'm not sure what could be causing it – vondip Apr 08 '15 at 11:06
  • Ok, seems as though I've been using float instead of double. This causes an overflow with unexpected values – vondip Apr 08 '15 at 11:42