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, 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).
1%| | 1/121 [00:00<00:12, 9.53it/s]
3%|▎ | 4/121 [00:00<00:05, 21.22it/s]
7%|▋ | 8/121 [00:00<00:04, 27.47it/s]
10%|▉ | 12/121 [00:00<00:03, 30.30it/s]
13%|█▎ | 16/121 [00:00<00:03, 31.73it/s]
17%|█▋ | 20/121 [00:00<00:03, 32.72it/s]
20%|█▉ | 24/121 [00:00<00:02, 33.50it/s]
23%|██▎ | 28/121 [00:00<00:02, 34.10it/s]
26%|██▋ | 32/121 [00:01<00:02, 34.80it/s]
30%|██▉ | 36/121 [00:01<00:02, 35.02it/s]
33%|███▎ | 40/121 [00:01<00:02, 35.16it/s]
36%|███▋ | 44/121 [00:01<00:02, 34.51it/s]
40%|███▉ | 48/121 [00:01<00:02, 33.20it/s]
43%|████▎ | 52/121 [00:01<00:02, 33.18it/s]
46%|████▋ | 56/121 [00:01<00:01, 33.26it/s]
50%|████▉ | 60/121 [00:01<00:01, 33.31it/s]
53%|█████▎ | 64/121 [00:02<00:03, 16.81it/s]
55%|█████▌ | 67/121 [00:02<00:03, 13.81it/s]
58%|█████▊ | 70/121 [00:03<00:04, 12.16it/s]
60%|█████▉ | 72/121 [00:03<00:04, 11.35it/s]
61%|██████ | 74/121 [00:03<00:04, 10.88it/s]
63%|██████▎ | 76/121 [00:03<00:04, 10.52it/s]
64%|██████▍ | 78/121 [00:03<00:04, 10.41it/s]
66%|██████▌ | 80/121 [00:04<00:03, 10.35it/s]
68%|██████▊ | 82/121 [00:04<00:03, 10.48it/s]
69%|██████▉ | 84/121 [00:04<00:03, 10.53it/s]
71%|███████ | 86/121 [00:04<00:03, 10.68it/s]
73%|███████▎ | 88/121 [00:04<00:03, 10.69it/s]
74%|███████▍ | 90/121 [00:04<00:02, 10.73it/s]
76%|███████▌ | 92/121 [00:05<00:03, 8.31it/s]
78%|███████▊ | 94/121 [00:05<00:03, 8.94it/s]
79%|███████▉ | 96/121 [00:05<00:02, 9.38it/s]
81%|████████ | 98/121 [00:05<00:02, 9.77it/s]
83%|████████▎ | 100/121 [00:06<00:02, 9.91it/s]
84%|████████▍ | 102/121 [00:06<00:01, 10.04it/s]
86%|████████▌ | 104/121 [00:06<00:01, 9.97it/s]
88%|████████▊ | 106/121 [00:06<00:01, 9.89it/s]
89%|████████▉ | 108/121 [00:06<00:01, 9.63it/s]
90%|█████████ | 109/121 [00:07<00:01, 9.57it/s]
91%|█████████ | 110/121 [00:07<00:01, 9.43it/s]
92%|█████████▏| 111/121 [00:07<00:01, 9.26it/s]
93%|█████████▎| 112/121 [00:07<00:01, 8.96it/s]
93%|█████████▎| 113/121 [00:07<00:00, 8.95it/s]
94%|█████████▍| 114/121 [00:07<00:00, 8.93it/s]
95%|█████████▌| 115/121 [00:07<00:00, 8.85it/s]
96%|█████████▌| 116/121 [00:07<00:00, 8.67it/s]
97%|█████████▋| 117/121 [00:07<00:00, 8.69it/s]
98%|█████████▊| 118/121 [00:08<00:00, 8.72it/s]
98%|█████████▊| 119/121 [00:08<00:00, 8.77it/s]
99%|█████████▉| 120/121 [00:08<00:00, 8.69it/s]
100%|██████████| 121/121 [00:08<00:00, 8.70it/s]
100%|██████████| 121/121 [00:08<00:00, 14.36it/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 13.169 seconds)