从2D坐标获取3D坐标

时间:2020-05-14 12:38:29

标签: opencv image-processing

我的目标是从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)

使用myRvecmyTvec可以将世界坐标转换为图像坐标,如下所示:

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)

这是我的输出: enter image description here

为什么我错过棋盘的右侧以及如何纠正呢?

0 个答案:

没有答案