3D text annotation for centroid of segmented objects found by skimage.measure.label

Analysis goals

I’m trying to text annotate my segmented objects via their label numbers given by skimage.measure.label in a 3D image. All I am able to achieve is color labeling of those objects in 3D which doesn’t suit my purpose.

Hey @kushaangupta ! While we currently don’t have a dedicated text layer (i.e., the ability to display text without other visuals), the Points layer may be better suited to your needs. Using the same text API, you can create a points layer where the text is displayed at each centroid. The points layer takes an array of coordinates (the centroids in this case) and you can provide the properties and text just as you did with the Shapes layer. You can also set the face and edge colors of the points to 'transparent' so they aren’t visible.

I have made an example script below that should get you started. Please let me know if you have any questions!

Example script (tested on napari==0.4.3)

"""
Display a labels layer above of an image layer using the add_labels and
add_image APIs
"""
import numpy as np
from skimage import data
from skimage.measure import regionprops_table
from scipy import ndimage as ndi
import napari

# create a label image of blobs
blobs = data.binary_blobs(length=128, volume_fraction=0.1, n_dim=3)
labeled = ndi.label(blobs)[0]

# get the centroids and label values from the label image
region_props = regionprops_table(labeled, properties=['label', 'centroid'])
centroid_coords = np.column_stack(
    [
        region_props['centroid-0'],
        region_props['centroid-1'],
        region_props['centroid-2']
    ]
)

# store the labels for each blob in a properties dictionary
points_properties = {'label': region_props['label']}

with napari.gui_qt():
    # create a viewer with the raw data and label image
    viewer = napari.view_image(blobs.astype(float), name='blobs')
    viewer.add_labels(labeled, name='blob ID')

    # add a points layer to display the text at the centroid of each
    # labeled region. By setting text='label', the text value for each point
    # will be the corresponding value in the 'label' property in the layer.properties dictionary.
    # note that we set the face and edge colors to  transparent so only the text is visible
    viewer.add_points(
        centroid_coords,
        edge_color = 'transparent',
        face_color = 'transparent',
        properties=points_properties,
        text='label'
    )

2 Likes