Last active
February 5, 2019 05:42
-
-
Save will-moore/1255b6675b229fe799b2bb0b0f632ffc to your computer and use it in GitHub Desktop.
Script to process OMERO Polygons, creating a Point at the centre of each Polygon (with Polygon ID as Point label)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import omero | |
from omero.rtypes import rdouble, rint, rstring, unwrap | |
from omero.gateway import BlitzGateway | |
USERNAME = 'username' | |
PASSWORD = 'password' | |
HOST = 'localhost' | |
PORT = 4064 | |
image_id = 160872 | |
conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) | |
conn.connect() | |
updateService = conn.getUpdateService() | |
# We have a helper function for creating an ROI and linking it to new shapes | |
def create_roi(i_id, shapes): | |
# create an ROI, link it to Image | |
roi = omero.model.RoiI() | |
# use the omero.model.ImageI that underlies the 'image' wrapper | |
roi.setImage(omero.model.ImageI(i_id, False)) | |
for shape in shapes: | |
roi.addShape(shape) | |
# Save the ROI (saves any linked shapes too) | |
return updateService.saveAndReturnObject(roi) | |
def create_point(i_id, x, y, text, z, t): | |
# create an ROI with single point shape | |
point = omero.model.PointI() | |
point.x = rdouble(x) | |
point.y = rdouble(y) | |
if z is not None: | |
point.theZ = rint(z) | |
if t is not None: | |
point.theT = rint(t) | |
point.textValue = rstring(text) | |
create_roi(i_id, [point]) | |
# Process Shapes (ONLY Polygons supported) to find centre point and | |
# create a Point for each, with the Polygon ID as the Point label | |
roi_service = conn.getRoiService() | |
result = roi_service.findByImage(image_id, None) | |
for roi in result.rois: | |
print "ROI: ID:", roi.getId().getValue() | |
for s in roi.copyShapes(): | |
shape_id = s.getId().getValue() | |
the_t = unwrap(s.getTheT()) | |
the_z = unwrap(s.getTheZ()) | |
cx = None | |
cy = None | |
print s.__class__.__name__ | |
if type(s) == omero.model.RectangleI: | |
min_x = s.getX().getValue() | |
min_y = s.getY().getValue() | |
max_x = min_x + s.getWidth().getValue() | |
max_y = min_y + s.getHeight().getValue() | |
cx = (min_x + max_x) / 2 | |
cy = (min_y + max_y) / 2 | |
elif type(s) == omero.model.LabelI: | |
cx = s.getX().getValue() | |
cy = s.getY().getValue() | |
elif type(s) == omero.model.LineI: | |
min_x = s.getX1().getValue() | |
min_y = s.getX2().getValue() | |
max_x = s.getY1().getValue() | |
max_y = s.getY2().getValue() | |
cx = (min_x + max_x) / 2 | |
cy = (min_y + max_y) / 2 | |
elif type(s) == omero.model.PolygonI or type(s) == omero.model.PolylineI: | |
# find bounding box and centre | |
points = s.getPoints().val.split(" "); | |
try: | |
x_coords = [float(p.split(",")[0]) for p in points if len(p) > 0] | |
y_coords = [float(p.split(",")[1]) for p in points if len(p) > 0] | |
min_x = min(x_coords) | |
max_x = max(x_coords) | |
min_y = min(y_coords) | |
max_y = max(y_coords) | |
cx = (min_x + max_x) / 2 | |
cy = (min_y + max_y) / 2 | |
except ValueError: | |
print "Invalid points", points | |
print 'xy', cx, cy, 'z', the_z, 't', the_t | |
if cx is not None and cy is not None: | |
# Create Point with shape_id as text | |
create_point(image_id, cx, cy, shape_id, the_z, the_t) | |
conn.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment