使用 OpenCV Python 计算相机世界位置 [英] Calculate camera world position with OpenCV Python

查看:89
本文介绍了使用 OpenCV Python 计算相机世界位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算我的相机在世界坐标中的位置.这应该相当容易,但我没有得到我期望的结果.我相信我已经阅读了有关此主题的所有内容,但是我的代码无法正常工作.这就是我所做的:

I want to calculate my camera's position in world coordinates. This should be fairly easy, but I don't get the results I expect. I believe I've read everything on this topic, but my code isn't working. Here's what I do:

我有一个摄像头在观察一个区域.

I have a camera looking at an area.

1) 我画了一张该地区的地图.

1) I drew a map of the area.

2) 我使用 cv2.getPerspectiveTransform

3) H单应性将每个世界坐标转换为相机坐标;这工作正常

3) The H homography transforms every world coordinate to camera coordinate; this is working properly

4) 为了计算我遵循的相机矩阵 这个:

4) To calculate the camera matrix I followed 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,相机的位置应该这样计算:

5) According to this, the camera's position should be calculated like this:

x,y,z = np.dot(-np.transpose(rotation),translation)

我得到的坐标完全错误.我猜问题应该在第 4 步或第 5 步中的某个地方.我的方法有什么问题?

The coordinates I'm getting are totally wrong. The problem should be somewhere in step 4 or 5 I guess. What's wrong with my method?

推荐答案

我想我现在明白了.问题在于步骤 4 中描述的方法.不能仅从单应矩阵计算相机位置.相机内在矩阵也是必要的.因此,正确的程序如下:

I think I've got it now. The problem was with the method described in step 4. The camera position cannot be calculated from the homography matrix alone. The camera intrinsics matrix is also necessary. So, the correct procedure is the following:

1) 绘制区域地图

2) 使用棋盘图像和 cv2.findChessboardCorners 校准相机,这会产生相机矩阵和失真系数

2) calibrate the camera using the chessboard image with cv2.findChessboardCorners this yields the camera matrix and the distortion coefficients

3) 使用世界坐标 (3D) 和图像坐标 (2D) 求解 PnP.在给定 4 个对应点和相机矩阵的情况下,solvePnP 在相机坐标系中返回对象的原点.

3) solvePnP with the world coordinates (3D) and image coordinates (2D). The solvePnP returns the object's origo in the camera's coordinate system given the 4 corresponding points and the camera matrix.

4) 现在我需要计算相机在世界坐标中的位置.旋转矩阵为:rotM = cv2.Rodrigues(rvec)[0]

4) Now I need to calculate the camera's position in world coordinates. The rotation matrix is: rotM = cv2.Rodrigues(rvec)[0]

5) 相机的 x,y,z 位置为:cameraPosition = -np.matrix(rotM).T * np.matrix(tvec)

5) The x,y,z position of the camera is: cameraPosition = -np.matrix(rotM).T * np.matrix(tvec)

这篇关于使用 OpenCV Python 计算相机世界位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