题目如下图:
我求出了单应矩阵h,然后使用了图中的公式二求目标点(xi,yi)位求原图中的点位(ui,vi)。在验证结果时,手动测量的四个点位与目标点位的映射关系是正确的,但是输入其他范围内的点却会出现不正确的结果,如原图点位(-194,1930)这种超出原图范围的点。看来半夜也没有找到哪里错哪儿了,恳请大家知道哪里出错了,指点一下
代码如下:
import numpy as np
import cv2
u1, v1 = 9, 121
u2, v2 = 184, 71
u3, v3 = 79, 388
u4, v4 = 316, 286
x1, y1 = 0, 0
x2, y2 = 210, 0
x3, y3 = 210, 298
x4, y4 = 0, 298
"""
Ah=B
h=A(逆)B
"""
A=[
[x1,y1,1,0,0,0,-u1x1,-u1y1],
[0,0,0,x1,y1,1,-v1x1,-v1y1],
[x2,y2,1,0,0,0,-u2x2,-u2y2],
[0,0,0,x2,y2,1,-v2x2,-v2y2],
[x3,y3,1,0,0,0,-u3x3,-u3y3],
[0,0,0,x3,y3,1,-v3x3,-v3y3],
[x4,y4,1,0,0,0,-u4x4,-u4y4],
[0,0,0,x4,y4,1,-v4x4,-v4y4]
]
B=[
[u1],
[v1],
[u2],
[v2],
[u3],
[v3],
[u4],
[v4]
]
A = np.array(A)
B = np.array(B)
A_inv = np.linalg.inv(A)
h = A_inv @ B
def get_u(x,y):
u = (h[0][0]x+h[1][0]y+h[2][0])/(h[6][0]x+h[7][0]y+1)
v = (h[3][0]x+h[4][0]y+h[5][0])/(h[6][0]x+h[7][0]y+1)
return (int(u),int(v))
image= np.zeros((420,596,3),dtype=np.uint8)
img = cv2.imread("img/test.jpg")
print(get_u(int(200),int(200)))
cv2.imshow("test",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
你给出的坐标(u3,v3) (u4,v4)好像是反的. 修正后(200,200)是对的.
不过你说的超出范围的还是不对,你可以关注一下分母,当式(2)分母 <= 0时候,应该是有特殊的物理意义的.