Skip to content

Instantly share code, notes, and snippets.

@motatoes
Last active March 12, 2018 19:24
Show Gist options
  • Save motatoes/19b9b346b905dfb392c37a8617b7511d to your computer and use it in GitHub Desktop.
Save motatoes/19b9b346b905dfb392c37a8617b7511d to your computer and use it in GitHub Desktop.
script to select n number of ROIs in ropily.py
# import pylab as pl
from matplotlib import pyplot as pl
from matplotlib.widgets import Button
import numpy as np
from roipoly import roipoly
img = np.ones((100, 100)) * range(0, 100)
ROIs = []
fig = pl.Figure()
done = False
class Callback(object):
has_next = True
def addROI(self, event):
# let user draw second ROI
ROI = roipoly(roicolor='b') #let user draw ROI
# not sure why this is needed to be honest as the figure is
# already being closed in the ROI class
pl.close(ROI.fig)
# pl.imshow(img, interpolation='nearest', cmap="Greys")
# pl.colorbar()
# ROI.displayROI()
print('done with rois')
ROIs.append(ROI)
print(ROIs)
# initUI()
def finish(self, event):
global done
global ROIs
# # show ROI masks
print(ROIs)
print(len(ROIs))
# if len(ROIs) > 0:
# sumMask = ROIs[0].getMask(img)
# for roi in ROIs:
# sumMask = np.bitwise_or(sumMask, roi.getMask(img))
# pl.imshow(sumMask,
# interpolation='nearest', cmap="Greys")
# pl.title('ROI masks of the two ROIs')
# print('finished')
# pl.show()
done = True
# close the current figure
pl.close(pl.gcf())
callback = Callback()
def initUI():
# show the image
pl.imshow(img, interpolation='nearest', cmap="Greys")
pl.colorbar()
pl.title("left click: line segment right click: close region")
pl.subplots_adjust(bottom=0.2)
ax = pl.gca()
axprev = pl.axes([0.7, 0.05, 0.1, 0.075])
axnext = pl.axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, 'Add ROI')
bnext.on_clicked(callback.addROI)
bprev = Button(axprev, 'Finish')
bprev.on_clicked(callback.finish)
[x.displayROI() for x in ROIs]
[x.displayMean(img) for x in ROIs]
pl.show()
while not done:
print(done)
initUI()
if len(ROIs) >0:
res = ROIs[0].getMask(img)
for roi in ROIs:
res = res + roi.getMask(img)
pl.imshow(res, interpolation='nearest', cmap="Greys")
pl.title('mask result')
pl.show()
# display the final mask
# To prevent terminating the script
# pl.pause(1)
# # let user draw first ROI
# ROI1 = roipoly(roicolor='r') #let user draw first ROI
# # show the image with the first ROI
# pl.imshow(img, interpolation='nearest', cmap="Greys")
# pl.colorbar()
# ROI1.displayROI()
# pl.title('draw second ROI')
# # let user draw second ROI
# ROI2 = roipoly(roicolor='b') #let user draw ROI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment