計算出透視變換的變換矩陣之后,准備對一組關鍵點進行變換,以得到關鍵點變換之后的位置。
先是采用如下函數計算,結果錯誤。
def transform_points(points, trans_mat):
points_expand = np.ones((len(points), 3), dtype=np.float32)
points_expand[:, 0:2] = points
new_points = np.matmul(trans_mat, points_expand.T)
new_points = new_points.T
return new_points[:, 0:2]
后來網上查到可以用cv2.perspectiveTransform 進行變換。但是變換前,一定對數據進行如下整理:
points = points.reshape(1, -1, 2).astype(np.float32) #二維變三維, 整形轉float型, 一個都不能少
new_points = cv2.perspectiveTransform(points, trans_mat)