.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated\examples\tutorials\plot_coreg_pairwise.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_examples_tutorials_plot_coreg_pairwise.py: ============================================== Pairwise coregistration API ============================================== ``mdreg`` includes a harmonized API for pairwise coregistration of 2D images or 3D volumes with three different packages: ``ants``, ``skimage`` and ``itk-elastix``. This examples illustrates their usage for the example of pairwise 3D registration. .. GENERATED FROM PYTHON SOURCE LINES 15-17 Setup ----- .. GENERATED FROM PYTHON SOURCE LINES 17-23 .. code-block:: Python import os import time import numpy as np import mdreg .. GENERATED FROM PYTHON SOURCE LINES 24-25 Load test data .. GENERATED FROM PYTHON SOURCE LINES 25-35 .. code-block:: Python data = mdreg.fetch('VFA') # Select first and last volume to coregister fixed = data['array'][:,:,:,0] moving = data['array'][:,:,:,-1] # Relevant header data FA = data['FA'] # The FA values in degrees spacing = data['spacing'] # (x,y,z) voxel size in mm. .. GENERATED FROM PYTHON SOURCE LINES 36-40 Check alignment --------------- On the difference image, the effect of breathing motion can be clearly seen as a white line at the edge of the liver: .. GENERATED FROM PYTHON SOURCE LINES 40-55 .. code-block:: Python # Difference image diff = fixed - moving # Keep the same scaling throughout this example v = np.percentile(diff, [1, 99]) # Display difference fig = mdreg.plot.par( diff, title='Difference without coregistration', vmin=v[0], vmax=v[1], ) .. image-sg:: /generated/examples/tutorials/images/sphx_glr_plot_coreg_pairwise_001.png :alt: Difference without coregistration , Slice 1, Slice 2, Slice 3, Slice 4, Slice 5, Slice 6, Slice 7, Slice 8, Slice 9, Slice 10, Slice 11, Slice 12, Slice 13, Slice 14, Slice 15, Slice 16, Slice 17, Slice 18, Slice 19, Slice 20, Slice 21, Slice 22, Slice 23, Slice 24, Slice 25, Slice 26, Slice 27, Slice 28, Slice 29, Slice 30, Slice 31, Slice 32, Slice 33, Slice 34, Slice 35, Slice 36 :srcset: /generated/examples/tutorials/images/sphx_glr_plot_coreg_pairwise_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 56-59 Coregister with elastix ----------------------- We first use elastix to coregister the images: .. GENERATED FROM PYTHON SOURCE LINES 59-71 .. code-block:: Python t = time.time() coreg, deform = mdreg.elastix.coreg( moving, fixed, spacing=spacing, FinalGridSpacingInPhysicalUnits=50.0, ) print(f"elastix computation time: {round(time.time()-t)} seconds.") .. rst-class:: sphx-glr-script-out .. code-block:: none elastix computation time: 4 seconds. .. GENERATED FROM PYTHON SOURCE LINES 72-77 We used here the default b-spline registration method, but since this is abdominal motion we used a coarser grid spacing than the elastix default of 16mm (note: you can use `mdreg.elastix.defaults` to find out what the defaults are). Any other defaults can be overridden by specifying additional keywords. .. GENERATED FROM PYTHON SOURCE LINES 80-82 We check the result by plotting the difference with the coregistered (deformed) moving image: .. GENERATED FROM PYTHON SOURCE LINES 82-90 .. code-block:: Python fig = mdreg.plot.par( fixed - coreg, title='Difference with elastix coregistration', vmin=v[0], vmax=v[1], ) .. image-sg:: /generated/examples/tutorials/images/sphx_glr_plot_coreg_pairwise_002.png :alt: Difference with elastix coregistration , Slice 1, Slice 2, Slice 3, Slice 4, Slice 5, Slice 6, Slice 7, Slice 8, Slice 9, Slice 10, Slice 11, Slice 12, Slice 13, Slice 14, Slice 15, Slice 16, Slice 17, Slice 18, Slice 19, Slice 20, Slice 21, Slice 22, Slice 23, Slice 24, Slice 25, Slice 26, Slice 27, Slice 28, Slice 29, Slice 30, Slice 31, Slice 32, Slice 33, Slice 34, Slice 35, Slice 36 :srcset: /generated/examples/tutorials/images/sphx_glr_plot_coreg_pairwise_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 91-94 The results clearly show the effect of the registration: the white line at the top of the liver is gone but the gallbladder is deformed in an unphysical way. .. GENERATED FROM PYTHON SOURCE LINES 96-100 Apart from the coregistered image, the function also returned the transformation parameters. These can be used to deform other images in the same way. As an example, we can check that transforming the moving image does indeed produce the coregistered image: .. GENERATED FROM PYTHON SOURCE LINES 100-110 .. code-block:: Python # Deform the moving image deformed = mdreg.elastix.transform(moving, deform, spacing) # Check the difference with the coregistered image err = 100*np.linalg.norm(deformed-coreg)/np.linalg.norm(moving) print(f"Difference between coregistered and deformed: {err} %") .. rst-class:: sphx-glr-script-out .. code-block:: none Difference between coregistered and deformed: 0.0 % .. GENERATED FROM PYTHON SOURCE LINES 111-120 Coregister with skimage ----------------------- We could try to improve the elastix coregistration by modifying the parameters, but for the purpose of this tutorial we try another package instead. `skimage` has an implementation of the optical flow method for registration which is wrapped by mdreg with the same API as elastix and ants. Let's try it on our problem: .. GENERATED FROM PYTHON SOURCE LINES 120-131 .. code-block:: Python t = time.time() coreg, deform = mdreg.skimage.coreg( moving, fixed, attachment=30.0, ) print(f"skimage computation time: {round(time.time()-t)} seconds.") .. rst-class:: sphx-glr-script-out .. code-block:: none skimage computation time: 2 seconds. .. GENERATED FROM PYTHON SOURCE LINES 132-136 We chose to use a coarser registration than the default by setting the attachment to a higher value - 30 instead of the default 15 (note: as in elastix you can find the default settings by calling `mdreg.skimage.defaults`). .. GENERATED FROM PYTHON SOURCE LINES 138-139 Plot the difference with the coregistered (deformed) moving image: .. GENERATED FROM PYTHON SOURCE LINES 139-147 .. code-block:: Python fig = mdreg.plot.par( fixed - coreg, title='Difference with skimage coregistration', vmin=v[0], vmax=v[1], ) .. image-sg:: /generated/examples/tutorials/images/sphx_glr_plot_coreg_pairwise_003.png :alt: Difference with skimage coregistration , Slice 1, Slice 2, Slice 3, Slice 4, Slice 5, Slice 6, Slice 7, Slice 8, Slice 9, Slice 10, Slice 11, Slice 12, Slice 13, Slice 14, Slice 15, Slice 16, Slice 17, Slice 18, Slice 19, Slice 20, Slice 21, Slice 22, Slice 23, Slice 24, Slice 25, Slice 26, Slice 27, Slice 28, Slice 29, Slice 30, Slice 31, Slice 32, Slice 33, Slice 34, Slice 35, Slice 36 :srcset: /generated/examples/tutorials/images/sphx_glr_plot_coreg_pairwise_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 148-150 This appears to have done a reasonable job at minimizing the difference between the images without creating unwanted deformations. .. GENERATED FROM PYTHON SOURCE LINES 152-155 In `skimage.coreg` the second return value is the deformation field. As in elastix we can use it to deform other images in the same way. If we try this on the moving image, we get the coregistered image again: .. GENERATED FROM PYTHON SOURCE LINES 155-162 .. code-block:: Python deformed = mdreg.skimage.transform(moving, deform) # Check the difference with the coregistered image err = 100*np.linalg.norm(deformed-coreg)/np.linalg.norm(moving) print(f"Difference between coregistered and deformed: {err} %") .. rst-class:: sphx-glr-script-out .. code-block:: none Difference between coregistered and deformed: 0.0044718291610479355 % .. GENERATED FROM PYTHON SOURCE LINES 163-167 Coregister with ants -------------------- Let's run this a final time with the third package wrapped in mdreg - ``ants``: .. GENERATED FROM PYTHON SOURCE LINES 167-184 .. code-block:: Python t = time.time() coreg, deform = mdreg.ants.coreg( moving, fixed, type_of_transform='SyNOnly', ) print(f"ANTs computation time: {round(time.time()-t)} seconds.") ## %% # We have used default settings for all parameters except the type # of transform. By default the transform is a two-stage process with affine # pre-alignment followed by deformable registration. Here we tried deformable # registration alone, which is more similar to what we have done with elastix. .. rst-class:: sphx-glr-script-out .. code-block:: none ANTs computation time: 1 seconds. .. GENERATED FROM PYTHON SOURCE LINES 185-186 Plot the difference with the coregistered (deformed) moving image: .. GENERATED FROM PYTHON SOURCE LINES 186-194 .. code-block:: Python fig = mdreg.plot.par( fixed - coreg, title='Difference with ANTs coregistration', vmin=v[0], vmax=v[1], ) .. image-sg:: /generated/examples/tutorials/images/sphx_glr_plot_coreg_pairwise_004.png :alt: Difference with ANTs coregistration , Slice 1, Slice 2, Slice 3, Slice 4, Slice 5, Slice 6, Slice 7, Slice 8, Slice 9, Slice 10, Slice 11, Slice 12, Slice 13, Slice 14, Slice 15, Slice 16, Slice 17, Slice 18, Slice 19, Slice 20, Slice 21, Slice 22, Slice 23, Slice 24, Slice 25, Slice 26, Slice 27, Slice 28, Slice 29, Slice 30, Slice 31, Slice 32, Slice 33, Slice 34, Slice 35, Slice 36 :srcset: /generated/examples/tutorials/images/sphx_glr_plot_coreg_pairwise_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 195-197 This also appears to have achieved the goal of reducing the main differences without creating unwanted deformations. .. GENERATED FROM PYTHON SOURCE LINES 199-203 The second return value is a path or list of paths to files that hold the parameter values. These can be used to deform other images in the same way. If we deform the moving image, we get the coregistered image again: .. GENERATED FROM PYTHON SOURCE LINES 203-211 .. code-block:: Python deformed = mdreg.ants.transform(moving, deform) # Check the difference with the coregistered image err = 100*np.linalg.norm(deformed-coreg)/np.linalg.norm(moving) print(f"Difference between coregistered and deformed: {err} %") .. rst-class:: sphx-glr-script-out .. code-block:: none Difference between coregistered and deformed: 0.000103812686575111 % .. GENERATED FROM PYTHON SOURCE LINES 212-214 Note since ants writes deformation parameters to files, this will leave traces on disk unless you remove these explicitly: .. GENERATED FROM PYTHON SOURCE LINES 214-217 .. code-block:: Python [os.remove(d) for d in deform] .. rst-class:: sphx-glr-script-out .. code-block:: none [None, None] .. GENERATED FROM PYTHON SOURCE LINES 218-220 Alternatively, if the transformation is not needed the coreg function can be called with return_transfo=False: .. GENERATED FROM PYTHON SOURCE LINES 220-228 .. code-block:: Python coreg = mdreg.ants.coreg( moving, fixed, type_of_transform='SyNOnly', return_transfo=False, ) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 10.599 seconds) .. _sphx_glr_download_generated_examples_tutorials_plot_coreg_pairwise.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_coreg_pairwise.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_coreg_pairwise.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_coreg_pairwise.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_