Rotating a pair of vectors using quarternions

3dquaternionsrotations

The objective is to take a pair of vectors and rotate them so that they match another pair of vectors, their corresponding pair.

The paper describing this says to minimize the error function:
$ Err = \sum ||n'-Rn||^2 $

Where n is the orientation vector.

The textbook the paper tags then says to use Quaternions to minimize this error function and get the unknown R.

The textbook breaks down the derivation as follows, after applying quaternions:

$ Err = \sum |n'-q\times n\times \bar q|^2 $

Using q^2 = 1 we can multiply that into the above equation and compress some terms again using this rule to get down to:

$ Err = \sum |n'\times q – q\times n|^2 $

According to the textbook: It follows from the definition of the product of the quaternion that $n'\times q – q\times n$ is a linear function of the 4 coordinates of q. Thus there is a 4×4 matrix A such that:

$|n'\times q – q\times n|^2 = q^TA^TAq $

so we need to find the min of the above right hand side.
The solution to this is the smallest eigenvalue – eigenvector pair of the matrix $A^TA$.

So my questions are:

  1. What are the elements of the $A^T A$ matrix

  2. How do I get my rotation matrix after solving for this pair

  3. All I have is the orientations of the vectors, is this enough to calculate this unknown R.

Any help is appreciated here, I am very new to Quaternions. Hopefully my professor responds to my email soon but if not, thanks for the help.

The "textbook" I keep mentioning is "O. Faugeras. Three–Dimensional Computer Vision. The MIT
Press, 1996."

Best Answer

You are looking for quaternion $q$ minimizing the following error function for some pure quaternions $n$ and $n'$.

$\sum_i \|n'_i q - q n_i \|^2$

S.t. $q q^* =1$

The product $q n_i$ can be expressed in linear algebra as a matrix multiplication of a $4 \times 4$ matrix $X_i$ representing the quaternion $n_i$ and a $4 \times 1$ column vector $Q$ representing quafernion $q$.

The product $n'_i q$ can be expressed in linear algebra as a matrix multiplication of a $4 \times 4$ matrix $Y_i$ representing the quaternion $n'_i$ and a $4 \times 1$ column vector $Q$ representing quafernion $q$.

So we have now:

$\sum_i \| Y_i Q - X_i Q\|^2$

S.t. $Q^T Q = 1$

Which is a pretty standard matrix equation. Factorizing the $Q$ we get:

$\sum_i \| (Y_i - X_i) Q\|^2$

S.t. $Q^T Q = 1$

$$\sum_i \| (Y_i - X_i) Q \|^2 = \sum_i ((Y_i - X_i) Q)^T (Y_i - X_i) Q$$

$$ = \sum_i Q^T (Y_i - X_i)^T (Y_i - X_i) Q$$

$$ = Q^T (\sum_i (Y_i - X_i)^T (Y_i - X_i)) Q$$

$$ = Q^T M Q$$

Where $M$ is:

$$M = \sum_i (Y_i - X_i)^T (Y_i - X_i)$$

Finally I will define the $X_i$ and $Y_i$ matrices given the pure quaternion $n = n_x i + n_y j + n_z k$ and pure quaternion $n' = n'_x i + n'_y j + n'_z k$:

$q n = X Q= \begin{bmatrix} 0&-n_x&-n_y&-n_z\\ n_x&0&n_z&-n_y\\ n_y&-n_z&0&n_x\\ n_z&n_y&-n_x&0 \end{bmatrix} Q$

$n' q = Y Q = \begin{bmatrix} 0&-n'_x&-n'_y&-n'_z\\ n'_x&0&-n'_z&n'_y\\ n'_y&n'_z&0&-n'_x\\ n'_z&-n'_y&n'_x&0 \end{bmatrix} Q$

Note that $X$ differs from $Y$ in that the lower $3 \times 3$ submatrix is transposed. This is because the noncommutative nature of multiplication of quaternions.

Related Question