我想计算我的相机在世界坐标系中的位置。这应该很容易,但我得到的结果与我期望的不同。我相信我已经阅读了关于这个主题的所有内容,但我的代码并没有起作用。这是我所做的:
我有一个看着一个区域的相机。
1)我绘制了该区域的地图。
2)我使用cv2.getPerspectiveTransform
将4个图像点与我的地图上的4个点匹配来计算单应性矩阵H。
3)单应性矩阵H将每个世界坐标转换为相机坐标;这部分正常工作。
4)为了计算相机矩阵,我遵循了this的方法:
translation = np.zeros((3,1))
translation[:,0] = homography[:,2]
rotation = np.zeros((3,3))
rotation[:,0] = homography[:,0]
rotation[:,1] = homography[:,1]
rotation[:,2] = np.cross(homography[0:3,0],homography[0:3,1])
cameraMatrix = np.zeros((3,4))
cameraMatrix[:,0:3] = rotation
cameraMatrix[:,3] = homography[:,2]
cameraMatrix = cameraMatrix/cameraMatrix[2][3] #normalize the matrix
5) 根据this,摄像机的位置应该按照以下方式计算:
x,y,z = np.dot(-np.transpose(rotation),translation)
我得到的坐标完全错误。问题应该是在第4或第5步,我想知道我的方法哪里出了问题。