Note
Go to the end to download the full example code.
Layer planes#
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 (880, 942) to (880, 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:08, 13.61it/s]
3%|▎ | 4/121 [00:00<00:08, 14.05it/s]
5%|▍ | 6/121 [00:00<00:07, 15.93it/s]
7%|▋ | 9/121 [00:00<00:06, 17.95it/s]
10%|▉ | 12/121 [00:00<00:05, 18.89it/s]
12%|█▏ | 14/121 [00:00<00:05, 17.95it/s]
13%|█▎ | 16/121 [00:00<00:05, 17.87it/s]
15%|█▍ | 18/121 [00:01<00:05, 18.30it/s]
17%|█▋ | 20/121 [00:01<00:05, 17.79it/s]
19%|█▉ | 23/121 [00:01<00:05, 19.05it/s]
21%|██ | 25/121 [00:01<00:05, 18.95it/s]
22%|██▏ | 27/121 [00:01<00:04, 19.03it/s]
24%|██▍ | 29/121 [00:01<00:04, 18.86it/s]
26%|██▌ | 31/121 [00:01<00:04, 19.16it/s]
28%|██▊ | 34/121 [00:01<00:04, 19.82it/s]
30%|██▉ | 36/121 [00:01<00:04, 19.57it/s]
31%|███▏ | 38/121 [00:02<00:04, 19.54it/s]
34%|███▍ | 41/121 [00:02<00:04, 19.99it/s]
36%|███▌ | 43/121 [00:02<00:04, 18.18it/s]
37%|███▋ | 45/121 [00:02<00:04, 17.66it/s]
40%|███▉ | 48/121 [00:02<00:04, 18.09it/s]
41%|████▏ | 50/121 [00:02<00:03, 18.07it/s]
43%|████▎ | 52/121 [00:02<00:03, 17.71it/s]
45%|████▍ | 54/121 [00:02<00:03, 17.74it/s]
46%|████▋ | 56/121 [00:03<00:03, 17.11it/s]
48%|████▊ | 58/121 [00:03<00:03, 17.09it/s]
50%|████▉ | 60/121 [00:03<00:03, 16.92it/s]
51%|█████ | 62/121 [00:03<00:05, 10.60it/s]
53%|█████▎ | 64/121 [00:04<00:07, 7.65it/s]
55%|█████▍ | 66/121 [00:04<00:08, 6.43it/s]
55%|█████▌ | 67/121 [00:04<00:08, 6.06it/s]
56%|█████▌ | 68/121 [00:04<00:09, 5.73it/s]
57%|█████▋ | 69/121 [00:05<00:09, 5.55it/s]
58%|█████▊ | 70/121 [00:05<00:09, 5.26it/s]
59%|█████▊ | 71/121 [00:05<00:09, 5.11it/s]
60%|█████▉ | 72/121 [00:05<00:09, 5.00it/s]
60%|██████ | 73/121 [00:06<00:09, 5.01it/s]
61%|██████ | 74/121 [00:06<00:09, 5.06it/s]
62%|██████▏ | 75/121 [00:06<00:09, 5.09it/s]
63%|██████▎ | 76/121 [00:06<00:08, 5.15it/s]
64%|██████▎ | 77/121 [00:06<00:08, 5.21it/s]
64%|██████▍ | 78/121 [00:06<00:08, 5.30it/s]
65%|██████▌ | 79/121 [00:07<00:07, 5.36it/s]
66%|██████▌ | 80/121 [00:07<00:07, 5.30it/s]
67%|██████▋ | 81/121 [00:07<00:07, 5.20it/s]
68%|██████▊ | 82/121 [00:07<00:07, 5.29it/s]
69%|██████▊ | 83/121 [00:07<00:07, 5.39it/s]
69%|██████▉ | 84/121 [00:08<00:06, 5.39it/s]
70%|███████ | 85/121 [00:08<00:06, 5.36it/s]
71%|███████ | 86/121 [00:08<00:06, 5.37it/s]
72%|███████▏ | 87/121 [00:08<00:06, 5.40it/s]
73%|███████▎ | 88/121 [00:08<00:06, 5.36it/s]
74%|███████▎ | 89/121 [00:09<00:05, 5.44it/s]
74%|███████▍ | 90/121 [00:09<00:05, 5.46it/s]
75%|███████▌ | 91/121 [00:09<00:06, 4.74it/s]
76%|███████▌ | 92/121 [00:09<00:06, 4.33it/s]
77%|███████▋ | 93/121 [00:09<00:06, 4.62it/s]
78%|███████▊ | 94/121 [00:10<00:05, 4.83it/s]
79%|███████▊ | 95/121 [00:10<00:05, 4.98it/s]
79%|███████▉ | 96/121 [00:10<00:04, 5.05it/s]
80%|████████ | 97/121 [00:10<00:04, 5.02it/s]
81%|████████ | 98/121 [00:10<00:04, 5.13it/s]
82%|████████▏ | 99/121 [00:11<00:04, 5.19it/s]
83%|████████▎ | 100/121 [00:11<00:04, 5.11it/s]
83%|████████▎ | 101/121 [00:11<00:03, 5.13it/s]
84%|████████▍ | 102/121 [00:11<00:03, 5.10it/s]
85%|████████▌ | 103/121 [00:11<00:03, 5.27it/s]
86%|████████▌ | 104/121 [00:12<00:03, 5.31it/s]
87%|████████▋ | 105/121 [00:12<00:03, 5.24it/s]
88%|████████▊ | 106/121 [00:12<00:02, 5.08it/s]
88%|████████▊ | 107/121 [00:12<00:02, 5.02it/s]
89%|████████▉ | 108/121 [00:12<00:02, 4.98it/s]
90%|█████████ | 109/121 [00:13<00:02, 4.90it/s]
91%|█████████ | 110/121 [00:13<00:02, 4.81it/s]
92%|█████████▏| 111/121 [00:13<00:02, 4.70it/s]
93%|█████████▎| 112/121 [00:13<00:01, 4.63it/s]
93%|█████████▎| 113/121 [00:13<00:01, 4.55it/s]
94%|█████████▍| 114/121 [00:14<00:01, 4.55it/s]
95%|█████████▌| 115/121 [00:14<00:01, 4.53it/s]
96%|█████████▌| 116/121 [00:14<00:01, 4.42it/s]
97%|█████████▋| 117/121 [00:14<00:00, 4.46it/s]
98%|█████████▊| 118/121 [00:15<00:00, 4.53it/s]
98%|█████████▊| 119/121 [00:15<00:00, 4.52it/s]
99%|█████████▉| 120/121 [00:15<00:00, 4.47it/s]
100%|██████████| 121/121 [00:15<00:00, 4.48it/s]
100%|██████████| 121/121 [00:15<00:00, 7.70it/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 20.536 seconds)