This test runs a two-stages registration process including following stages:

1) Rigid

2) Affine

The main difference between this test and the test 1 is the way that registration process is initialized and the different stages are connected to eachother. Here we use SetMovingInitialTransform() method to initialize the second stage using the output of the firts stage. Also, we use Inplace=False, so at each stage the output transform object is different than the input transform object. We use a composite transform as container to concatenate the initial transform and the outputs of different stages together.

Also, here we use fixed and moving images masks to do the registration only on regions of interest.

We evaluate the results of the current registration process based on the results of BRIANSFit program that is run with a consistent command set. BRIANSFit is a 3D registration tool included in BRAINSTools. Get BRAINSTools from: https://github.com/BRAINSia/BRAINSTools


In [1]:
## Boiler plate code common to many notebooks.  See the TestFilesCommonCode.ipynb for details
from __future__ import print_function
%run TestFilesCommonCode.ipynb


SimpleITK Version: 0.9.0.dev2042-g22c5b
Compiled: Aug 14 2014 10:17:06


In [2]:
######## WARNING:
# You will need to replace the following path with your local BRAINSFit path.
BRIANSToolsPath="/Users/aghayoor/WorkSpace/BS/release/bin"
BRAINSFitPath=os.path.join(BRIANSToolsPath,"BRAINSFit")
if not os.path.isfile(BRAINSFitPath):
    print("ERROR: This script requires that BRAINSFit can be found")
    print("{0} does not exists".format(BRAINSFitPath))

In [3]:
fixedImageFilename = fdata('BRAINSTools/test.nii.gz')
movingImageFilename = fdata('BRAINSTools/rotation.test.nii.gz')
fixed = sitk.ReadImage(fixedImageFilename,sitk.sitkFloat32)
moving = sitk.ReadImage(movingImageFilename, sitk.sitkFloat32)

#referenceBaselineImageFilename = fdata('BRAINSTools/BRAINSFitTest_AffineRotationMasks.result.nii.gz')
#baseline = sitk.ReadImage(referenceBaselineImageFilename, sitk.sitkFloat32)

fixedMaskFilename = fdata("BRAINSTools/test_mask.nii.gz")
movingMaskFilename = fdata("BRAINSTools/rotation.test_mask.nii.gz")
fixedMask = sitk.ReadImage(fixedMaskFilename, sitk.sitkFloat32)
movingMask = sitk.ReadImage(movingMaskFilename, sitk.sitkFloat32)

myshow3d(fixed, xslices=[], yslices=[], zslices=[], title="fixed")
myshow3d(moving, xslices=[], yslices=[], zslices=[], title="moving")
#myshow3d(baseline, xslices=[], yslices=[], zslices=[], title="baseline")
myshow3d(fixedMask, xslices=[], yslices=[], zslices=[], title="fixed mask")
myshow3d(movingMask, xslices=[], yslices=[], zslices=[], title="moving mask")



In [4]:
cshow3d(fixed,moving)


<bound method ResampleImageFilter.GetTransform of <SimpleITK.SimpleITK.ResampleImageFilter; proxy of <Swig Object of type 'itk::simple::ResampleImageFilter::Self *' at 0x11a95bb70> >>

In [5]:
# Read the initial transform
initialMovingTxFilename = fdata('BRAINSTools/BRAINSFitTest_Initializer_RigidRotationNoMasks.h5')
initialMovingTx = sitk.ReadTransform(initialMovingTxFilename)
print("Initial moving transform: ")
print(initialMovingTx)
# Define a composite transform that includes the initial transform. 
# This composite tranform has the container role.
ctx = sitk.Transform(initialMovingTx)


Initial moving transform: 
CompositeTransform (0x7fb8865005f0)
  RTTI typeinfo:   itk::CompositeTransform<double, 3u>
  Reference Count: 2
  Modified Time: 6126
  Debug: Off
  Object Name: 
  Observers: 
    none
  Transforms in queue, from begin to end:
  >>>>>>>>>
  VersorRigid3DTransform (0x7fb886592160)
    RTTI typeinfo:   itk::VersorRigid3DTransform<double>
    Reference Count: 1
    Modified Time: 6120
    Debug: Off
    Object Name: 
    Observers: 
      none
    Matrix: 
      0.939743 -0.336536 -0.0602277 
      0.341882 0.925361 0.163779 
      0.000614653 -0.174501 0.984657 
    Offset: [127.477, -157.478, -148.568]
    Center: [0.884831, 22.6711, -6.13573]
    Translation: [120.163, -159.873, -152.429]
    Inverse: 
      0.939743 0.341882 0.000614653 
      -0.336536 0.925361 -0.174501 
      -0.0602277 0.163779 0.984657 
    Singular: 0
    Versor: [ -0.0862046, -0.0155046, 0.172882, 0.98104 ]
  End of MultiTransform.
<<<<<<<<<<
  TransformsToOptimizeFlags, begin() to end(): 
    1 
  TransformsToOptimize in queue, from begin to end:
  End of TransformsToOptimizeQueue.
<<<<<<<<<<
  End of CompositeTransform.
<<<<<<<<<<


In [6]:
BFitOut=os.path.realpath('Data/BFitOut.nii.gz')
# RUN THE BRAINSFIT FIRST ######################
!{BRIANSToolsPath}/BRAINSFit \
--costMetric MMI \
--numberOfIterations 250,250 \
--numberOfHistogramBins 200 \
--samplingPercentage 1.0 \
--translationScale 250 \
--maximumStepLength 0.5 \
--minimumStepLength 0.001,0.001 \
--outputVolumePixelType float \
--initialTransform {initialMovingTxFilename} \
--transformType Rigid,Affine \
--fixedVolume {fixedImageFilename} \
--movingVolume {movingImageFilename} \
--outputVolume {BFitOut}


