我的目标是从2D(图像)坐标中计算3D(世界)坐标。为此,我使用棋盘计算了本征矩阵(对于相机)和非本征矩阵(对于特定图像)。假设我已将相机矩阵保存在mtx
中,将失真系数保存在dist
中。此外,对于9x6的棋盘,棋盘ID 25
的单个正方形的大小为mm。我已经使用solvePnP
来获取外部矩阵:
sq_size = 25
rows = 9
cols = 6
objp = np.zeros((cols * rows,3), np.float32)
objp[:,:2] = np.mgrid[0:rows,0:cols].T.reshape(-1,2)
objp = objp*sq_size
retval,myRvec,myTvec=cv2.solvePnP(objectPoints=objp,
imagePoints=corners.reshape(-1, 2),
cameraMatrix=mtx,
distCoeffs=dist)
使用myRvec
和myTvec
可以将世界坐标转换为图像坐标,如下所示:
axis = np.float32([[1,0,0], [0,1,0], [0,0,-1]]).reshape(-1,3)*sq_size
imgpts, jac = cv2.projectPoints(axis, myRvec, myTvec, mtx, dist)
现在,我想撤消该过程;给定图像坐标,我想计算世界坐标,该怎么做?
修改 我使用以下方法找到了单应性:
pts_dst = np.zeros((cols * rows,2), np.float32)
pts_dst[:,:2] = np.mgrid[0:rows,0:cols].T.reshape(-1,2)
pts_dst = pts_dst*sq_size
pts_src, jac = cv2.projectPoints(objp, myRvec, myTvec, mtx, dist)
pts_src = np.squeeze(pts_src)
h, status = cv2.findHomography(pts_src, pts_dst)
transformed = np.zeros((img.shape[0], img.shape[1]), dtype=np.uint8);
dst = cv2.warpPerspective(img, h, transformed.shape)
为什么我错过棋盘的右侧以及如何纠正呢?