import SimpleITK as sitk
import numpy as np
def cShow(img1, img2, title="", margin=0.05, expand=1):
simg1 = sitk.Cast(sitk.RescaleIntensity(img1), sitk.sitkUInt8)
simg2 = sitk.Cast(sitk.RescaleIntensity(img2), sitk.sitkUInt8)
cimg = sitk.Compose(simg1, simg2, simg1/2.+simg2/2.)
if expand != 1:
cimg = sitk.Expand(cimg, [expand]*3)
del simg1, simg2
aimg = sitk.GetArrayFromImage(cimg)
if aimg.ndim == 3:
ysize,xsize,c = aimg.shape
if not c in (3,4):
aimg = aimg[c//2,:,:]
else:
ysize,xsize = aimg.shape
dpi=80
# Make a figure big enough to accomodate an axis of xpixels by ypixels
# as well as the ticklabels, etc...
figsize = (1 + margin) * ysize / dpi, (1 + margin) * xsize / dpi
fig = plt.figure(figsize=figsize, dpi=dpi)
# Make the axis the right size...
ax = fig.add_axes([margin, margin, 1 - 2*margin, 1 - 2*margin])
extent = (0,xsize*cimg.GetSpacing()[1],0,ysize*cimg.GetSpacing()[0])
t = ax.imshow(aimg,extent=extent)
if aimg.ndim == 2:
t.set_cmap("gray")
if(title):
plt.title(title)
Load sample data from ANTS registration.
%cd ~/src/SimpleITK-Notebooks/
img1 = sitk.Cast(sitk.ReadImage("Data/B1.tiff"), sitk.sitkFloat32)
img2 = sitk.Cast(sitk.ReadImage("Data/B2.tiff"), sitk.sitkFloat32)
cShow(img1,img2, title="Pre-Regstration",expand=3)
antsRegistration -d 2 -r [ B1.tiff, B2.tiff ,1] -m mattes[ B1.tiff, B2.tiff , 1 , 32, regular, 0.1 ] -t affine[ 0.05 ] -c [100x0x0,1.e-8,20] -s 4x2x1 -f 6x4x2 -l 1 -m mattes[ B1.tiff, B2.tiff , 1 , 32 ] -t syn[ .3, 3, 0.0 ] -c [10x0x0,1.e-8,20] -s 2x1x0 -f 4x2x1 -l 1 -u 1 -z 1 --write-composite-transform 1 -o [\({nm},\){nm}_diff.nii.gz,
tx = sitk.ReadTransform("Data/B1_fixed_B2_moving0GenericAffine.mat")
print tx
img2_a = sitk.Resample(img2, img1, tx, sitk.sitkNearestNeighbor, sitk.sitkFloat32)
cShow(img1,img2_a, "Affine Registered", expand=3)
tx_comp = sitk.ReadTransform("Data/B1_fixed_B2_movingComposite.h5")
img2_a = sitk.Resample(img2, img1, tx_comp, sitk.sitkGaussian, sitk.sitkFloat32)
cShow(img1,img2_a, "Deformed Registered", expand=3)