def test_blur_effect_3d(): """Test that the blur metric works on a 3D image.""" image_3d = cells3d()[:, 1, :, :] # grab just the nuclei B0 = blur_effect(image_3d) B1 = blur_effect(gaussian(image_3d, sigma=1)) B2 = blur_effect(gaussian(image_3d, sigma=4)) assert 0 <= B0 < 1 assert B0 < B1 < B2
def main() -> None: # load your data - we use an example 3D dataset here img = data.cells3d() # (z, c, y, x) # make sure that the dimension of your data has the right order # we expect the following dimensions: Channels, X, Y, Z. img = np.transpose(img, [1, 3, 2, 0]) # choose a name for our dataset time_str = strftime("%Y-%m-%d_%H-%M-%S", gmtime()) name = f"cell_{time_str}" # voxel_size is defined in nm ds = wk.Dataset(name, voxel_size=(260, 260, 290)) ds.default_view_configuration = DatasetViewConfiguration(zoom=0.35) # The example microscopy data has two channels # Channel 0 contains cell membranes, channel 1 contains nuclei. layer_membranes = ds.add_layer( "cell membranes", COLOR_CATEGORY, dtype_per_layer=img.dtype, ) layer_membranes.add_mag(1, compress=True).write(img[0, :]) layer_membranes.default_view_configuration = LayerViewConfiguration( color=(17, 212, 17), intensity_range=(0, 16000)) layer_nuclei = ds.add_layer( "nuclei", COLOR_CATEGORY, dtype_per_layer=img.dtype, ) layer_nuclei.add_mag(1, compress=True).write(img[1, :]) layer_nuclei.default_view_configuration = LayerViewConfiguration( color=(212, 17, 17), intensity_range=(3000, 30000)) remote_dataset = ds.upload() url = remote_dataset.url print(f"Successfully uploaded {url}")
def test_ndim(): image = data.cells3d()[:5, 1, ...] kernel = ellipsoid_kernel((3, 100, 100), 100) rolling_ball(image, kernel=kernel)
information was reported by the microscope used to image the cells. """ import matplotlib.pyplot as plt from mpl_toolkits.mplot3d.art3d import Poly3DCollection import numpy as np from skimage import exposure, io, util from skimage.data import cells3d ##################################################################### # Load and display 3D images # ========================== data = util.img_as_float(cells3d()[:, 1, :, :]) # grab just the nuclei print(f'shape: {data.shape}') print(f'dtype: {data.dtype}') print(f'range: ({data.min()}, {data.max()})') # Report spacing from microscope original_spacing = np.array([0.2900000, 0.0650000, 0.0650000]) # Account for downsampling of slices by 4 rescaled_spacing = original_spacing * [1, 4, 4] # Normalize spacing so that pixels are a distance of 1 apart spacing = rescaled_spacing / rescaled_spacing[2] print(f'microscope spacing: {original_spacing}\n')
""" Display a 3D volume and the scale bar """ import numpy as np import napari from skimage import data cells = data.cells3d() viewer = napari.Viewer(ndisplay=3) viewer.add_image( cells, name=('membrane', 'nuclei'), channel_axis=1, scale=(0.29, 0.26, 0.26), ) viewer.scale_bar.visible = True viewer.scale_bar.unit = "um" napari.run()
background = restoration.rolling_ball(image, kernel=kernel) plot_result(image, background) plt.show() ###################################################################### # Higher Dimensions # ----------------- # # Another feature of ``rolling_ball`` is that you can directly # apply it to higher dimensional images, e.g., a z-stack of images # obtained during confocal microscopy. The number of kernel # dimensions must match the image dimensions, hence the kernel shape # is now 3 dimensional. image = data.cells3d()[:, 1, ...] background = restoration.rolling_ball(image, kernel=restoration.ellipsoid_kernel( (1, 21, 21), 0.1)) plot_result(image[30, ...], background[30, ...]) plt.show() ###################################################################### # A kernel size of 1 does not filter along this axis. In other words, # above filter is applied to each image in the stack individually. # # However, you can also filter along all 3 dimensions at the same # time by specifying a value other than 1. image = data.cells3d()[:, 1, ...]
def time_rollingball_ndim(self): from skimage.restoration.rolling_ball import ellipsoid_kernel image = data.cells3d()[:, 1, ...] kernel = ellipsoid_kernel((1, 100, 100), 100) restoration.rolling_ball(image, kernel=kernel)
from skimage.data import cells3d import napari v = napari.view_image(cells3d(), channel_axis=1) napari.run()
from numpy.testing import assert_array_equal from skimage.color import rgb2gray from skimage.data import astronaut, cells3d from skimage.filters import gaussian from skimage.measure import blur_effect image = astronaut() image_3d = cells3d()[:, 1, :, :] # grab just the nuclei def test_blur_effect(): """Test that the blur metric increases with more blurring.""" B0 = blur_effect(image, channel_axis=-1) B1 = blur_effect(gaussian(image, sigma=1, channel_axis=-1), channel_axis=-1) B2 = blur_effect(gaussian(image, sigma=4, channel_axis=-1), channel_axis=-1) assert 0 <= B0 < 1 assert B0 < B1 < B2 def test_blur_effect_h_size(): """Test that the blur metric decreases with increasing size of the re-blurring filter. """ B0 = blur_effect(image, h_size=3, channel_axis=-1) B1 = blur_effect(image, channel_axis=-1) # default h_size is 11 B2 = blur_effect(image, h_size=30, channel_axis=-1) assert 0 <= B0 < 1 assert B0 > B1 > B2
3 spatial dimensions (to be distinguished from 2D multichannel images, which are also arrays with three axes). :func:`skimage.data.cells3d` returns a 3D fluorescence microscopy image of cells. The returned dataset is a 3D multichannel image with dimensions provided in ``(z, c, y, x)`` order. Channel 0 contains cell membranes, while channel 1 contains nuclei. The example below shows how to explore this dataset. This 3D image can be used to test the various functions of scikit-image. """ from skimage import data import plotly import plotly.express as px import numpy as np img = data.cells3d()[20:] # omit some slices that are partially empty img = img[5:26] upper_limit = 1.5 * np.percentile(img, q=99) img = np.clip(img, 0, upper_limit) fig = px.imshow( img, facet_col=1, animation_frame=0, binary_string=True, binary_format="jpg", ) fig.layout.annotations[0]["text"] = "Cell membranes"
def cell3d_image(): from skimage import data return np.ascontiguousarray(data.cells3d()[30:48, 0, 20:36, 20:32])