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
...
![Screenshot 2023-10-30 at 15 21 51](https://private-user-images.githubusercontent.com/900055/279108225-5e6a0c27-a5f5-4cb3-ac44-8a553129c1bc.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU2MTQ2MTksIm5iZiI6MTcyNTYxNDMxOSwicGF0aCI6Ii85MDAwNTUvMjc5MTA4MjI1LTVlNmEwYzI3LWE1ZjUtNGNiMy1hYzQ0LThhNTUzMTI5YzFiYy5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTA2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDkwNlQwOTE4MzlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1iODYwNjI0OGRhNTY5OGYxYjUxMzIyYTFiMDE3MGQzYmNlNjY1Yzk3MmY4YjVkZDI1M2EyZGZmYmI0NzYwM2U4JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.FAIRR-0QX9WvjVQ5iGn2HWA3eAn9R49T1ZGpt5cOFLA)
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](https://private-user-images.githubusercontent.com/900055/279108641-eda706f0-42ac-4d2a-b3d0-57d0eb57dcbf.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU2MTQ2MTksIm5iZiI6MTcyNTYxNDMxOSwicGF0aCI6Ii85MDAwNTUvMjc5MTA4NjQxLWVkYTcwNmYwLTQyYWMtNGQyYS1iM2QwLTU3ZDBlYjU3ZGNiZi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTA2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDkwNlQwOTE4MzlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0xN2IxYTQzNmRkMjU2MGY3ZDZkMDcwYzU2ODY0OGMxYzVhY2I4ZjhlN2M1NDdiYjg2ODE4YTQzNjRhZmM0MjkyJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.L-irDtlMhygA7_uASqbOYpMyf4TXYWwJXyRbxbOSLJI)