HACK: NumberOf Threads -1
 4
Original Fixed image origin[127.969, 127.969, -127.969, 0]
Read ITK transform from file: /Users/aghayoor/WorkSpace/SimpleITK_DIRS/SimpleITK-Notebook-Answers/SimpleITK-Notebook-Answers/Data/BRAINSTools/BRAINSFitTest_Initializer_RigidRotationNoMasks.h5
HACK: 0  VersorRigid3DTransform
TranformTypes: Rigid(1 of 2).

TranformTypes: Affine(2 of 2).




=============================== ITKv4 Registration: Starting Transform Estimations for Rigid(1 of 2).===============================

FINAL_DERIVATIVE_VALUES 
0.0863784  0.797527  0.110127  0.00567726  -0.0301568  -0.000418902  
   0     -1.16404   [-0.08795197923791317, 0.010642842559750672, 0.17413932865128515, 120.2550567533264, -160.36145650255082, -152.43586158152254]
FINAL_DERIVATIVE_VALUES 
0.509579  -7.44294  0.209182  -0.016908  0.0126181  0.00216881  
   1    -0.927967   [-0.06298739224047281, -0.08744550361190973, 0.1848808911577924, 120.13962765477238, -160.27531366674958, -152.42105530765323]
FINAL_DERIVATIVE_VALUES 
-0.671759  2.83424  -0.493367  0.00895352  0.00460174  0.00196626  
   2    -0.689255   [-0.08107697981777176, -0.04561116834667341, 0.17318868824616634, 120.2111520888849, -160.23855305620066, -152.40534801436678]
FINAL_DERIVATIVE_VALUES 
-1.2158  6.53217  -0.268706  0.00969909  0.0167328  0.000154181  
   3    -0.890759   [-0.0985342022863903, 0.0013470875984890954, 0.16652003681874258, 120.24801654660185, -160.17495462296074, -152.40476200187842]
FINAL_DERIVATIVE_VALUES 
4.64743  -6.33931  2.38996  -0.0134787  0.0191513  -0.011358  
   4    -0.955939   [-0.06492357258710481, -0.029712832361338776, 0.18398411811262932, 120.20782633916791, -160.11784996609668, -152.4386289558163]
FINAL_DERIVATIVE_VALUES 
-4.90142  4.29767  -2.65833  0.00546104  -0.0010161  0.00684053  
   5    -0.921535   [-0.08826230191856217, -0.016404508694108116, 0.17106495376953204, 120.21939389579823, -160.12000227092193, -152.42413936827083]
FINAL_DERIVATIVE_VALUES 
5.23437  2.38962  3.27912  -0.00297348  0.0447772  -0.0112964  
   6     -1.14747   [-0.08278213173990172, -0.012267111942399569, 0.1746843299315878, 120.2176512521557, -160.09376004294063, -152.4307597517141]
FINAL_DERIVATIVE_VALUES 
-5.05467  -4.85473  -2.80027  -0.000875545  0.00792064  0.00707127  
   7     -1.14429   [-0.09073152643151168, -0.023740180086683563, 0.16996563420685076, 120.21679658640241, -160.0860282821629, -152.42385710753905]
FINAL_DERIVATIVE_VALUES 
4.81239  8.68571  2.32788  -0.00596405  0.0265321  -0.00934788  
   8     -1.07869   [-0.08673058534976322, -0.011780505904631458, 0.17196748669715822, 120.21306248889672, -160.06941653397655, -152.42970982329683]
FINAL_DERIVATIVE_VALUES 
2.23517  -6.62105  2.71325  0.00392259  0.0249172  -0.00441675  
   9     -1.14784   [-0.0814943989477939, -0.02109437020549414, 0.1771268175979396, 120.2161711376316, -160.04966965337923, -152.433210098795]
FINAL_DERIVATIVE_VALUES 
-4.98035  7.48041  -4.97462  -0.00414636  -0.000118273  0.00269134  
  10     -1.10125   [-0.08610102516047707, -0.016527015214154474, 0.17289827805804592, 120.21460559837183, -160.04971430943158, -152.43219393021053]
FINAL_DERIVATIVE_VALUES 
1.17256  3.33789  -0.00588143  -0.00769819  0.0212847  -0.00312173  
  11     -1.16253   [-0.08543369070355226, -0.012484216805985847, 0.17257846869897103, 120.21013003404491, -160.03733986197008, -152.43400883825407]
FINAL_DERIVATIVE_VALUES 
-0.498168  -5.88762  1.54116  0.00289853  0.0121697  0.00178246  
  12      -1.1564   [-0.08485434106787224, -0.018996535066119813, 0.1748571735047122, 120.21177689467586, -160.03042536749035, -152.43299609497595]
FINAL_DERIVATIVE_VALUES 
-1.83686  7.0779  -3.8469  -0.0123442  0.0048833  -0.00101454  
  13     -1.14258   [-0.08745344065441302, -0.013475946326623479, 0.17098631471987638, 120.20636366934046, -160.02828392650628, -152.43344099249816]
FINAL_DERIVATIVE_VALUES 
3.65237  -3.91945  4.2773  0.00370046  0.0230158  -0.00611102  
  14     -1.15208   [-0.08385963166539154, -0.01591324519680165, 0.17491182790017187, 120.20794749104554, -160.0184330190741, -152.43605655200264]
FINAL_DERIVATIVE_VALUES 
-4.16036  2.07214  -4.12227  -0.00412728  -0.00148643  0.00494308  
  15     -1.15602   [-0.08651909241354731, -0.015330322752125147, 0.1723077795623029, 120.2066935243923, -160.01888463021783, -152.434554726394]
FINAL_DERIVATIVE_VALUES 
2.01788  0.31648  1.82503  -0.00260867  0.0154643  -0.00446628  
  16     -1.16463   [-0.08576454232396798, -0.015005769774330248, 0.17302115605641086, 120.20617482056288, -160.01580974143795, -152.43544279318454]
