NiBabel

Access a cacophony of neuro-imaging file formats

Previous topic

nibabel.quaternions.isunit

Next topic

nibabel.quaternions.mult

Reggie -- the one

nibabel.quaternions.mat2quat

nibabel.quaternions.mat2quat(M)

Calculate quaternion corresponding to given rotation matrix

Parameters:

M : array-like

3x3 rotation matrix

Returns:

q : (4,) array

closest quaternion to input matrix, having positive q[0]

Notes

Method claimed to be robust to numerical errors in M

Constructs quaternion by calculating maximum eigenvector for matrix K (constructed from input M). Although this is not tested, a maximum eigenvalue of 1 corresponds to a valid rotation.

A quaternion q*-1 corresponds to the same rotation as q; thus the sign of the reconstructed quaternion is arbitrary, and we return quaternions with positive w (q[0]).

References

Examples

>>> import numpy as np
>>> q = mat2quat(np.eye(3)) # Identity rotation
>>> np.allclose(q, [1, 0, 0, 0])
True
>>> q = mat2quat(np.diag([1, -1, -1]))
>>> np.allclose(q, [0, 1, 0, 0]) # 180 degree rotn around axis 0
True