%pylab inline
import numpy as np
import matplotlib.pyplot as plt
Make sure you have downloaded the MNI templates archive from http://www.bic.mni.mcgill.ca/~vfonov/icbm/2009/mni_icbm152_nlin_asym_09a_nifti.zip
Unzip into the directory containing this notebook, so you have a directory like this:
import os
MNI_PATH = 'mni_icbm152_nlin_asym_09a'
os.listdir(MNI_PATH)
import nibabel as nib
t2_fname = os.path.join(MNI_PATH, 'mni_icbm152_t2_tal_nlin_asym_09a.nii')
t2_img = nib.load(t2_fname)
plt.imshow(t2_img.get_data()[:, :, 90], cmap="gray")
We get an example structural from the Haxby dataset:
DATA_PATH = os.path.join(os.path.expanduser('~'), 'data', 'ds105')
os.listdir(DATA_PATH)
anat_fname = os.path.join(DATA_PATH, 'sub001', 'anatomy', 'highres001.nii.gz')
anat_img = nib.load(anat_fname)
plt.imshow(anat_img.get_data()[:, :, 128], cmap="gray")
The affines give some match for the images. We can use these with the nipy resample function:
import nipy.algorithms.registration as nar
# Make a transform that does nothing to use the image affines only to register the datasets
empty_transform = nar.Affine()
empty_transform.param
anat_resampled = nar.resample(anat_img, empty_transform, t2_img)
fig, axes = plt.subplots(1, 2)
fig.set_size_inches(10.5,18.5)
axes[0].imshow(anat_resampled.get_data()[:, :, 90], cmap="gray")
axes[1].imshow(t2_img.get_data()[:, :, 90], cmap="gray")
Let's try and match the images using affine parameters
register_obj = nar.HistogramRegistration(from_img = t2_img, to_img = anat_img, similarity='cc')
transform = register_obj.optimize('affine')
anat_better_resampled = nar.resample(anat_img, transform, t2_img)
fig, axes = plt.subplots(1, 2)
fig.set_size_inches(10.5,18.5)
axes[0].imshow(anat_better_resampled.get_data()[:, :, 90], cmap="gray")
axes[1].imshow(t2_img.get_data()[:, :, 90], cmap="gray")
Can we do better than this?