C++ 如何从旋转矩阵计算角度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15022630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:54:26  来源:igfitidea点击:

How to calculate the angle from rotation matrix

c++opencvcomputer-visioneuler-angles

提问by N.J

I am using two image of the single object the object is roated certain degree from its first image.

我正在使用单个对象的两个图像,该对象从它的第一个图像旋转了一定程度。

I have calculated the POSE of each image and converted the rotational vector to Matrix using Rodergues(). Now how do I calculate and see how much it is rotated from its first position?

我已经计算了每个图像的姿势并使用 Rodergues() 将旋转向量转换为矩阵。现在我如何计算并查看它从第一个位置旋转了多少?

I have tried many ways but answers was no were close

我尝试了很多方法,但答案是否定的

EDIT: My camera is fixed only the object is moving.

编辑:我的相机是固定的,只有物体在移动。

回答by Krish

We can get Euler angles from rotation matrix using following formula.

我们可以使用以下公式从旋转矩阵中获得欧拉角。

Given a 3×3 rotation matrix

给定一个 3×3 的旋转矩阵

enter image description here

在此处输入图片说明

The 3 Euler angles are

3个欧拉角是

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

Here atan2 is the same arc tangent function, with quadrant checking, you typically find in C or Matlab.

这里 atan2 是相同的反正切函数,带有象限检查,您通常在 C 或 Matlab 中找到。

Note: Care must be taken if the angle around the y-axis is exactly +/-90°. In that case all elements in the first column and last row, except the one in the lower corner, which is either 1 or -1, will be 0 (cos(1)=0). One solution would be to fix the rotation around the x-axis at 180° and compute the angle around the z-axis from: atan2(r_12, -r_22).

注意:如果绕 y 轴的角度正好是 +/-90°,则必须小心。在这种情况下,第一列和最后一行中的所有元素,除了下角的元素,即 1 或 -1,都将为 0 (cos(1)=0)。一种解决方案是将绕 x 轴的旋转固定为 180°,并根据以下公式计算绕 z 轴的角度:atan2(r_12, -r_22)。

See also https://www.geometrictools.com/Documentation/EulerAngles.pdf, which includes implementations for six different orders of Euler angles.

另请参阅https://www.geometrictools.com/Documentation/EulerAngles.pdf,其中包括六种不同欧拉角阶数的实现。

回答by Beta

If Ris the (3x3) rotation matrix, then the angle of rotation will be acos((tr(R)-1)/2), where tr(R) is the trace of the matrix (i.e. the sum of the diagonal elements).

如果R是 (3x3) 旋转矩阵,则旋转角度将为 acos((tr( R)-1)/2),其中 tr( R) 是矩阵的迹(即对角线元素的总和)。

That is what you asked for; I estimate a 90% chance that it is not what you want.

这就是你所要求的;我估计有 90% 的机会不是你想要的。

回答by Curnane

For your reference, this code computes the Euler angles in MATLAB:

供您参考,此代码在 MATLAB 中计算欧拉角:

function Eul = RotMat2Euler(R)

if R(1,3) == 1 | R(1,3) == -1
  %special case
  E3 = 0; %set arbitrarily
  dlta = atan2(R(1,2),R(1,3));
  if R(1,3) == -1
    E2 = pi/2;
    E1 = E3 + dlta;
  else
    E2 = -pi/2;
    E1 = -E3 + dlta;
  end
else
  E2 = - asin(R(1,3));
  E1 = atan2(R(2,3)/cos(E2), R(3,3)/cos(E2));
  E3 = atan2(R(1,2)/cos(E2), R(1,1)/cos(E2));
end

Eul = [E1 E2 E3];

Code provided by Graham Taylor, Geoff Hinton and Sam Roweis. For more information, see here

代码由 Graham Taylor、Geoff Hinton 和 Sam Roweis 提供。有关更多信息,请参阅此处

回答by Francesco Callari

Let R1c and R2c be the 2 rotation matrices you have computed. These express the rotations from the object in poses 1 and 2 respectively to the camera frame (hence the second c suffix). The rotation matrix you want is from pose 1 to pose 2, i.e. R12. To compute it you must rotate, in your mind, the object from pose_1-to-camera, then from the camera-to-pose_2. The latter rotation is the inverse of the pose_2-to-camera espressed by R2c, hence:

让 R1c 和 R2c 是您计算的 2 个旋转矩阵。它们分别表示从姿势 1 和姿势 2 中的对象到相机帧的旋转(因此是第二个 c 后缀)。你想要的旋转矩阵是从pose 1到pose 2,即R12。要计算它,您必须在脑海中旋转物体,从pose_1 到相机,然后从相机到pose_2。后一个旋转是由 R2c 压缩的pose_2-to-camera的逆,因此:

R12 = R1c * inv(R2c)

R12 = R1c * inv(R2c)

From matrix R12 you can then compute the angle and axis of rotation using Rodiguez's formula.

然后,您可以从矩阵 R12 使用 Rodiguez 公式计算角度和旋转轴。