opencv - 如何确定相机的世界坐标?

标签 opencv pose-estimation

我在墙上有一个尺寸和位置已知的矩形目标,在机器人上有一个移动摄像头。当机器人在房间内行驶时,我需要定位目标并计算相机的位置及其姿态。作为进一步的改进,可以使用伺服系统改变摄像机的仰角和方位角。我能够使用 OpenCV 定位目标,但我在计算相机的位置时仍然很模糊(实际上,上周我的头撞到墙上,我的额头上有一个扁平点)。这是我正在做的:

  1. 读入先前计算的相机内在文件
  2. 从轮廓中获取目标矩形的4个点的像素坐标
  3. 使用矩形的世界坐标、像素坐标、相机矩阵和畸变矩阵调用 solvePnP
  4. 使用旋转和平移向量调用 projectPoints
  5. ???

我已经阅读了 OpenCV 这本书,但我想我只是遗漏了一些关于如何使用投影点、旋转和平移向量来计算相机的世界坐标及其姿势的内容(我不是数学天才) :-(

2013-04-02 按照“morynicz”的建议,我编写了这个简单的独立程序。

#include <Windows.h>
#include "opencv\cv.h"

using namespace cv;

int main (int argc, char** argv)
{
const char          *calibration_filename = argc >= 2 ? argv [1] : "M1011_camera.xml";
FileStorage         camera_data (calibration_filename, FileStorage::READ);
Mat                 camera_intrinsics, distortion;
vector<Point3d>     world_coords;
vector<Point2d>     pixel_coords;
Mat                 rotation_vector, translation_vector, rotation_matrix, inverted_rotation_matrix, cw_translate;
Mat                 cw_transform = cv::Mat::eye (4, 4, CV_64FC1);


// Read camera data
camera_data ["camera_matrix"] >> camera_intrinsics;
camera_data ["distortion_coefficients"] >> distortion;
camera_data.release ();

// Target rectangle coordinates in feet
world_coords.push_back (Point3d (10.91666666666667, 10.01041666666667, 0));
world_coords.push_back (Point3d (10.91666666666667, 8.34375, 0));
world_coords.push_back (Point3d (16.08333333333334, 8.34375, 0));
world_coords.push_back (Point3d (16.08333333333334, 10.01041666666667, 0));

// Coordinates of rectangle in camera
pixel_coords.push_back (Point2d (284, 204));
pixel_coords.push_back (Point2d (286, 249));
pixel_coords.push_back (Point2d (421, 259));
pixel_coords.push_back (Point2d (416, 216));

// Get vectors for world->camera transform
solvePnP (world_coords, pixel_coords, camera_intrinsics, distortion, rotation_vector, translation_vector, false, 0);
dump_matrix (rotation_vector, String ("Rotation vector"));
dump_matrix (translation_vector, String ("Translation vector"));

// We need inverse of the world->camera transform (camera->world) to calculate
// the camera's location
Rodrigues (rotation_vector, rotation_matrix);
Rodrigues (rotation_matrix.t (), camera_rotation_vector);
Mat t = translation_vector.t ();
camera_translation_vector = -camera_rotation_vector * t;

printf ("Camera position %f, %f, %f\n", camera_translation_vector.at<double>(0), camera_translation_vector.at<double>(1), camera_translation_vector.at<double>(2));
printf ("Camera pose %f, %f, %f\n", camera_rotation_vector.at<double>(0), camera_rotation_vector.at<double>(1), camera_rotation_vector.at<double>(2));
}

我在测试中使用的像素坐标来自于目标矩形(宽 62 英寸,高 20 英寸)左侧约 27 英尺处以约 45 度角拍摄的真实图像。输出不是我所期望的。我做错了什么?

Rotation vector
2.7005
0.0328
0.4590

Translation vector
-10.4774
8.1194
13.9423

Camera position -28.293855, 21.926176, 37.650714
Camera pose -2.700470, -0.032770, -0.459009

如果我的世界坐标的 Y 轴与 OpenCV 屏幕 Y 轴的 Y 轴相反,会不会有问题? (我的坐标系的原点在目标左边的地板上,而 OpenCV 的原点是屏幕的左上角)。

姿势的单位是什么?

最佳答案

您从 solvePnP 获得平移和旋转向量,它告诉相机坐标中的对象在哪里。您需要进行逆变换。

变换相机 -> 对象可以写成齐次坐标的矩阵 [R T;0 1]。该矩阵的逆将是,使用它的特殊属性,[R^t -R^t*T;0 1] 其中 R^t 是 R 转置。你可以从 Rodrigues 得到 R 矩阵转换。这样你就可以得到变换对象->相机坐标的平移向量和旋转矩阵。

如果你知道对象在世界坐标中的位置你可以使用世界->对象变换*对象->相机变换矩阵来提取相机平移和姿势。

姿势由单个向量或 R 矩阵描述,您肯定会在您的书中找到它。如果是“Learning OpenCV”,您会在第 401 - 402 页找到它:)

看看你的代码,你需要做这样的事情

    cv::Mat R;
    cv::Rodrigues(rotation_vector, R);

    cv::Mat cameraRotationVector;

    cv::Rodrigues(R.t(),cameraRotationVector);

    cv::Mat cameraTranslationVector = -R.t()*translation_vector;

cameraTranslationVector 包含相机坐标。 cameraRotationVector 包含相机姿势。

关于opencv - 如何确定相机的世界坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15727527/

相关文章:

python - 我遇到了这个错误(-215 :Assertion failed) ! ssize.empty() in function 'resize' in OpenCV

php - 如何为 PHP 安装 OpenCV?

opencv - 如何找到增强现实的相机矩阵?

c++ - OpenCV solvePnP 方法返回 NaN 值

matlab - 使用n点进行姿势估计的稳定性

python - 估计矩形对象的姿势不稳定

Python 只向 Arduino 发送一次信号

c++ - OpenCV Traincascade.exe 在没有输出的情况下崩溃

python - 隔离视频流中的彩色文本区域

opencv - 查找两个相机框架之间的位移