.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated\examples\plot_3d_mask_translation.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_plot_3d_mask_translation.py: ============================= 3D translation: image to mask ============================= This example illustrates coregistration by 3D translation. The moving volume is a 3D magnetization transfer map and the static volume is a 3D mask covering both kidneys. Coregistration is performed by brute force optimization using a mutual information metric. .. GENERATED FROM PYTHON SOURCE LINES 15-17 Setup ----- .. GENERATED FROM PYTHON SOURCE LINES 17-21 .. code-block:: Python import numpy as np import vreg import vreg.plot as plt .. GENERATED FROM PYTHON SOURCE LINES 22-24 Get data -------- .. GENERATED FROM PYTHON SOURCE LINES 24-35 .. code-block:: Python # Static oblique volumes lk = vreg.fetch('left_kidney') rk = vreg.fetch('right_kidney') # Moving volume mtr = vreg.fetch('MTR') # Geometrical reference dixon = vreg.fetch('Dixon_water') .. GENERATED FROM PYTHON SOURCE LINES 36-40 Format data ----------- Create a mask containing both kidneys (bk) with the geometry of the complete DIXON series .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: Python bk = lk.slice_like(dixon).add(rk) .. GENERATED FROM PYTHON SOURCE LINES 44-48 Bounding box ------------ Extract a bounding box to reduce the size of the volume. This is not necessary but it speeds up the calculation a little as the volume is smaller. .. GENERATED FROM PYTHON SOURCE LINES 48-51 .. code-block:: Python bk = bk.bounding_box() .. GENERATED FROM PYTHON SOURCE LINES 52-56 Overlay data before registration -------------------------------- If we overlay the mask on the volume, we clearly see the misalignment due to different breath holding positions: .. GENERATED FROM PYTHON SOURCE LINES 56-62 .. code-block:: Python plt.overlay_2d(mtr, bk, title='Before 3D translation', vmin=np.percentile(mtr.values, 10), vmax=np.percentile(mtr.values, 99)) .. image-sg:: /generated/examples/images/sphx_glr_plot_3d_mask_translation_001.png :alt: Before 3D translation :srcset: /generated/examples/images/sphx_glr_plot_3d_mask_translation_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 63-70 Coregister ---------- We are coregistering using a 3D translation in the reference frame of the moving volume. We are using a brute force optimization which is slow but robust. We allow for translations between [-20, 20] mm in-slice, and [-5, 5] mm through-slice, in steps of 2mm. .. GENERATED FROM PYTHON SOURCE LINES 70-83 .. code-block:: Python optimizer = { 'method': 'brute', 'grid': ( [-20, 20, 20], [-20, 20, 20], [-5, 5, 5], ), } params = mtr.find_translate_to(bk, optimizer=optimizer, coords='volume') mtr = mtr.translate(params, coords='volume') .. GENERATED FROM PYTHON SOURCE LINES 84-88 Overlay data after registration ------------------------------- If we overlay the mask on the new volume, we can see that the misalignment is significantly reduced: .. GENERATED FROM PYTHON SOURCE LINES 88-95 .. code-block:: Python plt.overlay_2d(mtr, bk, title='After 3D translation', vmin=np.percentile(mtr.values, 10), vmax=np.percentile(mtr.values, 99)) .. image-sg:: /generated/examples/images/sphx_glr_plot_3d_mask_translation_002.png :alt: After 3D translation :srcset: /generated/examples/images/sphx_glr_plot_3d_mask_translation_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (4 minutes 10.542 seconds) .. _sphx_glr_download_generated_examples_plot_3d_mask_translation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_3d_mask_translation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_3d_mask_translation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_3d_mask_translation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_