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 Explore 3D images (of cells). 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).

import matplotlib.pyplot as plt
import numpy as np

import plotly
import plotly.express as px
from skimage import data

Load image#

This biomedical image is available through scikit-image’s data registry.

The returned dataset is a 3D multichannel image:

print(f'number of dimensions: {data.ndim}')
print(f'shape: {data.shape}')
print(f'dtype: {data.dtype}')
number of dimensions: 4
shape: (16, 512, 512, 3)
dtype: uint16

Dimensions are provided in the following order: (z, y, x, c), i.e., [plane, row, column, channel].

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.

plot 3d interaction
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].

<matplotlib.image.AxesImage object at 0x7f115e08e840>

According to the warning message, the range of values is unexpected. The image rendering is clearly not satisfactory color-wise.

vmin, vmax = data.min(), data.max()
print(f'range: ({vmin}, {vmax})')
range: (10, 4095)

We turn to Plotly’s implementation of the plotly.express.imshow() function, for it supports value ranges beyond (0.0, 1.0) for floats and (0, 255) for integers.

Here you go, fluorescence microscopy!

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:

fig = px.imshow(
    data[n_plane // 2], facet_col=2, binary_string=True, labels={'facet_col': 'channel'}
)
plotly.io.show(fig)