Last edited 17oct15 by Martin Miller
  ___              _                  _
 / _ \ _   _  __ _| |_ ___ _ __ _ __ (_) ___  _ __  ___
| | | | | | |/ _` | __/ _ \ '__| '_ \| |/ _ \| '_ \/ __|
| |_| | |_| | (_| | ||  __/ |  | | | | | (_) | | | \__ \
 \__\_\\__,_|\__,_|\__\___|_|  |_| |_|_|\___/|_| |_|___/

Abstract

Below are some notes and code for using quaternions in computer vision and robotics. The notes are for me to use as a reference in my work in using riverine reflections to determine feature depth in robot localization and mapping.

Code

I have written a python class for manipulating quaternions. You can download it here. For quick viewing in your browser, click here. This class implements quaternion multiplication and allows quick access to the column vector representation of a quaternion (i.e. (a,b,c,d)^T) as well as the 2x2 complex matrix representation suitable for unit quaternions.

Representation

We can represent a quaternion: a+bi+cj+dk in the column vector form described above. We call a the scalar part of the quaternion and b,c,d the vector part of the quaternion.

Scalar multiplication and quaternion addition are defined in the same way as for typical vectors in R^4.

Quaternions have an additional operation of quaternion multiplication, which is non-commutative, and is defined as follows:

q1=(a1,b1,c1,d1)^T
q2=(a2,b2,c2,d2)^T
q3=q1q2=
a1*a2-b1*b2-c1*c2-d1*d2
a1*b2+b1*a2+c1*d2-d1*c2
a1*c2-b1*d2+c1*a2+d1*b2
a1*d2+b1*c2-c1*b2+d1*a2

Rotations

A vector x is often transformed to x' using a rotation matrix R using the equation x'=Rx. When we work with quaternions we could convert them to a rotation matrix in order to perform transformations, but we can also perform the transformation directly. A quaternion transforms a vector as follows:

Let q be a unit quaternion (i.e. a quaternion with norm of 1), x a vector to transform.

Create a new quaternion, y from x where the scalar part is 0 and the vector part is x.

y'=qyinv(q)

And x' is the vector part of y'

Combining rotations

We can easily combine a sequence of rotations into a single quaternion using quaternion multiplication as defined above. We can think of this the same way as when combining a sequence of rotation matrices. Start the with first rotation and left multiply it by the subsequent rotations so that the first rotation is on the right and the last rotation is on the far left. For the sequence of rotations q1,...,qn the composite quaternion Q is computed as:

qn...q3q2q1=Q

Reflections

Since my research makes use of reflections on the water surface to estimate depth information it is interesting to note that quaternions can be used for reflecting a vector across a plane. Define the reflection quaternion, q, as a pure quaternion (0 scalar part) with a vector part equal to the unit normal of the plane. Then the reflection r is a pure quaternion of the source quaternion s by the following calculation:

r=qsq

Of course, in case of a reflection in a river, the reflection plane is the z axis and source vector s is trivially reflected by replacing its z component with its opposite.

Resources

The notes provided here have been informed by the following online resources:
  1. https://en.wikipedia.org/wiki/Quaternion
  2. https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
  3. http://www.euclideanspace.com/maths/geometry/affine/reflection/quaternion/index.htm
  4. http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/index.htm
  5. http://tutis.ca/Rotate/7quaternions.htm