FINAL_DERIVATIVE_VALUES 
-0.099665  -0.290352  -0.0566452  -0.0015386  0.0099038  0.000498454  
  17     -1.16654   [-0.0858006114063745, -0.015242913457327735, 0.17299630048610343, 120.20558048424209, -160.01198406074565, -152.43525024846028]
FINAL_DERIVATIVE_VALUES 
-0.0219829  0.276767  -0.061368  -0.00208234  0.00908355  0.000950513  
  18     -1.16654   [-0.08585733028748493, -0.015025620739892418, 0.17292651250891905, 120.20471847904126, -160.00822384222735, -152.43485677507022]
FINAL_DERIVATIVE_VALUES 
0.0808309  -0.284581  0.177945  -0.00119066  0.00816677  7.17379e-05  
  19     -1.16657   [-0.08573984279323628, -0.015258962500838589, 0.17311343303902277, 120.20416268052077, -160.00441159529478, -152.43482328778802]
FINAL_DERIVATIVE_VALUES 
-0.187211  0.352582  -0.381158  -0.00204988  0.00653216  0.00112445  
  20     -1.16656   [-0.08599615664076493, -0.014957642983148322, 0.17267665787809122, 120.20306289171312, -160.00090699252254, -152.43422000262098]
FINAL_DERIVATIVE_VALUES 
0.400223  -0.587048  0.859244  0.000540362  0.0069402  -0.00097114  
  21     -1.16632   [-0.08554450748067381, -0.015363978180742886, 0.17352075530577868, 120.20331646663878, -159.99765017683112, -152.43467572751447]
FINAL_DERIVATIVE_VALUES 
-0.757932  0.83569  -1.45464  -0.00335819  0.00247173  0.00172547  
  22     -1.16591   [-0.08632795129618215, -0.014853890952387847, 0.17215444307113792, 120.20179752997235, -159.99653219187286, -152.43389528471025]
FINAL_DERIVATIVE_VALUES 
1.29808  -0.967814  2.20139  0.0010016  0.00723802  -0.00314062  
  23     -1.16475   [-0.08551438232774848, -0.01516368515736034, 0.17346193775793223, 120.20208695465837, -159.99444066815306, -152.43480281008232]
FINAL_DERIVATIVE_VALUES 
-0.814733  0.256502  -1.32072  -0.00224703  0.00138118  0.00246752  
  24     -1.16622   [-0.08595789333600755, -0.015165099322595839, 0.1727457675211617, 120.201481986842, -159.9940688123563, -152.4341384802038]
FINAL_DERIVATIVE_VALUES 
0.248117  -0.0281644  0.616843  -0.000568863  0.00480795  0.000499529  
  25     -1.16647   [-0.08578963859784007, -0.015117038630462334, 0.17317623484618908, 120.20128170659065, -159.99237607339185, -152.4339626104708]
FINAL_DERIVATIVE_VALUES 
-0.144382  -0.0211844  -0.482927  -0.00100686  0.00311716  0.000725998  
  26     -1.16661   [-0.08591999949873336, -0.015204051010378321, 0.17270317938890692, 120.20077944284702, -159.99082110532402, -152.43360045243102]
FINAL_DERIVATIVE_VALUES 
0.155654  0.0428602  0.714834  -0.000428069  0.00370234  0.000911164  
  27     -1.16644   [-0.0858112192415147, -0.015098593075062306, 0.17326946449272357, 120.2006062862153, -159.98932348589196, -152.43323188107692]
FINAL_DERIVATIVE_VALUES 
-0.0973116  -0.0236706  -0.739644  -0.00134866  0.00235621  0.000231699  
  28     -1.16654   [-0.08588869807714643, -0.015198662727942867, 0.17256873597194233, 120.1999546058193, -159.98818495263058, -152.433119923007]
FINAL_DERIVATIVE_VALUES 
0.102371  -0.0464925  1.07982  0.000343689  0.00292836  0.000857558  
  29      -1.1663   [-0.08582106411382068, -0.01515106876131741, 0.17335086760598375, 120.2000808176821, -159.98710957950686, -152.4328050047076]
FINAL_DERIVATIVE_VALUES 
-0.070506  0.14596  -0.99538  -0.00199607  0.00172391  6.68793e-06  
  30     -1.16644   [-0.08588537756059039, -0.015114154840541264, 0.17254838359436023, 120.19927192854034, -159.9864109806631, -152.43280229448052]
FINAL_DERIVATIVE_VALUES 
0.0772305  -0.293635  1.15228  0.00130469  0.00222696  0.000541676  
  31      -1.1663   [-0.08580716625641682, -0.015240217516289702, 0.17337793742987112, 120.19973971801596, -159.98561251444275, -152.43260807923272]
FINAL_DERIVATIVE_VALUES 
-0.104474  0.39676  -1.10813  -0.00245048  0.00113977  0.000169537  
METRIC       THREADS USED: 4 of 4
REGISTRATION THREADS USED: 4 of 4
Stop condition from optimizer.RegularStepGradientDescentOptimizerv4: Step too small after 32 iterations. Current step (0.000976562) is less than minimum step (0.001).



=============================== ITKv4 Registration: Starting Transform Estimations for Affine(2 of 2).===============================

FINAL_DERIVATIVE_VALUES 
-0.411409  -0.0385918  0.160879  -0.318148  -0.908247  -0.339523  -0.0735452  -0.00119246  -0.988074  -0.00399765  0.00520703  -0.000163411  
   0     -1.16424   [0.9397836518785314, -0.336272990040891, -0.05947419762565486, 0.34141307509014196, 0.9254191929299085, 0.16326376959937086, 0.00013802202823438034, -0.17384427568286093, 0.9845486905866949, 120.1844451627927, -159.96954533558642, -152.43426814391057]
