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 (967, 943) to (976, 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:06, 18.36it/s]
  4%|▍         | 5/121 [00:00<00:05, 22.05it/s]
  7%|▋         | 8/121 [00:00<00:04, 23.56it/s]
  9%|▉         | 11/121 [00:00<00:04, 24.37it/s]
 12%|█▏        | 14/121 [00:00<00:04, 24.89it/s]
 14%|█▍        | 17/121 [00:00<00:04, 25.18it/s]
 17%|█▋        | 20/121 [00:00<00:03, 25.38it/s]
 19%|█▉        | 23/121 [00:00<00:03, 25.58it/s]
 21%|██▏       | 26/121 [00:01<00:03, 25.75it/s]
 24%|██▍       | 29/121 [00:01<00:03, 25.83it/s]
 26%|██▋       | 32/121 [00:01<00:03, 26.12it/s]
 29%|██▉       | 35/121 [00:01<00:03, 26.16it/s]
 31%|███▏      | 38/121 [00:01<00:03, 26.18it/s]
 34%|███▍      | 41/121 [00:01<00:03, 26.15it/s]
 36%|███▋      | 44/121 [00:01<00:03, 25.34it/s]
 39%|███▉      | 47/121 [00:01<00:02, 25.24it/s]
 41%|████▏     | 50/121 [00:01<00:02, 24.98it/s]
 44%|████▍     | 53/121 [00:02<00:02, 24.47it/s]
 46%|████▋     | 56/121 [00:02<00:02, 23.98it/s]
 49%|████▉     | 59/121 [00:02<00:02, 24.46it/s]
 51%|█████     | 62/121 [00:02<00:03, 18.32it/s]
 54%|█████▎    | 65/121 [00:02<00:03, 15.59it/s]
 55%|█████▌    | 67/121 [00:03<00:03, 14.58it/s]
 57%|█████▋    | 69/121 [00:03<00:03, 13.70it/s]
 59%|█████▊    | 71/121 [00:03<00:03, 13.06it/s]
 60%|██████    | 73/121 [00:03<00:03, 12.59it/s]
 62%|██████▏   | 75/121 [00:03<00:03, 12.38it/s]
 64%|██████▎   | 77/121 [00:03<00:03, 12.08it/s]
 65%|██████▌   | 79/121 [00:04<00:03, 11.93it/s]
 67%|██████▋   | 81/121 [00:04<00:03, 11.77it/s]
 69%|██████▊   | 83/121 [00:04<00:03, 11.72it/s]
 70%|███████   | 85/121 [00:04<00:03, 11.59it/s]
 72%|███████▏  | 87/121 [00:04<00:02, 11.48it/s]
 74%|███████▎  | 89/121 [00:04<00:02, 11.24it/s]
 75%|███████▌  | 91/121 [00:05<00:03,  9.59it/s]
 77%|███████▋  | 93/121 [00:05<00:03,  8.49it/s]
 79%|███████▊  | 95/121 [00:05<00:02,  9.07it/s]
 80%|████████  | 97/121 [00:05<00:02,  9.44it/s]
 82%|████████▏ | 99/121 [00:06<00:02,  9.80it/s]
 83%|████████▎ | 101/121 [00:06<00:02,  9.97it/s]
 85%|████████▌ | 103/121 [00:06<00:01, 10.22it/s]
 87%|████████▋ | 105/121 [00:06<00:01, 10.12it/s]
 88%|████████▊ | 107/121 [00:06<00:01, 10.32it/s]
 90%|█████████ | 109/121 [00:07<00:01, 10.38it/s]
 92%|█████████▏| 111/121 [00:07<00:00, 10.35it/s]
 93%|█████████▎| 113/121 [00:07<00:00, 10.31it/s]
 95%|█████████▌| 115/121 [00:07<00:00, 10.26it/s]
 97%|█████████▋| 117/121 [00:07<00:00, 10.24it/s]
 98%|█████████▊| 119/121 [00:08<00:00, 10.28it/s]
100%|██████████| 121/121 [00:08<00:00, 10.29it/s]
100%|██████████| 121/121 [00:08<00:00, 14.67it/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.032 seconds)

Gallery generated by Sphinx-Gallery