# - compatibility with Python 3
from __future__ import print_function # print('me') instead of print 'me'
from __future__ import division # 1/2 == 0.5, not 0
# - show figures inside the notebook
%matplotlib inline
# - import common modules
import numpy as np # the Python array package
import matplotlib.pyplot as plt # the Python plotting package
# - set gray colormap and nearest neighbor interpolation by default
plt.rcParams['image.cmap'] = 'gray'
plt.rcParams['image.interpolation'] = 'nearest'
# - import numpy.linalg with a shorter name
import numpy.linalg as npl
# - make numpy print out values in less verbose form
np.set_printoptions(suppress=True, precision=4)
I took this image
ds114_sub009_highres.niiand I made a new copy, in img, hdr format:
ds114_sub009_highres_moved.imgI then modified the affine in ds114_sub009_highres_moved.hdr to add an extra rotation and translation.
What extra translation and rotation did I add?
Hint: remember the rotation formulae:
$$ \begin{alignat}{1} R_x(\theta) &= \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos \theta & -\sin \theta \\[3pt] 0 & \sin \theta & \cos \theta \\[3pt] \end{bmatrix} \\[6pt] R_y(\theta) &= \begin{bmatrix} \cos \theta & 0 & \sin \theta \\[3pt] 0 & 1 & 0 \\[3pt] -\sin \theta & 0 & \cos \theta \\ \end{bmatrix} \\[6pt] R_z(\theta) &= \begin{bmatrix} \cos \theta & -\sin \theta & 0 \\[3pt] \sin \theta & \cos \theta & 0\\[3pt] 0 & 0 & 1\\ \end{bmatrix} \end{alignat} $$You might also want to compare against results from our rotations code:
# - get our functions to make rotation matrices
# If this gives an error - maybe you didn't set your PYTHONPATH? Let us know!
from rotations import x_rotmat, y_rotmat, z_rotmat
Final hint:
np.arcsin, np.arccos.# Get the original image affine
import nibabel as nib
orig_img = nib.load('ds114_sub009_highres.nii')
print(orig_img.affine)
# Get the new image affine
moved_img = nib.load('ds114_sub009_highres_moved.img')
moved_img.affine
# Work out what transform has been added in the new affine
extra = moved_img.affine.dot(npl.inv(orig_img.affine))
extra
# What rotation and translation has been applied?
z_angle = np.arccos(extra[0, 0])
translation = extra[:3, 3]
z_angle, translation