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 (959, 942) 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%|▏ | 3/121 [00:00<00:05, 22.98it/s]
5%|▍ | 6/121 [00:00<00:04, 25.69it/s]
7%|▋ | 9/121 [00:00<00:04, 26.99it/s]
10%|▉ | 12/121 [00:00<00:03, 27.59it/s]
12%|█▏ | 15/121 [00:00<00:03, 28.20it/s]
15%|█▍ | 18/121 [00:00<00:03, 28.64it/s]
17%|█▋ | 21/121 [00:00<00:03, 29.00it/s]
20%|█▉ | 24/121 [00:00<00:03, 29.27it/s]
22%|██▏ | 27/121 [00:00<00:03, 29.40it/s]
25%|██▍ | 30/121 [00:01<00:03, 29.38it/s]
28%|██▊ | 34/121 [00:01<00:02, 29.81it/s]
31%|███ | 37/121 [00:01<00:02, 29.37it/s]
33%|███▎ | 40/121 [00:01<00:02, 29.40it/s]
36%|███▌ | 43/121 [00:01<00:02, 28.76it/s]
38%|███▊ | 46/121 [00:01<00:02, 27.83it/s]
40%|████ | 49/121 [00:01<00:02, 27.54it/s]
43%|████▎ | 52/121 [00:01<00:02, 27.42it/s]
45%|████▌ | 55/121 [00:01<00:02, 27.67it/s]
48%|████▊ | 58/121 [00:02<00:02, 27.61it/s]
50%|█████ | 61/121 [00:02<00:02, 27.57it/s]
53%|█████▎ | 64/121 [00:03<00:10, 5.68it/s]
55%|█████▍ | 66/121 [00:04<00:13, 4.13it/s]
56%|█████▌ | 68/121 [00:05<00:15, 3.35it/s]
58%|█████▊ | 70/121 [00:06<00:17, 2.92it/s]
59%|█████▊ | 71/121 [00:06<00:18, 2.76it/s]
60%|█████▉ | 72/121 [00:07<00:18, 2.63it/s]
60%|██████ | 73/121 [00:07<00:18, 2.55it/s]
61%|██████ | 74/121 [00:08<00:18, 2.51it/s]
62%|██████▏ | 75/121 [00:08<00:18, 2.49it/s]
63%|██████▎ | 76/121 [00:09<00:18, 2.50it/s]
64%|██████▎ | 77/121 [00:09<00:17, 2.52it/s]
64%|██████▍ | 78/121 [00:09<00:16, 2.54it/s]
65%|██████▌ | 79/121 [00:10<00:16, 2.58it/s]
66%|██████▌ | 80/121 [00:10<00:15, 2.60it/s]
67%|██████▋ | 81/121 [00:11<00:15, 2.62it/s]
68%|██████▊ | 82/121 [00:11<00:14, 2.64it/s]
69%|██████▊ | 83/121 [00:11<00:14, 2.67it/s]
69%|██████▉ | 84/121 [00:12<00:13, 2.66it/s]
70%|███████ | 85/121 [00:12<00:13, 2.68it/s]
71%|███████ | 86/121 [00:12<00:12, 2.70it/s]
72%|███████▏ | 87/121 [00:13<00:12, 2.71it/s]
73%|███████▎ | 88/121 [00:13<00:12, 2.71it/s]
74%|███████▎ | 89/121 [00:13<00:11, 2.71it/s]
74%|███████▍ | 90/121 [00:14<00:11, 2.71it/s]
75%|███████▌ | 91/121 [00:14<00:11, 2.50it/s]
76%|███████▌ | 92/121 [00:15<00:12, 2.35it/s]
77%|███████▋ | 93/121 [00:15<00:11, 2.43it/s]
78%|███████▊ | 94/121 [00:16<00:10, 2.49it/s]
79%|███████▊ | 95/121 [00:16<00:10, 2.53it/s]
79%|███████▉ | 96/121 [00:16<00:09, 2.56it/s]
80%|████████ | 97/121 [00:17<00:09, 2.60it/s]
81%|████████ | 98/121 [00:17<00:08, 2.63it/s]
82%|████████▏ | 99/121 [00:17<00:08, 2.64it/s]
83%|████████▎ | 100/121 [00:18<00:07, 2.64it/s]
83%|████████▎ | 101/121 [00:18<00:07, 2.63it/s]
84%|████████▍ | 102/121 [00:19<00:07, 2.63it/s]
85%|████████▌ | 103/121 [00:19<00:06, 2.62it/s]
86%|████████▌ | 104/121 [00:19<00:06, 2.59it/s]
87%|████████▋ | 105/121 [00:20<00:06, 2.57it/s]
88%|████████▊ | 106/121 [00:20<00:05, 2.54it/s]
88%|████████▊ | 107/121 [00:21<00:05, 2.48it/s]
89%|████████▉ | 108/121 [00:21<00:05, 2.41it/s]
90%|█████████ | 109/121 [00:21<00:05, 2.36it/s]
91%|█████████ | 110/121 [00:22<00:04, 2.29it/s]
92%|█████████▏| 111/121 [00:22<00:04, 2.21it/s]
93%|█████████▎| 112/121 [00:23<00:04, 2.15it/s]
93%|█████████▎| 113/121 [00:23<00:03, 2.12it/s]
94%|█████████▍| 114/121 [00:24<00:03, 2.11it/s]
95%|█████████▌| 115/121 [00:24<00:02, 2.09it/s]
96%|█████████▌| 116/121 [00:25<00:02, 2.06it/s]
97%|█████████▋| 117/121 [00:25<00:01, 2.06it/s]
98%|█████████▊| 118/121 [00:26<00:01, 2.07it/s]
98%|█████████▊| 119/121 [00:26<00:00, 2.07it/s]
99%|█████████▉| 120/121 [00:27<00:00, 2.05it/s]
100%|██████████| 121/121 [00:27<00:00, 2.06it/s]
100%|██████████| 121/121 [00:27<00:00, 4.35it/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 32.807 seconds)