.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/applications/plot_3d_interaction.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. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_applications_plot_3d_interaction.py: ========================================== Interact with 3D images (of kidney tissue) ========================================== In this tutorial, we explore interactively a biomedical image which has three spatial dimensions and three color dimensions (channels). For a general introduction to 3D image processing, please refer to :ref:`sphx_glr_auto_examples_applications_plot_3d_image_processing.py`. The data we use here correspond to kidney tissue which was imaged with confocal fluorescence microscopy (more details at [1]_ under ``kidney-tissue-fluorescence.tif``). .. [1] https://gitlab.com/scikit-image/data/#data .. GENERATED FROM PYTHON SOURCE LINES 17-26 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import plotly import plotly.express as px from skimage import data .. GENERATED FROM PYTHON SOURCE LINES 27-30 Load image ========== This biomedical image is available through `scikit-image`'s data registry. .. GENERATED FROM PYTHON SOURCE LINES 30-33 .. code-block:: Python data = data.kidney() .. GENERATED FROM PYTHON SOURCE LINES 34-35 The returned dataset is a 3D multichannel image: .. GENERATED FROM PYTHON SOURCE LINES 35-40 .. code-block:: Python print(f'number of dimensions: {data.ndim}') print(f'shape: {data.shape}') print(f'dtype: {data.dtype}') .. rst-class:: sphx-glr-script-out .. code-block:: none number of dimensions: 4 shape: (16, 512, 512, 3) dtype: uint16 .. GENERATED FROM PYTHON SOURCE LINES 41-43 Dimensions are provided in the following order: ``(z, y, x, c)``, i.e., ``[plane, row, column, channel]``. .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: Python n_plane, n_row, n_col, n_chan = data.shape .. GENERATED FROM PYTHON SOURCE LINES 47-50 Let us consider only a slice (2D plane) of the data for now. More specifically, let us consider the slice located halfway in the stack. The `imshow` function can display both grayscale and RGB(A) 2D images. .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. code-block:: Python _, ax = plt.subplots() ax.imshow(data[n_plane // 2]) .. image-sg:: /auto_examples/applications/images/sphx_glr_plot_3d_interaction_001.png :alt: plot 3d interaction :srcset: /auto_examples/applications/images/sphx_glr_plot_3d_interaction_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Got range [64..4095]. .. GENERATED FROM PYTHON SOURCE LINES 55-57 According to the warning message, the range of values is unexpected. The image rendering is clearly not satisfactory color-wise. .. GENERATED FROM PYTHON SOURCE LINES 57-61 .. code-block:: Python vmin, vmax = data.min(), data.max() print(f'range: ({vmin}, {vmax})') .. rst-class:: sphx-glr-script-out .. code-block:: none range: (10, 4095) .. GENERATED FROM PYTHON SOURCE LINES 62-66 We turn to Plotly's implementation of the :func:`plotly.express.imshow` function, for it supports `value ranges `_ beyond ``(0.0, 1.0)`` for floats and ``(0, 255)`` for integers. .. GENERATED FROM PYTHON SOURCE LINES 66-70 .. code-block:: Python fig = px.imshow(data[n_plane // 2], zmax=vmax) plotly.io.show(fig) .. raw:: html :file: images/sphx_glr_plot_3d_interaction_002.html .. GENERATED FROM PYTHON SOURCE LINES 72-73 Here you go, *fluorescence* microscopy! .. GENERATED FROM PYTHON SOURCE LINES 75-81 Normalize range for each channel ================================ Generally speaking, we may want to normalize the value range on a per-channel basis. Let us facet our data (slice) along the channel axis. This way, we get three single-channel images, where the max value of each image is used: .. GENERATED FROM PYTHON SOURCE LINES 81-87 .. code-block:: Python fig = px.imshow( data[n_plane // 2], facet_col=2, binary_string=True, labels={'facet_col': 'channel'} ) plotly.io.show(fig) .. raw:: html :file: images/sphx_glr_plot_3d_interaction_003.html .. GENERATED FROM PYTHON SOURCE LINES 88-91 What is the range of values for each color channel? We check by taking the min and max across all non-channel axes. .. GENERATED FROM PYTHON SOURCE LINES 91-98 .. code-block:: Python vmin_0, vmin_1, vmin_2 = np.min(data, axis=(0, 1, 2)) vmax_0, vmax_1, vmax_2 = np.max(data, axis=(0, 1, 2)) print(f'range for channel 0: ({vmin_0}, {vmax_0})') print(f'range for channel 1: ({vmin_1}, {vmax_1})') print(f'range for channel 2: ({vmin_2}, {vmax_2})') .. rst-class:: sphx-glr-script-out .. code-block:: none range for channel 0: (10, 4095) range for channel 1: (68, 4095) range for channel 2: (35, 4095) .. GENERATED FROM PYTHON SOURCE LINES 99-100 Let us be very specific and pass value ranges on a per-channel basis: .. GENERATED FROM PYTHON SOURCE LINES 100-106 .. code-block:: Python fig = px.imshow( data[n_plane // 2], zmin=[vmin_0, vmin_1, vmin_2], zmax=[vmax_0, vmax_1, vmax_2] ) plotly.io.show(fig) .. raw:: html :file: images/sphx_glr_plot_3d_interaction_004.html .. GENERATED FROM PYTHON SOURCE LINES 107-109 Plotly lets you interact with this visualization by panning, zooming in and out, and exporting the desired figure as a static image in PNG format. .. GENERATED FROM PYTHON SOURCE LINES 111-115 Explore slices as animation frames ================================== Click the play button to move along the ``z`` axis, through the stack of all 16 slices. .. GENERATED FROM PYTHON SOURCE LINES 115-126 .. code-block:: Python fig = px.imshow( data, zmin=[vmin_0, vmin_1, vmin_2], zmax=[vmax_0, vmax_1, vmax_2], animation_frame=0, binary_string=True, labels={'animation_frame': 'plane'}, ) plotly.io.show(fig) .. raw:: html :file: images/sphx_glr_plot_3d_interaction_005.html .. GENERATED FROM PYTHON SOURCE LINES 127-129 Combine channel facetting and slice animation ============================================= .. GENERATED FROM PYTHON SOURCE LINES 129-139 .. code-block:: Python fig = px.imshow( data, animation_frame=0, facet_col=3, binary_string=True, labels={'facet_col': 'channel', 'animation_frame': 'plane'}, ) plotly.io.show(fig) .. raw:: html :file: images/sphx_glr_plot_3d_interaction_006.html .. GENERATED FROM PYTHON SOURCE LINES 140-144 The biologist's eye can spot that the two bright blobs (best seen in ``channel=2``) are kidney glomeruli [2]_. .. [2] https://en.wikipedia.org/wiki/Glomerulus_(kidney) .. GENERATED FROM PYTHON SOURCE LINES 144-146 .. code-block:: Python plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 7.125 seconds) .. _sphx_glr_download_auto_examples_applications_plot_3d_interaction.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-image/scikit-image/v0.24.0?filepath=notebooks/auto_examples/applications/plot_3d_interaction.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_3d_interaction.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_3d_interaction.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_