In [1]:
import SimpleITK as sitk
import numpy as np
In [2]:
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.

In [3]:
%cd ~/src/SimpleITK-Notebooks/

img1 = sitk.Cast(sitk.ReadImage("Data/B1.tiff"), sitk.sitkFloat32)
img2 = sitk.Cast(sitk.ReadImage("Data/B2.tiff"), sitk.sitkFloat32)
/Users/blowekamp/src/SimpleITK-Notebooks

In [4]:
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,

In [5]:
tx = sitk.ReadTransform("Data/B1_fixed_B2_moving0GenericAffine.mat")
print tx
CompositeTransform (0x7fc94c200b70)
  RTTI typeinfo:   itk::CompositeTransform<double, 2u>
  Reference Count: 2
  Modified Time: 1463
  Debug: Off
  Object Name: 
  Observers: 
    none
  Transforms in queue, from begin to end:
  >>>>>>>>>
  AffineTransform (0x7fc94c200a00)
    RTTI typeinfo:   itk::AffineTransform<double, 2u>
    Reference Count: 1
    Modified Time: 1460
    Debug: Off
    Object Name: 
    Observers: 
      none
    Matrix: 
      0.983927 0.00905935 
      0.0247638 1.01424 
    Offset: [1.8376, 0.825096]
    Center: [0, 0]
    Translation: [1.8376, 0.825096]
    Inverse: 
      1.01656 -0.00908013 
      -0.0248206 0.986185 
    Singular: 0
  End of MultiTransform.
<<<<<<<<<<
  TransformsToOptimizeFlags, begin() to end(): 
    1 
  TransformsToOptimize in queue, from begin to end:
  End of TransformsToOptimizeQueue.
<<<<<<<<<<
  End of CompositeTransform.
<<<<<<<<<<


In [6]:
img2_a = sitk.Resample(img2, img1, tx, sitk.sitkNearestNeighbor, sitk.sitkFloat32)
cShow(img1,img2_a, "Affine Registered", expand=3)
In [7]:
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)
In [7]: