.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated\examples\plot_3d_multislice_translation_t2.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_multislice_translation_t2.py: ===================================== Multislice 2D to 3D translation (T2*) ===================================== This example illustrates 2D to 3D coregistration using 3D translations The moving volume is an oblique multi-slice T2* map and the static volume is a 3D coronal mask covering both kidneys. An initial 3D translation is performed using both kidneys as a static target. In a second step, fine tuning is done for each kidney separately. Coregistration is performed by brute force optimization using a mutual information metric. .. GENERATED FROM PYTHON SOURCE LINES 19-21 Setup ----- .. GENERATED FROM PYTHON SOURCE LINES 21-38 .. code-block:: Python # Import packages import vreg import vreg.plot as plt # Get static volumes lk = vreg.fetch('left_kidney') rk = vreg.fetch('right_kidney') # get moving volumes multislice = vreg.fetch('T2star') # Get geometrical reference dixon = vreg.fetch('Dixon_water') .. GENERATED FROM PYTHON SOURCE LINES 39-43 Format data ----------- Create a mask containing both kidneys (bk) with the geometry of the complete DIXON series .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: Python bk = lk.slice_like(dixon).add(rk) .. GENERATED FROM PYTHON SOURCE LINES 47-49 Extract bounding boxes 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 49-54 .. code-block:: Python bk = bk.bounding_box() lk = lk.bounding_box() rk = rk.bounding_box() .. GENERATED FROM PYTHON SOURCE LINES 55-57 If we overlay the mask on the volume, we clearly see the misalignment due to different breath holding positions: .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: Python plt.overlay_2d(multislice, bk) .. image-sg:: /generated/examples/images/sphx_glr_plot_3d_multislice_translation_t2_001.png :alt: plot 3d multislice translation t2 :srcset: /generated/examples/images/sphx_glr_plot_3d_multislice_translation_t2_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 61-67 Coregister to both kidneys -------------------------- In a first step we coregister by 3D translation to both kidneys. Since the moving data are multislice, we need to perform a coregistration for each slice separately. We perform brute force optimization allowing translations between [-20, 20] mm in-slice, and [-5, 5] mm through-slice, in steps of 2mm: .. GENERATED FROM PYTHON SOURCE LINES 67-87 .. code-block:: Python # Optimizer settings optimizer = { 'method': 'brute', 'grid': ( [-20, 20, 20], [-20, 20, 20], [-5, 5, 5], ), } # Translations are defined in volume coordinates options = { 'coords':'volume', } # Perform the coregistration for each slice for z, sz in enumerate(multislice): tz = sz.find_translate_to(bk, optimizer=optimizer, **options) multislice[z] = sz.translate(tz, **options) .. GENERATED FROM PYTHON SOURCE LINES 88-90 If we overlay the mask on the new volume, we can see that the misalignment is significantly reduced but some imperfections still remain. .. GENERATED FROM PYTHON SOURCE LINES 90-93 .. code-block:: Python plt.overlay_2d(multislice, bk) .. image-sg:: /generated/examples/images/sphx_glr_plot_3d_multislice_translation_t2_002.png :alt: plot 3d multislice translation t2 :srcset: /generated/examples/images/sphx_glr_plot_3d_multislice_translation_t2_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 94-98 Left kidney fine tuning ----------------------- We now perform a rigid transformation to the left kidney to fine tune the alignment. .. GENERATED FROM PYTHON SOURCE LINES 98-108 .. code-block:: Python # Try 10 translations between +/- 2mm in each directon optimizer['grid'] = 3*[[-2, 2, 10]] # Perform the fine tuning align_lk = [] for z, sz in enumerate(multislice): tz = sz.find_translate_to(lk, optimizer=optimizer, **options) align_lk.append(sz.translate(tz, **options)) .. GENERATED FROM PYTHON SOURCE LINES 109-110 Plot the result .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: Python plt.overlay_2d(align_lk, lk, title='Left kidney alignment') .. image-sg:: /generated/examples/images/sphx_glr_plot_3d_multislice_translation_t2_003.png :alt: Left kidney alignment :srcset: /generated/examples/images/sphx_glr_plot_3d_multislice_translation_t2_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 113-116 Right kidney fine tuning ------------------------ Repeat the same steps for the right kidney .. GENERATED FROM PYTHON SOURCE LINES 116-122 .. code-block:: Python align_rk = [] for z, sz in enumerate(multislice): tz = sz.find_translate_to(rk, optimizer=optimizer, **options) align_rk.append(sz.translate(tz, **options)) .. GENERATED FROM PYTHON SOURCE LINES 123-124 Plot the result .. GENERATED FROM PYTHON SOURCE LINES 124-126 .. code-block:: Python plt.overlay_2d(align_rk, rk, title='Right kidney alignment') .. image-sg:: /generated/examples/images/sphx_glr_plot_3d_multislice_translation_t2_004.png :alt: Right kidney alignment :srcset: /generated/examples/images/sphx_glr_plot_3d_multislice_translation_t2_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (22 minutes 39.181 seconds) .. _sphx_glr_download_generated_examples_plot_3d_multislice_translation_t2.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_multislice_translation_t2.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_3d_multislice_translation_t2.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_3d_multislice_translation_t2.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_