相机旋转解决方案 [英] Camera Rotation SolvePnp

查看:236
本文介绍了相机旋转解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我进行数学计算的相机Y轴旋转吗? 我使用SolvePnP获取rvec.通过这个rvec,我想知道我的相机绕其自身的Y轴旋转了多少.

Could somebody help me with the math on calculating the Y rotation of my camera? I use SolvePnP to get a rvec. From this rvec I want to know how much my camera is rotated around its own Y axis.

PointF[] ImagePointsCam = GetImagePointsCam(imgThres);
MCvPoint3D32f[] ObjectPointsCam = GetObjectPointsCam();
float f = 1.0f / (float)(Math.Tan(CameraFOV / 360.0 * 3.1415));
Matrix<float> CamMat = new Matrix<float>(3, 3);
CamMat.Data[0, 0] = (float)(f * 0.5 * ResolutionWidth);
CamMat.Data[0, 1] = 0;
CamMat.Data[0, 2] = (float)(ResolutionWidth / 2);
CamMat.Data[1, 0] = 0;
CamMat.Data[1, 1] = (float)(f * 0.5 * ResolutionHeight);
CamMat.Data[1, 2] = (float)(ResolutionHeight / 2);
CamMat.Data[2, 0] = 0;
CamMat.Data[2, 1] = 0;
CamMat.Data[2, 2] = 1;
Mat DistMat = new Mat();
Mat rvec = new Mat();
Mat tvec = new Mat();
CvInvoke.SolvePnP(ObjectPointsCam, ImagePointsCam, CamMat, DistMat, rvec, tvec, false, SolvePnpMethod.Iterative);
Mat R = new Mat();
CvInvoke.Rodrigues(rvec, R);

我不确定如何继续操作,以便获得代表相机Y轴旋转角度的单个值.

I'm not sure how to continue so that I can get a single value which represents the Y rotation of the camera.

编辑

我设法通过DllImport和@ Tahera.T的答案使其在C#中工作.现在,我得到一个错误,该错误与我为该函数提供的变量有关.有人可以看看我是否做对了吗?

I managed to get it working in C# with DllImport and the answer of @Tahera.T. Now I get an error which I think has to do with the variables that I provide for the function. Could somebody have a look if I do it the right way?

[DllImport("opencv_world320.dll")]

private static extern void cvDecomposeProjectionMatrix(Mat projMatr, out Mat calibMatr, 
out Mat rotMatr, out Mat posVect, out Mat rotMatrX, out Mat rotMatrY, out Mat rotMatrZ,
out MCvPoint3D64f eulerAngles);

public MCvPoint3D64f GetEulerAngles(Mat R)
{
    Matrix<float> _r = new Matrix<float>(3,4);
    Matrix<float> _R = new Matrix<float>(R.Rows, R.Cols);
    R.CopyTo(_R);
    _r.Data[0, 0] = _R.Data[0, 0];
    _r.Data[0, 1] = _R.Data[0, 1];
    _r.Data[0, 2] = _R.Data[0, 2];
    _r.Data[0, 3] = 0;
    _r.Data[1, 0] = _R.Data[1, 0];
    _r.Data[1, 1] = _R.Data[1, 1];
    _r.Data[1, 2] = _R.Data[1, 2];
    _r.Data[1, 3] = 0;
    _r.Data[2, 0] = _R.Data[2, 0];
    _r.Data[2, 1] = _R.Data[2, 1];
    _r.Data[2, 2] = _R.Data[2, 2];
    _r.Data[2, 3] = 0;
    Mat projMatr = _r.Mat;

    cvDecomposeProjectionMatrix(projMatr, out Mat calibMatr, out Mat rotMatr, out Mat posVect, 
    out Mat rotMatrX, out Mat rotMatrY, out Mat rotMatrZ, out MCvPoint3D64f eulerAngles);

    return eulerAngles;
}

我现在得到的错误是:

System.Runtime.InteropServices.SEHException: 'External component has thrown an exception.'

带有错误代码:

-2147467259

我用错误的变量调用该函数吗?

Do I call the function with the wrong variables?

推荐答案

Solvepnp不提供实际的旋转值,它用于获取真实世界的点,因此要获得实际的旋转角度,您将需要执行以下几个步骤: 首先使用solvepnp获得旋转矢量. 第二种使用罗德里格斯将其转换为旋转矩阵. 现在使用decomposeProjectionMatrix来计算欧拉角.这些欧拉角使您可以沿轴旋转.您可以尝试以下方法:

Solvepnp does not give actual rotation values, it used to get real world point so in order to get actual rotation angles you will have to do few more steps : First use solvepnp to get rotation vector. Second use rodrigues to convert it to rotation matrix. Now use decomposeProjectionMatrix to compute eulers angle . These eulers angle give you the rotation along axis. You can try this :

void getEulerAngles(Mat &rotCamerMatrix,Vec3d &eulerAngles){

Mat cameraMatrix,rotMatrix,transVect,rotMatrixX,rotMatrixY,rotMatrixZ;
double* _r = rotCamerMatrix.ptr();
double projMatrix[12] = {_r[0],_r[1],_r[2],0,
_r[3],_r[4],_r[5],0,
_r[6],_r[7],_r[8],0};

decomposeProjectionMatrix( Mat(3,4,CV_64FC1,projMatrix),
cameraMatrix,
rotMatrix,
transVect,
rotMatrixX,
rotMatrixY,
rotMatrixZ,
eulerAngles);
}

调用如下函数:

Vec3d eulerAngles;
getEulerAngles(rotCamerMatrix1,eulerAngles);

您的旋转角度将为:

yaw = eulerAngles[1];
pitch = eulerAngles[0];
roll = eulerAngles[2];

俯仰是您沿Y轴的旋转.

Where pitch is your rotation along Y axis.

这篇关于相机旋转解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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