File: labels-2d.py

package info (click to toggle)
napari 0.6.6-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,036 kB
  • sloc: python: 112,264; xml: 72; makefile: 44; sh: 5
file content (33 lines) | stat: -rw-r--r-- 827 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Labels 2D
=========

Display a labels layer above of an image layer using the ``add_labels`` and
``add_image`` APIs

.. tags:: visualization-basic
"""

from skimage import data
from skimage.color import rgb2gray
from skimage.segmentation import slic

import napari

astro = data.astronaut()

# initialise viewer with astro image
viewer = napari.Viewer()
layer = viewer.add_image(rgb2gray(astro), name='astronaut', rgb=False)

# add the labels
# we add 1 because SLIC returns labels from 0, which we consider background
labels = slic(astro, channel_axis=-1, compactness=20) + 1
label_layer = viewer.add_labels(labels, name='segmentation')

# Set the labels layer mode to picker with a string
label_layer.mode = 'PICK'
print(f'The color of label 5 is {label_layer.get_color(5)}')

if __name__ == '__main__':
    napari.run()