Note
Go to the end to download the full example code.
Layer planes#
/home/runner/work/napari-animation/napari-animation/examples/layer_planes.py:16: FutureWarning: Parameter `area_threshold` is deprecated since version 0.26.0 and will be removed in 2.0.0 (or later). To avoid this warning, please use the parameter `max_size` instead. For more details, see the documentation of `remove_small_holes`. Note that the new threshold removes objects smaller than **or equal to** its value, while the previous parameter only removed smaller ones.
th_nuclei = skimage.morphology.remove_small_holes(th_nuclei, 20**3)
Rendering frames...
0%| | 0/121 [00:00<?, ?it/s]IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (960, 941) to (960, 944) to ensure video compatibility with most codecs and players. To prevent resizing, make your input image divisible by the macro_block_size or set the macro_block_size to 1 (risking incompatibility).
2%|▏ | 2/121 [00:00<00:07, 15.87it/s]
5%|▍ | 6/121 [00:00<00:04, 26.66it/s]
8%|▊ | 10/121 [00:00<00:03, 31.21it/s]
12%|█▏ | 14/121 [00:00<00:03, 33.61it/s]
15%|█▍ | 18/121 [00:00<00:02, 34.76it/s]
18%|█▊ | 22/121 [00:00<00:02, 35.56it/s]
21%|██▏ | 26/121 [00:00<00:02, 36.00it/s]
25%|██▍ | 30/121 [00:00<00:02, 36.42it/s]
28%|██▊ | 34/121 [00:00<00:02, 37.03it/s]
31%|███▏ | 38/121 [00:01<00:02, 37.10it/s]
35%|███▍ | 42/121 [00:01<00:02, 37.21it/s]
38%|███▊ | 46/121 [00:01<00:02, 36.30it/s]
41%|████▏ | 50/121 [00:01<00:01, 35.55it/s]
45%|████▍ | 54/121 [00:01<00:01, 35.62it/s]
48%|████▊ | 58/121 [00:01<00:01, 35.29it/s]
51%|█████ | 62/121 [00:01<00:02, 23.85it/s]
54%|█████▎ | 65/121 [00:02<00:02, 21.34it/s]
56%|█████▌ | 68/121 [00:02<00:02, 19.73it/s]
59%|█████▊ | 71/121 [00:02<00:02, 18.93it/s]
61%|██████ | 74/121 [00:02<00:02, 18.15it/s]
63%|██████▎ | 76/121 [00:02<00:02, 17.72it/s]
64%|██████▍ | 78/121 [00:02<00:02, 17.56it/s]
66%|██████▌ | 80/121 [00:03<00:02, 17.29it/s]
68%|██████▊ | 82/121 [00:03<00:02, 16.89it/s]
69%|██████▉ | 84/121 [00:03<00:02, 16.43it/s]
71%|███████ | 86/121 [00:03<00:02, 16.52it/s]
73%|███████▎ | 88/121 [00:03<00:02, 16.44it/s]
74%|███████▍ | 90/121 [00:03<00:01, 16.55it/s]
76%|███████▌ | 92/121 [00:03<00:02, 11.27it/s]
78%|███████▊ | 94/121 [00:04<00:02, 12.34it/s]
79%|███████▉ | 96/121 [00:04<00:01, 13.13it/s]
81%|████████ | 98/121 [00:04<00:01, 13.93it/s]
83%|████████▎ | 100/121 [00:04<00:01, 14.35it/s]
84%|████████▍ | 102/121 [00:04<00:01, 14.80it/s]
86%|████████▌ | 104/121 [00:04<00:01, 15.00it/s]
88%|████████▊ | 106/121 [00:04<00:00, 15.31it/s]
89%|████████▉ | 108/121 [00:05<00:00, 15.21it/s]
91%|█████████ | 110/121 [00:05<00:00, 15.42it/s]
93%|█████████▎| 112/121 [00:05<00:00, 15.21it/s]
94%|█████████▍| 114/121 [00:05<00:00, 15.41it/s]
96%|█████████▌| 116/121 [00:05<00:00, 15.30it/s]
98%|█████████▊| 118/121 [00:05<00:00, 15.37it/s]
99%|█████████▉| 120/121 [00:05<00:00, 15.36it/s]
100%|██████████| 121/121 [00:05<00:00, 20.69it/s]
from scipy import ndimage as ndi
from napari_animation import Animation
import napari
import skimage
import scipy
viewer = napari.Viewer(ndisplay=3)
nuclei = skimage.data.cells3d()[:,1,...]
denoised = scipy.ndimage.median_filter(nuclei, size=3)
th_nuclei = denoised > skimage.filters.threshold_li(denoised)
th_nuclei = skimage.morphology.remove_small_holes(th_nuclei, 20**3)
labels_data = skimage.measure.label(th_nuclei)
animation = Animation(viewer)
image_layer = viewer.add_image(nuclei, name="nuclei", depiction="plane",
blending='translucent')
labels_layer = viewer.add_labels(labels_data, name="labels", blending='translucent')
viewer.camera.angles = (-18.23797054423494, 41.97404742075617, 141.96173085742896)
viewer.camera.zoom *= 0.5
def replace_labels_data():
z_cutoff = int(image_layer.plane.position[0])
new_labels_data = labels_data.copy()
new_labels_data[z_cutoff:] = 0
labels_layer.data = new_labels_data
labels_layer.visible = False
image_layer.plane.position = (0, 0, 0)
animation.capture_keyframe(steps=30)
image_layer.plane.position = (59, 0, 0)
animation.capture_keyframe(steps=30)
image_layer.plane.position = (0, 0, 0)
animation.capture_keyframe(steps=30)
image_layer.plane.events.position.connect(replace_labels_data)
labels_layer.visible = True
labels_layer.experimental_clipping_planes = [{
"position": (0, 0, 0),
"normal": (-1, 0, 0), # point up in z (i.e: show stuff above plane)
}]
image_layer.plane.position = (59, 0, 0)
# access first plane, since it's a list
labels_layer.experimental_clipping_planes[0].position = (59, 0, 0)
animation.capture_keyframe(steps=30)
image_layer.plane.position = (0, 0, 0)
animation.capture_keyframe(steps=30)
animation.animate("layer_planes.mp4", canvas_only=True)
image_layer.plane.position = (0, 0, 0)
Total running time of the script: (0 minutes 11.977 seconds)