Skip to content

Instantly share code, notes, and snippets.

@will-moore
Created February 25, 2020 22:07
Show Gist options
  • Save will-moore/311dc5fe7afc34995f794d79e1c32c60 to your computer and use it in GitHub Desktop.
Save will-moore/311dc5fe7afc34995f794d79e1c32c60 to your computer and use it in GitHub Desktop.
Draw a Polygon around FRAP bleach spot. Works with FRAP images from https://downloads.openmicroscopy.org/images/DV/will/FRAP/
import omero
from omero.gateway import BlitzGateway
from omero.rtypes import rint, rstring
from skimage import morphology
from skimage import measure
conn = BlitzGateway('user', 'password', port=4064, host='localhost')
conn.connect()
updateService = conn.getUpdateService()
def rgba_to_int(red, green, blue, alpha=255):
""" Return the color as an Integer in RGBA encoding """
r = red << 24
g = green << 16
b = blue << 8
a = alpha
rgba_int = r+g+b+a
if (rgba_int > (2**31-1)): # convert to signed 32-bit int
rgba_int = rgba_int - 2**32
return rgba_int
def add_polygon(contour):
""" points is 2D list of [[x, y], [x, y]...]"""
# format points like "10,20, 50,150, 200,200, 250,75"
points = ["%s,%s" % (xy[1], xy[0]) for xy in contour]
points = ", ".join(points)
polygon = omero.model.PolygonI()
polygon.strokeColor = rint(rgba_to_int(255, 255, 0))
polygon.points = rstring(points)
roi = omero.model.RoiI()
roi.setImage(image._obj)
roi.addShape(polygon)
updateService.saveObject(roi)
dataset_id = 208
dataset = conn.getObject("Dataset", dataset_id)
for image in dataset.listChildren():
pixels = image.getPrimaryPixels()
# Bleach happens after 3rd time-point (T=2)
plane2 = pixels.getPlane(theT=2)
plane3 = pixels.getPlane(theT=3)
# difference between before and after
bleach = plane2 - plane3
# threshold at midpoint between min and max
threshold = bleach > ((bleach.min() + bleach.max())/2)
threshold = morphology.remove_small_objects(threshold, 100)
threshold = morphology.remove_small_holes(threshold, 100)
contours = measure.find_contours(threshold, 0)
# we want to find longest contour (biggest polygon)
print('Found %s contours', len(contours))
if len(contours) == 0:
continue
longest_contour = contours[0]
for c in contours:
if len(c) > len(longest_contour):
longest_contour = c
add_polygon(longest_contour)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment