14

I am trying to calculate scale, rotation and translation between two consecutive frames of a video. So basically I matched keypoints and then used opencv function findHomography() to calculate the homography matrix.
homography = findHomography(feature1 , feature2 , CV_RANSAC); //feature1 and feature2 are matched keypoints

My question is: How can I use this matrix to calculate scale, rotation and translation?.
Can anyone provide me the code or explanation as to how to do it?

Angie Quijano
  • 3,308
  • 3
  • 20
  • 30
Lakshya Kejriwal
  • 1,190
  • 4
  • 15
  • 27
  • 1
    the keyword is "homography decomposition". Afair you can extract the rotation with a QR decomposition, but you should better google that... – Micka Sep 04 '14 at 07:31
  • 1
    maybe this one (or its links) will help: http://stackoverflow.com/questions/15420693/how-to-get-rotation-translation-shear-from-a-3x3-homography-matrix-in-c-sharp – Micka Sep 04 '14 at 07:36
  • 1
    and this one =) http://hal.archives-ouvertes.fr/docs/00/17/47/39/PDF/RR-6303.pdf – Micka Sep 04 '14 at 07:38
  • 1
    This is a complex problem, but this answer explains it well: http://stackoverflow.com/questions/7388893/extract-transform-and-rotation-matrices-from-homography?rq=1 You should try to get a deeper understanding of how the Homography matrix works. By doing so you'll also learn the pros and cons. You should also look into other kinds of transforms as affine transform and rigid transform. If they can solve your problem, they are easier to use. – Øystein W. Sep 04 '14 at 09:11

6 Answers6

6

if you can use opencv 3.0, this decomposition method is available http://docs.opencv.org/3.0-beta/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#decomposehomographymat

Vineet
  • 140
  • 1
  • 6
  • 2
    are we talking about rotation in plane or in 3D. It seems that the question was about 2D in plane rotation and the answer is about 3D. – Vlad Oct 15 '15 at 04:04
1

Given Homography matrix H:

    |H_00, H_01, H_02|
H = |H_10, H_11, H_12|
    |H_20, H_21, H_22|

Assumptions:

H_20 = H_21 = 0 and normalized to H_22 = 1 to obtain 8 DOF.

The translation along x and y axes are directly calculated from H:

tx = H_02
ty = H_12

The 2x2 sub matrix on the top left corner is decomposed to calculate shear, scaling and rotation. An easy and quick decomposition method is explained here.

Note: this method assumes invertible matrix.

Färid Alijani
  • 428
  • 4
  • 16
  • The translation in 3D cannot be recovered completely. – Vlad Oct 15 '15 at 04:10
  • You are talking about affine matrix, which is different to homography matrix. – hillin Jun 14 '18 at 04:08
  • 1
    How did you assume that first and second components of the third row are zero? In `cv::find_homography()` I do not get such values for two consecutive frames. – Färid Alijani Jun 19 '19 at 09:50
  • I believe the assumption that `H_20 = H_21 = 0 ` and `H_22 = 1 ` means there are only 6 degrees of freedom. Not sure if this is exactly an affine matrix, but I think it is. – Erotemic May 20 '21 at 23:25
1

The right answer is to use homography as it is defined dst = H ⋅ src and explore what it does to small segments around a particular point.

Translation

Given a single point, for translation do

T = dst - (H ⋅ src)

Rotation

Given two points p1 and p2

p1 = H ⋅ p1

p2 = H ⋅ p2

Now just calculate the angle between vectors p1 p2 and p1' p2'.

Scale

You can use the same trick but now just compare the lengths: |p1 p2| and |p1' p2'|.

To be fair, use another segment orthogonal to the first and average the result. You will see that there is no constant scale factor or translation one. They will depend on the src location.

legends2k
  • 27,643
  • 22
  • 108
  • 196
Vlad
  • 4,223
  • 1
  • 28
  • 38
0

For estimating a tree-dimensional transform and rotation induced by a homography, there exist multiple approaches. One of them provides closed formulas for decomposing the homography, but they are very complex. Also, the solutions are never unique.

Luckily, OpenCV 3 already implements this decomposition (decomposeHomographyMat). Given an homography and a correctly scaled intrinsics matrix, the function provides a set of four possible rotations and translations.

Emiswelt
  • 3,651
  • 1
  • 30
  • 52
0

The question seems to be about 2D parameters. Homography matrix captures perspective distortion. If the application does not create much perspective distortion, one can approximate a real world transformation using affine transformation matrix (that uses only scale, rotation, translation and no shearing/flipping). The following link will give an idea about decomposing an affine transformation into different parameters.

https://math.stackexchange.com/questions/612006/decomposing-an-affine-transformation

Vinmean
  • 53
  • 1
  • 7
0

Since i had to struggle for a couple of days to create my homography transformation function I'm going to put it here for the benefit of everyone.

Here you can see the main loop where every input position is multiplied by the homography matrix h. Then the result is used to copy the pixel from the original position to the destination position.

    for (tempIn[0] = 0; tempIn[0] < stride; tempIn[0]++)
    {
        for (tempIn[1] = 0; tempIn[1] < rows; tempIn[1]++)
        {
            double w = h[6] * tempIn[0] + h[7] * tempIn[1] + 1; // very important!
            //H_20 = H_21 = 0 and normalized to H_22 = 1 to obtain 8 DOF. <-- this is wrong

            tempOut[0] = ((h[0] * tempIn[0]) + (h[1] * tempIn[1]) + h[2])/w;
            tempOut[1] =(( h[3] * tempIn[0]) +(h[4] * tempIn[1]) + h[5])/w;


            if (tempOut[1] < destSize && tempOut[0] < destSize && tempOut[0] >= 0 && tempOut[1] >= 0)
                dest_[destStride * tempOut[1] + tempOut[0]] = src_[stride * tempIn[1] + tempIn[0]];
        }
    }

After such process an image with some kind of grid will be produced. Some kind of filter is needed to remove the grid. In my code i have used a simple linear filter.

Note: Only the central part of the original image is really required for producing a correct image. Some rows and columns can be safely discarded.

deight
  • 261
  • 4
  • 15