Hey I am trying to compute a perspective transformation using openCV.
My code is as follows:
original_pts = np.array([[745, 1233], [1505, 1581], [1502, 1212], [855, 1229]], dtype=np.float32)
desired_pts = np.array([[46., 50.], [949., 490.], [949., 50.], [175., 50.]], dtype=np.float32)
M = cv2.getPerspectiveTransform(original_pts, desired_pts)
cv2.perspectiveTransform(np.float32(original_pts[None, :, :]), M)[0]
The output is:
array([[ 46., 50.],
[ 0., 0.],
[ 949., 50.],
[ 175., 50.]], dtype=float32)
For some reason the second point is just very wrong
Trying to debug I implemented the linear system myself:
x = original_pts[:, 0]
y = original_pts[:, 1]
xt = desired_pts[:, 0]
yt = desired_pts[:, 1]
A = []
for i in range(4):
equation = [x[i], y[i], 1, 0, 0, 0, 0, 0]
equation += [0] * i
equation += [-xt[i]]
equation += [0] * (3 - i)
A.append(equation)
equation = [0, 0, 0, x[i], y[i], 1, 0, 0]
equation += [0] * i
equation += [-yt[i]]
equation += [0] * (3 - i)
A.append(equation)
equation = [0, 0, 0, 0, 0, 0, x[i], y[i]]
equation += [0] * i
equation += [-1]
equation += [0] * (3 - i)
A.append(equation)
A = np.array(A)
b = np.array([0]*8 + [-1]*4)
res = np.linalg.solve(A, b)
my_M = np.array([[res[0], res[1], res[2]], [res[3], res[4], res[5]], [res[6], res[7], 1]])
cv2.perspectiveTransform(np.float32(original_pts[None, :, :]), my_M)[0]
Which yields a better approximation:
array([[ 47.653404, 51.79718 ],
[ 975.5118 , 503.6889 ],
[ 949. , 50. ],
[ 175.0361 , 50.036106]], dtype=float32)
Does anyone have any idea, whether I’m using cv2.getPerspectiveTransfrom
wrongly or does it simply not work for some problems? I thought I’d basically reimplemented a non-optimized version of the function