FINAL_DERIVATIVE_VALUES 
-0.255067  0.0148999  0.179442  -0.163088  -0.670524  -0.324348  -0.0709605  -0.123881  -0.622265  0.00113645  -0.000226871  0.00365362  
   1     -1.16454   [0.9397389035107327, -0.3362732705477805, -0.05945081411531938, 0.34138182626486363, 0.9253391909986117, 0.16321934987476938, 0.00012753094089993852, -0.17385322163881556, 0.9844485929673655, 120.18312193885289, -159.96577745394646, -152.4284387121223]
FINAL_DERIVATIVE_VALUES 
-0.174097  0.000787759  0.151397  -0.102706  -0.533553  -0.30648  -0.0752075  -0.110888  -0.498842  0.00147783  -0.00108947  0.00282592  
   2     -1.16457   [0.9397362957341121, -0.3362732619143067, -0.05944872047158959, 0.3413802878484957, 0.9253333435025919, 0.16321511161453128, 0.0001264044169926507, -0.17385443692317848, 0.984441694560344, 120.18348947339256, -159.9660484041796, -152.42773591157206]
FINAL_DERIVATIVE_VALUES 
-0.169547  -0.00190546  0.150144  -0.100123  -0.523083  -0.304193  -0.0744056  -0.10716  -0.492475  0.00131165  -0.00097863  0.00264681  
   3     -1.16458   [0.939733756115871, -0.336273282797269, -0.059446644151299174, 0.34137878811193584, 0.9253276107499179, 0.1632109049825066, 0.00012528990452250147, -0.1738556113443178, 0.9844348842027156, 120.18381567838925, -159.9662917875302, -152.427077655375]
FINAL_DERIVATIVE_VALUES 
-0.164961  -0.00449417  0.148883  -0.0973483  -0.512826  -0.301841  -0.0735154  -0.103771  -0.485959  0.00116476  -0.000877128  0.0024854  
   4     -1.16457   [0.9397321601469685, -0.33627331461032806, -0.059445314321909894, 0.3413778462841142, 0.9253239805846081, 0.1632082089319156, 0.0001245786552145509, -0.17385634591005097, 0.9844305436050024, 120.1840027778229, -159.96643268364483, -152.42667841652815]
FINAL_DERIVATIVE_VALUES 
-0.16199  -0.00609164  0.148056  -0.0954784  -0.506357  -0.300286  -0.0729188  -0.101758  -0.481672  0.00108052  -0.000817253  0.00239121  
   5     -1.16457   [0.9397305929199566, -0.3362733577315149, -0.059443991880630744, 0.34137692254738927, 0.9253203962118013, 0.16320552677358774, 0.00012387317805097553, -0.17385706623082114, 0.9844262413002706, 120.18417634627559, -159.96656396192606, -152.42629430772888]
FINAL_DERIVATIVE_VALUES 
-0.159019  -0.0076514  0.147215  -0.093567  -0.499985  -0.298701  -0.0723161  -0.0998455  -0.477358  0.00100241  -0.000760893  0.00230297  
   6     -1.16457   [0.9397287606128749, -0.33627342223793205, -0.05944242582865139, 0.34137584441645685, 0.9253161810042543, 0.16320234922782645, 0.00012303991133585826, -0.17385790799538475, 0.9844211632220247, 120.18436811977709, -159.9667095298057, -152.4258537226022]
FINAL_DERIVATIVE_VALUES 
-0.155483  -0.00946472  0.146196  -0.0912478  -0.492508  -0.296781  -0.0715949  -0.0976869  -0.472199  0.000916174  -0.000697706  0.00220454  
   7     -1.16457   [0.9397283186157738, -0.33627344192397235, -0.059442042139887775, 0.3413755850223211, 0.9253151566170948, 0.16320157033048596, 0.00012283638557471563, -0.17385811117815578, 0.9844199239435047, 120.18441136202718, -159.96674246065385, -152.42574967082749]
FINAL_DERIVATIVE_VALUES 
-0.154614  -0.00989825  0.14594  -0.0906672  -0.490692  -0.2963  -0.0714173  -0.097182  -0.470912  0.000896703  -0.000683152  0.00218196  
   8     -1.16457   [0.9397277951452468, -0.33627346644364786, -0.05944158597246603, 0.34137527805408424, 0.9253139410876311, 0.16320064418046365, 0.00012259459090237096, -0.17385835191471988, 0.9844184520062, 120.1844617682999, -159.96678086262273, -152.4256270164413]
FINAL_DERIVATIVE_VALUES 
-0.153577  -0.0104131  0.145634  -0.0899729  -0.488534  -0.29572  -0.0712011  -0.096589  -0.469376  0.000873977  -0.000666118  0.00215552  
   9     -1.16456   [0.939727175881291, -0.336273497165192, -0.05944104382332621, 0.3413749152599998, 0.9253124997808597, 0.16319954330966274, 0.00012230748956813524, -0.17385863687828465, 0.9844167046734044, 120.1845202798622, -159.96682545830308, -152.4254827075]
FINAL_DERIVATIVE_VALUES 
-0.152343  -0.0110223  0.14527  -0.0891416  -0.48598  -0.29503  -0.0709421  -0.0958972  -0.467552  0.000847622  -0.000646223  0.00212472  
METRIC       THREADS USED: 4 of 4
REGISTRATION THREADS USED: 4 of 4
Stop condition from optimizer.ConjugateGradientLineSearchOptimizerv4Template: Convergence checker passed at iteration 10.

In [7]:
outputBFit = sitk.ReadImage(BFitOut,sitk.sitkFloat32)
myshow3d(outputBFit, xslices=[], yslices=[], zslices=[], title="OutputVolume of BRANSFit")



