I’m trying to create homography matrix from this field:
The points for the source points are:
pts_src = [[ 761, 704], [ 910, 292], [1109, 544], [ 619, 479], [ 656, 373 ], [1329, 446], [ 20, 559], [ 87, 664], [ 238, 501], [ 399, 450]]
And the points for destination points (new image):
pts_dst = [[147, 330], [ 35 , 20], [147, 225], [ 75, 203], [ 35, 155], [147, 155], [ 35, 317], [ 75, 351], [ 35, 237], [ 35, 203]]
I tried to create homography matrix with the following code:
import numpy as np pts_src = np.array(pts_src) pts_dst = np.array(pts_dst) h, status = cv2.findHomography(pts_src, pts_dst) print(h) #homography matrix
And I got the following homography matrix:
[[ 4.00647822e-01 1.41196305e+00 -6.90548584e+02] [-1.28068526e-01 3.03783700e+00 -6.98945354e+02] [ 3.12182175e-04 4.06980322e-03 1.00000000e+00]]
I tried to check if the homography matrix is correct, so I used the first coordinate from the source image (761, 704)
, and check if I get the correct coordinate in destination image, which should be (147, 330)
. I tried to use the equation new_x, new_y, new_z = h*(x,y,z)
:
p = [761, 704, 1] print(np.matmul(h, p))
And I got:
array([ 608.36639573, 1342.23174648, 4.1027121 ])
Which is very far (and for some reason the z
is 4.1
).
And I also tried the second point (910, 292)
, and I should get (35 , 20)
, but I got [86.33414416, 71.5606917 , 2.47246832]
.
Any idea why I’m not able to get the correct coordinates?
Advertisement
Answer
The solution was mentioned in the comments thanks to @fana – dividing the x
and y
coordinates by z
(x/z, y/z)
.