Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

napari segmentation example #280

Open
will-moore opened this issue Oct 30, 2023 · 0 comments
Open

napari segmentation example #280

will-moore opened this issue Oct 30, 2023 · 0 comments

Comments

@will-moore
Copy link
Member

will-moore commented Oct 30, 2023

This uses code from https://napari.org/stable/tutorials/segmentation/annotate_segmentation.html

I have tested this workflow with an RGB image from IDR, exported with Batch_Image_Export, but you can also simply save https://idr.openmicroscopy.org/webclient/render_image/179764/0/0/ and import it...

Then, using napari-omero open it in napari with $ omero napari view Image:ID

Copy the code from the link above - you just need the imports (maybe not all needed) and the segment() method.
Specifically I copied this code and pasted it into napari terminal...

import numpy as np
from skimage import data
from skimage.filters import threshold_otsu
from skimage.segmentation import clear_border
from skimage.measure import label, regionprops_table
from skimage.morphology import closing, square, remove_small_objects
import napari


def segment(image):
    """Segment an image using an intensity threshold determined via
    Otsu's method.

    Parameters
    ----------
    image : np.ndarray
        The image to be segmented

    Returns
    -------
    label_image : np.ndarray
        The resulting image where each detected object labeled with a unique integer.
    """
    # apply threshold
    thresh = threshold_otsu(image)
    bw = closing(image > thresh, square(4))

    # remove artifacts connected to image border
    cleared = remove_small_objects(clear_border(bw), 20)

    # label image regions
    label_image = label(cleared)

    return label_image

Then you can use the first "Red" layer from the RGB, slice it to 2D (required by code above) and compute() to turn it from a dask array into a numpy array, which also seems to be needed.

image = viewer.layers[0].data
image.shape
>>> (1, 1, 1024, 1344)
plane = image[0, 0]
plane = plane.compute()

Now we can use the segment() method we created above to give us labels...
Then we add back 2 dimensions to get back to 4D (shape is (1, 1, 1024, 1344) again) which seems to be needed for saving to OMERO.

labels = segment(plane)
lab3d = np.expand_dims(labels, axis=0)
lab4d = np.expand_dims(lab3d, axis=0)

Finally, we can add the labels to the viewer to see them in napari...

viewer.add_labels(lab4d)

Screenshot 2023-10-30 at 15 21 51

Then you can hit Save ROIs to OMERO and you should see the labels saved as new Masks in OMERO:

Screenshot 2023-10-30 at 15 23 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant