Explanation of `cv.findHomography`

Viewed 725

I am trying to find the homography between two sets of points using opencv. I have source src and destination dst and I am computing the homography H a follows.

import numpy as np
import cv2

src = np.array([[-47.59, -57.43, 139.59, 149.43, 46.],
       [141.43, 31.03, -1.43, 108.97, 70.],
       [579.23, 412.75, 422.55, 589.03, 500.89]])


dst = np.array([[-100, -100, 100, 100, 0],
       [-100, 100, 100, -100, 0],
       [0, 0, 0, 0, 0]])


H, _ = cv2.findHomography(src.T, dst.T)

print(np.allclose(H@src, dst))

OpenCV is computing the homography but when I try recovering the original dst using the computed H, it doesn't work. np.allclose(H@src, dst) gives False

However, if I take a closer look at the two arrays, the following is what I have.

The top is H@src and bottom is dst

There is a clear correspondence between the two sets of values. I would be almost be comfortable saying that they are just off by a scaling factor if it wasn't for the last row. Clearly a scaling factor of zero doesn't make sense. What am I missing here?

1 Answers

Since, a 3x3 homography matrix computes the transformation between sets of 2D points between two spaces, having 0 in the last components of dst would be an issue. This is because src and dst must be a set of 2D points in homogeneous representation. Hence, having 0 would make these points at infinity.

Maybe you are looking for something like this:

import cv2
import numpy as np

if __name__ == '__main__':
    src = np.array([[-47.59, -57.43, 139.59, 149.43, 46.],
                    [141.43, 31.03, -1.43, 108.97, 70.],
                    [579.23, 412.75, 422.55, 589.03, 500.89]])

    dst = np.array([[-100, -100, 100, 100, 0],
                    [-100, 100, 100, -100, 0],
                    [1, 1, 1, 1, 1]])  # converting to homogeneous coordinates

    H, _ = cv2.findHomography(src.T, dst.T)
    dst_est = H @ src
    print(np.allclose(dst_est / (dst_est[2, :]), dst / dst[2, :], atol=1e-2))  # dividing by last component to fix the scaling and adjusting the tolerance

Related