In [8]:
# Stage 1
# Set the registration filter
R = sitk.ImageRegistrationMethod()
# Transform of the first stage
tx1 = sitk.VersorRigid3DTransform()
tx1.SetFixedParameters(initialMovingTx.GetFixedParameters())
R.SetInitialTransform(tx1,inPlace=False)
# Set the initial transform of this stage
R.SetMovingInitialTransform(ctx)
# Set the other components of the registration filter
R.SetMetricFixedMask(fixedMask)
R.SetMetricMovingMask(movingMask)
R.SetMetricAsMattesMutualInformation( 200 )
R.SetOptimizerAsRegularStepGradientDescent(learningRate =1.0,
                                           minStep=1e-3,
                                           numberOfIterations=250,
                                           gradientMagnitudeTolerance=1e-4,
                                           estimateLearningRate=R.Never)
R.SetOptimizerScales([1, 1, 1, 1.0/250, 1.0/250, 1.0/250])
R.SetInterpolator(sitk.sitkLinear)
R.SetMetricSamplingPercentage(0.5)
R.SetMetricSamplingStrategy(R.RANDOM)
R.SetShrinkFactorsPerLevel([1])
R.SetSmoothingSigmasPerLevel([0])


Out[8]:
<SimpleITK.SimpleITK.ImageRegistrationMethod; proxy of <Swig Object of type 'itk::simple::ImageRegistrationMethod *' at 0x11a92d5a0> >

In [9]:
R.RemoveAllCommands()
R.AddCommand( sitk.sitkIterationEvent, lambda: command_iteration(R) )
outTx = R.Execute(sitk.Cast(fixed,sitk.sitkFloat32), sitk.Cast(moving,sitk.sitkFloat32))

