$$% Arcus cosine. \def\acos{\cos^{-1}} % Vector projection. \def\projection#1#2{{proj_{#1}\left(#2\right)}} % Vector rejection. \def\rejection#1#2{{rej_{#1}\left(#2\right)}} % Norm. \def\norm#1{{\left\|#1\right\|}} % Cross product. \def\cross#1#2{\mathit{cross}\left(#1,#2\right)} % Dot product. \def\dot#1#2{{#1 \cdot #2}} % Magnitude. \def\mag#1{{\left|#1\right}} \def\group#1{\left(#1\right)}} \def\sbgrp#1{\left\{#1\right\}}$$

Quaternion to axis angle

A unit quaternion $$\mathbf{q}=\group{\cos\group{\frac{\alpha}{2}},\hat{r}\sin\group{\frac{\alpha}{2}}}$$ represents a rotation by an angle $$\alpha$$ around an axis $$\hat{r}$$ where

\begin{aligned} &\alpha = 2\acos{q_w} \end{aligned}
\begin{aligned} &\hat{v}_x = \frac{q_x}{\sqrt{1-q_w^2}} \end{aligned}
\begin{aligned} &\hat{v}_y = \frac{q_y}{\sqrt{1-q_w^2}} \end{aligned}
\begin{aligned} &\hat{v}_z = \frac{q_z}{\sqrt{1-q_w^2}} \end{aligned}

Proof

Recall how an axis angle $$\hat{a},\alpha$$ is converted to a unit quaternion $$q$$:

\begin{aligned} &q_x = \hat{a}_x \sin\left(\alpha/2\right) \end{aligned}
\begin{aligned} &q_y = \hat{a}_y \sin(\alpha/2) \end{aligned}
\begin{aligned} &q_z = \hat{a}_z \sin(\alpha/2) \end{aligned}
\begin{aligned} &q_w = \cos(\alpha/2) \end{aligned}
To obtain $$\alpha$$ from the quaternion solve $$q_w = \cos(\alpha/2)$$ for $$\alpha$$:
\begin{aligned} q_w = \cos(\alpha/2) \end{aligned}
\begin{aligned} &\acos{q_w} = \acos{\cos(\alpha/2)} \end{aligned}
\begin{aligned} &\acos{q_w} = \frac{\alpha}{2} \end{aligned}
\begin{aligned} &2\acos{q_w} = \alpha \end{aligned}
\begin{aligned} &\alpha = 2\acos{q_w} \end{aligned}
To obtain $$\hat{v}_{k}$$ solve $$q_k = \hat{v}_k \sin(\alpha/2)$$ for $$\hat{v}_k$$:
\begin{aligned} &q_k = \hat{v}_k \sin(\alpha/2) \end{aligned}
\begin{aligned} &\frac{q_k}{\sin(\alpha/2)} = \hat{v}_k \end{aligned}
As \begin{aligned} &\frac{\alpha}{2} = \frac{2\acos{q_w}}{2} = \acos{q_w} \end{aligned}
\begin{aligned} &\frac{q_k}{\sin(\acos{q_w})} = \hat{v}_k \end{aligned}
As \begin{aligned} &\sin(\acos{q_w}) = \sqrt{1 - q_w^2} \end{aligned}
\begin{aligned} &\frac{q_k}{\sqrt{1 - q_w^2}} = \hat{v}_k \end{aligned}
As \begin{aligned} &\sin(\acos{q_w}) = \sqrt{1 - q_w^2} \end{aligned}
\begin{aligned} &\hat{v}_k = \frac{q_k}{\sqrt{1 - q_w^2}} \end{aligned}

The expression $$\hat{v}_k=\frac{q_k}{\sqrt{1-q_w^2}}$$ for $$q_w = \pm1$$ is undefined. However, we claim that a unit quaterion with $$q_w = \pm 1$$ represents a rotation by zero degrees. If that claim holds, we can avoid the evaluation of the undefined expression by picking an arbitrary unit vector for $$\hat{v}$$.

We convince us that for $$q_w = \pm 1$$, $$\alpha$$ is some integer multiple of $$2\pi$$ and hence the quaternion represents a rotation by zero degrees: First, observe the cosine is one at even non-negative integer multiples of $$pi$$ e.g. $$0$$, $$2\pi$$, $$4\pi$$, $$6\pi$$, $$8\pi$$, ... Second, the cosine is minus one at odd positive integer multiples of $$pi$$ e.g. $$1 \pi$$, $$3 \pi$$, $$5 \pi$$, ... Because of $$\cos(-x)) = cos(x)$$ all this is also true for negative multiples. Hence $$\alpha/2$$ must be some integer multiple of $$0,\pm\pi, \pm 2\pi, \pm 3\pi,\ldots$$ for that $$\pm 1 = \cos\left(\alpha/2\right)$$ holds. Consequently $$\alpha$$ must be $$0$$, $$\pm 2\pi$$, $$\pm 4\pi$$, $$\pm 6\pi$$, ... Hence a unit quaternion with $$q_w = 1$$ represents a rotation by $$\alpha = 2 n \pi$$ radians. And these rotations are all equivalent to the rotation by zero degrees.