Just for reference: I would like to get the shapes of the ROIs I have drawn/segmented so I can later plot them on the images where I have segmented them from and also have them saved to my datastack.
Below is an example. I am working in a jupyter notebook just for reference, so I’ll post the cells here.
from mpl_interactions import image_segmenter
import numpy as np
%matplotlib widget
test_array = np.zeros((128,128))
foo = image_segmenter(test_array)
Next cell:
foo
# this now opens a window in which I can segment my image
Next cell:
print(foo.verts)
This outputs the coordinates of the roi that I have drawn:
[(44.3, 40.5070997362013), (42.97012987012987, 40.5070997362013), (39.64545454545454, 40.5070997362013), (33.661038961038955, 45.16164519074674), (33.32857142857142, 51.47852830762986),.......]
So fine so good. What I want to do now is to do this for a stack of images interactively. Therefore I have written a little function that uses ipywidgets and gets a list of image_segmenters as an argument
I do so in a new notebook:
from mpl_interactions import image_segmenter
import numpy as np
%matplotlib widget
import ipywidgets as widgets
test_array = np.zeros((128,128,4))
#create a 3D array simulating stacks of images
foo = [image_segmenter(test_array[:,:,n]) for n in range(4) ]
def draw_on_image(segmenter_list):
def plot_imgs(n_slice, eraser_mode, roi_number):
temp_seg = segmenter_list[n_slice]
temp_seg.erasing = eraser_mode
temp_seg.current_class = roi_number
display(temp_seg)
n_rois = segmenter_list[0].nclasses
n_slices = len(segmenter_list)
# Making the UI
class_selector = widgets.Dropdown(
options=list(range(1, n_rois + 1)), description="ROI number"
)
erasing_button = widgets.Checkbox(value=False, description="Erasing")
slice_slider = widgets.IntSlider(
value=n_slices // 2, min=0, max=n_slices - 1, description="Slice: "
)
# put both sliders inside a HBox for nice alignment etc.
ui = widgets.HBox(
[erasing_button, slice_slider, class_selector],
layout=widgets.Layout(display="flex"),
)
sliders = widgets.interactive_output(
plot_imgs,
{
"n_slice": slice_slider,
"eraser_mode": erasing_button,
"roi_number": class_selector,
},
)
display(ui, sliders)
I now call this function to draw as many rois as I want:
draw_on_image(foo)
After doing so I can access the masks using:
foo[0].mask
# for slice 0 for example
This allows me to save the masks for different ROIs.
But I cannot:
foo[0].verts
As it gives an error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[30], line 1
----> 1 foo[0].verts
AttributeError: 'image_segmenter' object has no attribute 'verts'
Any insight into this is greatly appreciated. Thans Ian!