print("-------")
print(outTx)
print("Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
print(" Iteration: {0}".format(R.GetOptimizerIteration()))
print(" Metric value: {0}".format(R.GetMetricValue()))


0 = -1.61071246208 : (0.0207883283878909, 0.10666306698620878, -0.05212441319506481, 0.03403218046725581, -0.267680985148973, -0.9320947810530252)
1 = -0.461561935438 : (0.028296848744853116, 0.021172317450337957, -0.020656227198795573, -0.2645629178424874, -0.5832731715390473, -0.7658679664714625)
2 = -0.865141490208 : (-0.0767001400894634, -0.04472710962880101, 0.09452071191372974, -0.3263982344157563, -0.4464914688315056, -0.43056365597905705)
3 = -0.463996080608 : (-0.04630529539682772, 0.018444826796492952, 0.03052845164570188, 0.10974525840528615, -0.29644364773007503, -0.39866516942714036)
4 = -0.696075191739 : (-0.015566754413645762, -0.01724036556674941, -0.013976777693286462, -0.09285924763536799, -0.32332643022601176, -0.33605834759434355)
5 = -0.99261641116 : (-0.006552790900131108, 0.007289949061220859, 0.007441989665795373, 0.00777287729450922, -0.3208890454751712, -0.30565352073645624)
6 = -1.35412034638 : (-0.0005430630855413499, -0.0031316659997659797, -0.005931939295334754, -0.03913327195775118, -0.3081519351187177, -0.28986024547715983)
7 = -1.47727245837 : (-0.00013133497067086334, 0.0013623775510663706, 0.0008129662305759178, -0.016876172188993618, -0.30166179626300654, -0.27660807428087436)
8 = -1.58778816337 : (-0.0009088393117061225, -6.870009307022003e-05, -0.0027505000731809152, -0.02359623340863515, -0.2817508853953601, -0.25484674069517194)
9 = -1.57282284949 : (-0.0003419312054707839, 0.003088645007038421, 0.002364677585003936, -0.007728151409311122, -0.27117346241480494, -0.2332350983380488)
10 = -1.5681698574 : (-0.0006193155494422547, -0.001599684896282347, -0.0031076212690594623, -0.02764405858255022, -0.25981432104994917, -0.2176506700832972)
11 = -1.55909459929 : (-0.0005435798742349365, 0.0007767735044891949, -0.0005933194177093204, -0.016361569806450954, -0.25627271970525095, -0.2101404811082677)
12 = -1.59774044982 : (-0.0003729824861345376, 0.001068391009281243, -0.00014868182523253065, -0.015668375688929266, -0.2441492427128606, -0.20037144711359706)
13 = -1.60022270031 : (-0.0003304504184520399, 0.0006240734303538129, -0.00037280946243103627, -0.017356914840798033, -0.23088727074284118, -0.19234575596615738)
14 = -1.6010479792 : (-0.0004026528517543763, 0.0012037912811916445, -0.00022481925277990567, -0.015735224036791704, -0.21853077988102623, -0.1829982895658522)
15 = -1.60243758756 : (-0.00010314327637696259, 0.0003836740130550555, -0.0003941061788047185, -0.01825971742351025, -0.2048980284760573, -0.17601603923281747)
16 = -1.60296107432 : (-0.0004743367210587064, 0.0015594663199147603, -0.00017755099688974489, -0.014656044315398457, -0.19306545027190186, -0.16680324494251167)
17 = -1.60342989149 : (0.000105117706060079, -0.0003186598692363361, -0.0005680735304225797, -0.020505602585394682, -0.18079952278992162, -0.16021478741876732)
18 = -1.60163604038 : (-0.00044247782968396946, 0.002059007568381752, 0.00012166582057679767, -0.012476521714022207, -0.17143336067584933, -0.15207636939249938)
19 = -1.60205419115 : (9.479176259016153e-05, -0.0009181396585415513, -0.0009213726298034591, -0.022839495413815802, -0.163649228864994, -0.1461436906720751)
20 = -1.59739376612 : (-0.0001675327175541644, 0.0004582149465178234, -0.00034465520256141895, -0.017766077873757505, -0.16002982724448908, -0.14253641433531203)
21 = -1.6074663862 : (-0.00020467504883962142, 0.0011278008570267996, -0.00021442452935534895, -0.015700956086306797, -0.1536321836880063, -0.1387982537437473)
22 = -1.60796208577 : (-5.529134301979777e-05, 0.0006262436298908414, -0.00041561301040166196, -0.0174295930873749, -0.14656528669449642, -0.13618128435197527)
23 = -1.60829013698 : (-0.00015771170544806817, 0.0011468453295814028, -0.00025723590013977844, -0.016218781761886184, -0.13981099123043148, -0.13261446689710127)
24 = -1.60858058252 : (-2.372195141714053e-05, 0.0005963011722030358, -0.0004228107585881038, -0.018388464862170485, -0.13289134590154883, -0.12995885574552457)
25 = -1.60898819209 : (-0.00013179721346724685, 0.0012307918015384144, -0.00022018492862768205, -0.016942061105732494, -0.1262792565477375, -0.12629797029989911)
26 = -1.60902952061 : (1.3303626692380273e-06, 0.0004349991618477943, -0.0004930636878269056, -0.020033617990468485, -0.11979101995177824, -0.12375271395080996)
27 = -1.60924112612 : (-0.00014841843440732664, 0.0014440774715582289, -9.50915161874489e-05, -0.017443321111348484, -0.11395840646882152, -0.11981444623212562)
28 = -1.60881023955 : (-2.023836608384052e-05, 0.00013920982105507088, -0.0006463114116847868, -0.022148473318212108, -0.1088365856831412, -0.11767626142650867)
29 = -1.60849477805 : (-0.00017400869713592458, 0.0015887315459396393, 2.7894480135261723e-05, -0.017809891916600838, -0.10460211020439089, -0.11393944060276451)
30 = -1.60847342569 : (-0.00010639963566804894, 0.0008067033496115006, -0.0003451648651686447, -0.020621254514116064, -0.10269831334129277, -0.11309722083671671)
31 = -1.61052409692 : (3.458661145028129e-05, 0.0009401541349732473, -0.0002960940933015274, -0.020651472811394202, -0.09916971251788942, -0.11147042170643026)
32 = -1.61067529998 : (3.1954014533513755e-05, 0.0008570785405209693, -0.00035678371293101506, -0.021432526559429278, -0.09554349119553639, -0.11026340217269325)
33 = -1.61074164726 : (3.164718834076543e-05, 0.0009320070043155466, -0.00031473750719498945, -0.021782148960406932, -0.09192720376341017, -0.10883880067498025)
34 = -1.61084864663 : (4.909877803637336e-05, 0.0008665144427537087, -0.00034983504198463273, -0.02257661867311225, -0.08832399702996807, -0.10756553499834547)
35 = -1.61093605448 : (4.5803242015227e-05, 0.0009371858413918259, -0.000308973842708112, -0.022997522250995158, -0.08471172057966286, -0.10614906463221155)
36 = -1.61102615415 : (6.55531564317995e-05, 0.0008597289421550009, -0.00035487909629192344, -0.02387528167377021, -0.08112232922328604, -0.10489582505137846)
37 = -1.61109007636 : (5.477150949285831e-05, 0.0009680564531421626, -0.00028794261169327797, -0.0242541621566101, -0.07752616584092532, -0.10344065454596849)
38 = -1.61116880648 : (8.502818174424925e-05, 0.0008144693789580708, -0.0003871353750636368, -0.02538440276502798, -0.07399061722237123, -0.10228140197880178)
39 = -1.61117284534 : (4.4546749759275766e-05, 0.0010874828206201813, -0.00020628200622793978, -0.025349757485913373, -0.07050165695164455, -0.10065376174817832)
40 = -1.61119106039 : (0.00010543091086785355, 0.0006301606938942709, -0.0005220779044602106, -0.02726199932288264, -0.0673928571048923, -0.09982480241916553)
41 = -1.61088716734 : (-1.2305588289468326e-05, 0.0014000127575163062, 1.857170398029723e-05, -0.025533826592491416, -0.06512908450001396, -0.09794007553339178)
42 = -1.61038432255 : (9.271234601313932e-06, 0.0009570309496533971, -0.00027229562270999135, -0.026925515619457285, -0.06427561559818844, -0.0977842557305887)
43 = -1.6114001572 : (0.00011512380564144882, 0.0008732336128632278, -0.0003718481357526666, -0.027565546838244706, -0.06255626442252875, -0.0972043083363362)
44 = -1.61135068971 : (7.42839478934574e-05, 0.0009745899363869349, -0.000275116567200608, -0.027724287434659496, -0.06075712751057672, -0.09652063219350361)
45 = -1.61144526208 : (0.00010668535846941257, 0.0008630907797006957, -0.00037416161971760755, -0.028437275586693855, -0.05901874272291007, -0.09608337239744226)
46 = -1.61137707364 : (7.89456833220366e-05, 0.00100995219824618, -0.0002504271330988133, -0.02848540069009244, -0.05725754708149005, -0.09533505484516037)
47 = -1.61146600205 : (0.00011300990539841918, 0.0008238535907384393, -0.00041017130085367306, -0.029378946735891937, -0.05562604375572727, -0.0950045635054793)
48 = -1.61133362818 : (6.667260675506526e-05, 0.0011045654085092558, -0.0001721600488332799, -0.02902574818167079, -0.05409040863248455, -0.094120600752056)
49 = -1.61141459444 : (0.00010899125356282296, 0.0007683042912873081, -0.000460122439832345, -0.030215342108258807, -0.052827902095127256, -0.09400017812172996)
50 = -1.61122148011 : (6.01628162130897e-05, 0.0011826891491278884, -0.00011117519421051088, -0.02936422928405644, -0.051802449078670335, -0.09307535537156171)
-------
CompositeTransform (0x7fb885e787d0)
  RTTI typeinfo:   itk::CompositeTransform<double, 3u>
  Reference Count: 2
  Modified Time: 533548
  Debug: Off
  Object Name: 
  Observers: 
    none
  Transforms in queue, from begin to end:
  >>>>>>>>>
  VersorRigid3DTransform (0x7fb8867443d0)
    RTTI typeinfo:   itk::VersorRigid3DTransform<double>
    Reference Count: 1
    Modified Time: 533485
    Debug: Off
    Object Name: 
    Observers: 
      none
    Matrix: 
      0.999997 0.000222493 0.00236536 
      -0.000222208 1 -0.000120589 
      -0.00236539 0.000120063 0.999997 
    Offset: [-0.0198927, -0.052345, -0.0937215]
    Center: [0.884831, 22.6711, -6.13573]
    Translation: [-0.0293642, -0.0518024, -0.0930754]
    Inverse: 
      0.999997 -0.000222208 -0.00236539 
      0.000222493 1 0.000120063 
      0.00236536 -0.000120589 0.999997 
    Singular: 0
    Versor: [ 6.01628e-05, 0.00118269, -0.000111175, 0.999999 ]
  End of MultiTransform.
<<<<<<<<<<
  TransformsToOptimizeFlags, begin() to end(): 
    1 
  TransformsToOptimize in queue, from begin to end:
  End of TransformsToOptimizeQueue.
<<<<<<<<<<
  End of CompositeTransform.
<<<<<<<<<<

Optimizer stop condition: RegularStepGradientDescentOptimizerv4: Step too small after 51 iterations. Current step (0.000976562) is less than minimum step (0.001).
 Iteration: 52
 Metric value: -1.61129633136

In [10]:
# Update the composite transform with the output of the first stage
ctx.AddTransform(outTx)
cshow3d(fixed,moving,ctx)


<bound method ResampleImageFilter.GetTransform of <SimpleITK.SimpleITK.ResampleImageFilter; proxy of <Swig Object of type 'itk::simple::ResampleImageFilter::Self *' at 0x11a92da50> >>

In [11]:
# Stage 2
tx2 = sitk.AffineTransform(fixed.GetDimension())
tx2.SetFixedParameters(tx1.GetFixedParameters())
R.SetInitialTransform(tx2,inPlace=False)
R.SetMovingInitialTransform(ctx)
# Set a new optimizer
R.SetOptimizerAsConjugateGradientLineSearch(learningRate=1.0,
                                            numberOfIterations=250,
                                            convergenceMinimumValue=1e-6,
                                            convergenceWindowSize=10,
                                            lineSearchLowerLimit=0,
                                            lineSearchUpperLimit=2,
                                            lineSearchEpsilon=0.2,
                                            lineSearchMaximumIterations=40,
                                            estimateLearningRate=R.EachIteration,
                                            maximumStepSizeInPhysicalUnits=1.0)
R.SetOptimizerScalesFromPhysicalShift()
affineOut = R.Execute(fixed,moving)
print("-------")
print(affineOut)
print("Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
print(" Iteration: {0}".format(R.GetOptimizerIteration()))
print(" Metric value: {0}".format(R.GetMetricValue()))


0 = -1.60437511691 : (0.9997913842903147, -3.402545458222812e-06, -0.00014975125415448427, -2.484856174779254e-05, 0.9997597134654345, 6.3852443646940875e-06, 6.336010461067638e-05, 3.513809219921322e-05, 0.9996726709746477, 0.016785733036460103, -0.002551605019812476, 0.07554817604777342)
1 = -1.60620012096 : (0.9990986433832021, 9.684550406806465e-06, -0.0006935005665822481, -6.975640221985936e-05, 0.9989432877359379, 1.8482318432839108e-05, 0.0002677935670007499, 0.00013812747122599238, 0.9984446975015724, 0.03320234490818754, 0.018755433627394458, 0.12061189534117234)
2 = -1.61039375047 : (0.9981902538817846, 1.0109702313219253e-06, -0.0013266719452261731, -0.0001321996598711118, 0.9980546530318875, -0.00010274994026618494, 0.0005377138238744687, 0.0001244467114806972, 0.9969548684872698, 0.03479380108390189, -0.0012491956864646193, 0.0561446719755255)
3 = -1.61419334292 : (0.9976087894823082, -3.2598088771230057e-06, -0.0017264673133230535, -0.0001680800292193891, 0.9975084793161408, -0.000192222342943906, 0.0006893979242172027, 9.183325049402327e-05, 0.9960327318117411, 0.0379757836532805, -0.019875511861737104, 0.04235610697969129)
4 = -1.61478822071 : (0.9970056283672747, -7.925009187708565e-07, -0.00213296714705695, -0.00020115946130600163, 0.9969806638095573, -0.0002982830735178338, 0.0008080101989927936, 1.2070795181502646e-05, 0.9951114746582208, 0.049379633155941016, -0.043344037935947384, 0.08763199948771311)
5 = -1.61714430315 : (0.9969608299006918, -5.600572591459092e-06, -0.0021464869362103696, -0.00020017100292496275, 0.9969844452089759, -0.0003119246019149777, 0.0007906629943087447, -4.2833667791942e-05, 0.9950688730711833, 0.06415539579336628, -0.040107752456341635, 0.1382759036927886)
6 = -1.6180843054 : (0.9968964419172927, -7.98913261176715e-06, -0.002196075657911988, -0.00019885655303905365, 0.9969699651296655, -0.00032821689326296885, 0.0007743549207287706, -0.0001057951421405843, 0.9949685705022252, 0.06399664051815965, -0.03343210594933866, 0.15067258760434207)
7 = -1.61812743221 : (0.9967964199252217, -2.0752296786480852e-05, -0.002266921108602735, -0.00019739096046374912, 0.9969367639615699, -0.00036487633601587824, 0.0007567344624779545, -0.0001994429586019769, 0.9947821216471511, 0.06825791938172321, -0.028103140492678112, 0.15032697743060777)
8 = -1.61816674499 : (0.996682877624559, -3.5174770909135775e-05, -0.002357967744669759, -0.00019396695585306733, 0.9968992760123125, -0.0004131629583906201, 0.0007341880693570202, -0.00031434839378612447, 0.9945567763228356, 0.07069635212874979, -0.024508917592453692, 0.15347417550858475)
9 = -1.61819156149 : (0.9966573864162798, -3.8919210517159184e-05, -0.002381855488346974, -0.00019250432415880242, 0.9968904805037567, -0.0004266235694841229, 0.000728556141426757, -0.00034304309980858047, 0.9944996590194873, 0.07089670628485274, -0.02448973075273489, 0.1552597082697841)
10 = -1.61819431919 : (0.9966545170114938, -3.959367815661436e-05, -0.002385651153158383, -0.00019209100160620193, 0.9968892595027915, -0.00042901203301296236, 0.0007278145500760613, -0.00034715340098440595, 0.9944907283982068, 0.07088297382134738, -0.02471285476973712, 0.15574476196861992)
11 = -1.61819439029 : (0.9966544138121683, -3.9618302807580035e-05, -0.0023857858956389654, -0.00019207627594595368, 0.9968892145294461, -0.00042909631271853066, 0.0007277890208335134, -0.00034729881653791433, 0.99449040710465, 0.0708831849819844, -0.02471991702943013, 0.15576070495874503)
12 = -1.61819438148 : (0.9966543106208554, -3.964293696913182e-05, -0.002385920576877098, -0.00019206155472394247, 0.9968891695253892, -0.0004291805367645884, 0.0007277635286231291, -0.00034744414730957727, 0.9944900858234559, 0.0708834176208377, -0.02472694769442553, 0.15577660053986744)
13 = -1.61819437629 : (0.9966541361391413, -3.968460596541534e-05, -0.00238614822157176, -0.00019203666607538436, 0.9968890933716688, -0.00042932286467519857, 0.0007277204866579797, -0.00034768975655247847, 0.9944895425620092, 0.07088384524340531, -0.02473878434340774, 0.15580339925381267)
14 = -1.61819436695 : (0.9966539616825525, -3.972630215610765e-05, -0.0023863756960532745, -0.0001920117894907393, 0.9968890171317134, -0.0004294650338221029, 0.0007276775523573201, -0.00034793512367242886, 0.9944889993359618, 0.07088433224852946, -0.0247505323406224, 0.15583006263410198)
15 = -1.61819435295 : (0.9966538078376526, -3.976309941585417e-05, -0.0023865761703028513, -0.0001919898589734578, 0.9968889498132192, -0.0004295902835335633, 0.0007276397806930898, -0.00034815131660205224, 0.9944885202593875, 0.07088481437688594, -0.024760816106918566, 0.155853461046476)
16 = -1.6181943453 : (0.9966536721673132, -3.979557222353717e-05, -0.002386752868487169, -0.0001919705235299314, 0.9968888903825238, -0.0004297006435531427, 0.0007276065388028277, -0.00034834182748799306, 0.9944880977550394, 0.0708852787449646, -0.02476982697288442, 0.15587400577615257)
-------
CompositeTransform (0x7fb885de6cd0)
  RTTI typeinfo:   itk::CompositeTransform<double, 3u>
  Reference Count: 2
  Modified Time: 1072156
  Debug: Off
  Object Name: 
  Observers: 
    none
  Transforms in queue, from begin to end:
  >>>>>>>>>
  AffineTransform (0x7fb8862e7600)
    RTTI typeinfo:   itk::AffineTransform<double, 3u>
    Reference Count: 1
    Modified Time: 1072148
    Debug: Off
    Object Name: 
    Observers: 
      none
    Matrix: 
      0.996654 -3.96183e-05 -0.00238579 
      -0.000192076 0.996889 -0.000429096 
      0.000727789 -0.000347299 0.99449 
    Offset: [0.0601031, 0.0433422, 0.129185]
    Center: [0.884831, 22.6711, -6.13573]
    Translation: [0.0708832, -0.0247199, 0.155761]
    Inverse: 
      1.00336 4.07138e-05 0.00240707 
      0.000193006 1.00312 0.000433283 
      -0.000734209 0.000350283 1.00554 
    Singular: 0
  End of MultiTransform.
<<<<<<<<<<
  TransformsToOptimizeFlags, begin() to end(): 
    1 
  TransformsToOptimize in queue, from begin to end:
  End of TransformsToOptimizeQueue.
<<<<<<<<<<
  End of CompositeTransform.
<<<<<<<<<<

Optimizer stop condition: ConjugateGradientLineSearchOptimizerv4Template: Convergence checker passed at iteration 17.
 Iteration: 17
 Metric value: -1.61819439029

In [12]:
# Update the composite transform with the output of the second stage
ctx.AddTransform(affineOut)
cshow3d(fixed,moving,ctx)


<bound method ResampleImageFilter.GetTransform of <SimpleITK.SimpleITK.ResampleImageFilter; proxy of <Swig Object of type 'itk::simple::ResampleImageFilter::Self *' at 0x11a95b4e0> >>

In [13]:
compare_with_baseline(fixed,moving,outputBFit,transform=ctx,numberOfPixelsTolerance=300,radiusTolerance=1,intensityTolerance=10)


<DartMeasurement name="ImageError baseTV2_d0.nii.gz" type="numeric/double">110</DartMeasurement>
<DartMeasurement name="BaselineImageName" type="text/string">baseTV2_d0.nii.gz</DartMeasurement>
Exit code: 
Test passed.