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);
I'm not sure how to continue so that I can get a single value which represents the Y rotation of the camera.
EDIT
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;
}
The error that I get now is:
System.Runtime.InteropServices.SEHException: 'External component has thrown an exception.'
With error code:
-2147467259
Do I call the function with the wrong variables?
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);
}
Call the function like:
Vec3d eulerAngles;
getEulerAngles(rotCamerMatrix1,eulerAngles);
Where your rotation angles will be :
yaw = eulerAngles[1];
pitch = eulerAngles[0];
roll = eulerAngles[2];
Where pitch is your rotation along Y axis.