How to compare two rotations represented by axis-angle rotation vectors?

2.3k Views Asked by At

I currently have two rotation vectors r1 and r2 using axis-angle representations, i.e. they are 3d vectors, their norms are the rotation angles and the normalized unit vectors are rotation axes respectively.

I am confused how to compare r1 and r2. In an optimization process of a regression system, if r1 is the ground truth, and r2 is the current result produced by the system, how do I define a proper loss to encourage the system to converge to r1? Can I just use L1/L2 loss between r1 and r2?

Thanks!

2

There are 2 best solutions below

0
On BEST ANSWER

There are multiple ways one could define the error between two rotation, see for example: Huynh, Du Q. "Metrics for 3D rotations: Comparison and analysis." Journal of Mathematical Imaging and Vision 35.2 (2009): 155-164. That source mainly defines errors between rotation matrices and quaternions, but your representation can be converted to those relatively easily. In my opinion the most geometrically meaningful error measure would be the smallest angle the second rotation representation would have to be rotated by in order to match the first rotation. This angle can be calculated from the rotation separating the two, which can be obtained by first representing $r_1$ and $r_2$ as rotation matrices or quaternions and divide one by the other.


Your axis-angle representation can be converted into a rotation matrix using

$$ R(r) = I + \frac{\sin(\|r\|)}{\|r\|}S(r) + \frac{1-\cos(\|r\|)}{\|r\|^2}S(r)^2, \tag{1} $$

with $I$ the three by three identity matrix, $r = \begin{bmatrix}r_1 & r_2 & r_3\end{bmatrix}^\top$ the vector of the axis-angle representation and

$$ S(r) = \begin{bmatrix} 0 & -r_3 & r_2 \\ r_3 & 0 & -r_1 \\ -r_2 & r_1 & 0 \end{bmatrix}. \tag{2} $$

Rotation matrices can be "divided" by each other by multiplying one by the inverse of the other and the inverse of a rotation matrix is the same as the transpose of that rotation matrix. Taking the transpose of a matrix is equivalent to negating its skew-symmetric part. From $(1)$ it can be noted that $S(r)$ is always skew-symmetric, but $I$ and $S(r)^2$ are symmetric. Using this and that the scalar parts of $(1)$ are even or odd functions of $\|r\|$ yields that when taking the transpose of $(1)$ is equivalent to using $R(-r)$. Therefore, the relative rotation in your case could be obtained using

$$ R_e = R(r_1)\,R(-r_2), \tag{3} $$

whose associated angle in the interval $[0,\pi]$ can be calculated with

$$ \theta = \cos^{-1}\left(\frac{\text{tr}(R_e) - 1}{2}\right). \tag{4} $$


It is also possible to calculate the same using unit quaternions. The axis-angle representation can be converted into a quaternion using the scalar vector representation with

$$ q(r) = \left(\cos\left(\frac{\|r\|}{2}\right),\sin\left(\frac{\|r\|}{2}\right)\frac{r}{\|r\|}\right). \tag{5} $$

Quaternion can be divided by each other by multiplying one by the inverse of the other. Here the inverse of unit quaternions can be obtained by negating the vector part. However, when multiplying the quaternions together one has to use the Hamilton product. When one only want to recover the angle in the interval $[0,\pi]$ from the resulting unit quaternion it is sufficient to only calculate the scalar part. After substituting in $(5)$ it can be shown that the resulting angle can be calculated with

$$ \theta = 2\cos^{-1}\left(\left|\cos\left(\frac{\|r_1\|}{2}\right) \cos\left(\frac{\|r_2\|}{2}\right) + \sin\left(\frac{\|r_1\|}{2}\right)\sin\left(\frac{\|r_2\|}{2}\right)\frac{r_1^\top\,r_2}{\|r_1\|\,\|r_2\|}\right|\right). \tag{6} $$

Both $(4)$ and $(6)$ should give the same result (up to machine accuracy), but $(6)$ is probably faster to calculate.

0
On

These vectors are actually both the Lie Algebra associated with $SO(3)$, and this is actually a vector space, so assuming that your coordinate frames line up, you can totally use something like $L_2$ loss between between them as a cost function.

The trick here is knowing what the coordinate frames are.

To convert from the Lie Algebra to the rotation matrix, we can use the matrix exponential ($SO(3)$ has a shortcut, where you can just use the rodriguez formula), but in general it's

$R = \exp\left(\lfloor r \rfloor_\times\right)$

where $\lfloor r \rfloor_\times$ is the skew-symmetric matrix formed from $r$.

Disclaimer: There are various conventions here that can be super confusing, and my conventions may not agree with yours, so you may need to play around with my equations below to make your particular implementation work.

Assuming that you define $R_1 = \exp\left(\lfloor r_1 \rfloor_\times\right)$ and $R_2 = \exp\left(\lfloor r_2 \rfloor_\times\right)$. If $R_1$ and $R_2$ are defined in the same frame (say, they are both "world to whatever"), then the $L_2$ norm will give you proper convergence since both $r_1$ and $r_2$ will be defined in the same coordinate frame. If, instead they are something like "estimate to world" and "truth to world", then you need to do $J = \lVert R_1^\top r_1 - R_2^\top r_2\rVert$ because you'll want to do the subtraction in a common frame. If for some crazy reason you have a mix of the two, then make the appropriate coordinate frame swaps.

One additional trick is that you probably want to make sure that the norm of these vectors has any extra $2\pi$ factors removed, but otherwise I do this kind of thing all the time and it totally works. In my experience, I usually don't have axis-angle parameterizations. Instead I have the unit quaternion or rotation matrix, so I tend to use the following formula as a cost function in bundle-adjustment or other optimization problems over rotation manifolds.

$$ J = \lVert\log(R_1 R_2^\top)\rVert $$

where

$$ \log(R) = \frac{\theta}{2 \sin \theta}(R-R^\top) $$

and the rotation angle

$$ \theta = \arccos\left(\frac{\mathrm{tr}(R) - 1}{2}\right). $$