.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated\examples\tutorials\plot_coreg_groupwise.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_groupwise.py: ============================================== Groupwise coregistration API ============================================== ``mdreg`` includes a harmonized API for groupwise coregistration of series of 2D images or 3D volumes with three different packages: ``ants``, ``skimage`` and ``itk-elastix``. This examples illustrates their usage. .. GENERATED FROM PYTHON SOURCE LINES 12-14 Setup ----- .. GENERATED FROM PYTHON SOURCE LINES 14-19 .. code-block:: Python import time import numpy as np import mdreg .. GENERATED FROM PYTHON SOURCE LINES 20-21 Load test data .. GENERATED FROM PYTHON SOURCE LINES 21-31 .. code-block:: Python data = mdreg.fetch('VFA') # Select short series of 3 volumes to coregister fixed = data['array'][:,:,:,:3] moving = data['array'][:,:,:,-3:] # 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 32-36 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 36-51 .. code-block:: Python # Difference image diff = fixed - moving # Keep the same scaling throughout this example v = np.percentile(diff, [1, 99]) # Display difference anim = mdreg.plot.animation( diff, title='Difference without coregistration', vmin=v[0], vmax=v[1], ) .. container:: sphx-glr-animation .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 52-55 Coregister with elastix ----------------------- We first use elastix to coregister the images: .. GENERATED FROM PYTHON SOURCE LINES 55-67 .. code-block:: Python t = time.time() coreg, deform = mdreg.elastix.coreg_series( 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: 11 seconds. .. GENERATED FROM PYTHON SOURCE LINES 68-73 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 75-77 We check the result by plotting the difference with the coregistered (deformed) moving image: .. GENERATED FROM PYTHON SOURCE LINES 77-85 .. code-block:: Python anim = mdreg.plot.animation( fixed - coreg, title='Difference with elastix coregistration', vmin=v[0], vmax=v[1], ) .. container:: sphx-glr-animation .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 86-90 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 90-100 .. code-block:: Python # Deform the moving image deformed = mdreg.elastix.transform_series(moving, deform, spacing=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 101-106 Coregister with skimage ----------------------- `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 106-117 .. code-block:: Python t = time.time() coreg, deform = mdreg.skimage.coreg_series( 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: 3 seconds. .. GENERATED FROM PYTHON SOURCE LINES 118-122 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 124-125 Plot the difference with the coregistered (deformed) moving image: .. GENERATED FROM PYTHON SOURCE LINES 125-133 .. code-block:: Python anim = mdreg.plot.animation( fixed - coreg, title='Difference with skimage coregistration', vmin=v[0], vmax=v[1], ) .. container:: sphx-glr-animation .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 134-138 In `skimage.coreg_series` 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 138-145 .. code-block:: Python deformed = mdreg.skimage.transform_series(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.0032464349642395973 % .. GENERATED FROM PYTHON SOURCE LINES 146-150 Coregister with ants -------------------- Let's run this a final time with the third package wrapped in mdreg - ``ants``: .. GENERATED FROM PYTHON SOURCE LINES 150-167 .. code-block:: Python t = time.time() coreg, deform = mdreg.ants.coreg_series( 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: 3 seconds. .. GENERATED FROM PYTHON SOURCE LINES 168-169 Plot the difference with the coregistered (deformed) moving image: .. GENERATED FROM PYTHON SOURCE LINES 169-177 .. code-block:: Python anim = mdreg.plot.animation( fixed - coreg, title='Difference with ANTs coregistration', vmin=v[0], vmax=v[1], ) .. container:: sphx-glr-animation .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 178-180 This also appears to have acheived the goal of reducing the main differences without creating unwanted deformations. .. GENERATED FROM PYTHON SOURCE LINES 182-186 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 186-194 .. code-block:: Python deformed = mdreg.ants.transform_series(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: 9.0885536337737e-05 % .. GENERATED FROM PYTHON SOURCE LINES 195-197 Note since ants writes deformation parameters to files, this will leave traces on disk unless you remove these explicitly. .. GENERATED FROM PYTHON SOURCE LINES 199-201 Alternatively, if the transformation is not needed the coreg function can be called with return_transfo=False: .. GENERATED FROM PYTHON SOURCE LINES 201-209 .. code-block:: Python coreg = mdreg.ants.coreg_series( moving, fixed, type_of_transform='SyNOnly', return_transfo=False, ) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 31.356 seconds) .. _sphx_glr_download_generated_examples_tutorials_plot_coreg_groupwise.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_groupwise.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_coreg_groupwise.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_coreg_groupwise.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_