Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import omero
- from omero.rtypes import rdouble, rint, rstring
- from omero.gateway import BlitzGateway
- USERNAME = 'username'
- PASSWORD = 'password'
- HOST = 'localhost'
- PORT = 4064
- image_id = 76904
- 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=0, t=0):
- # create an ROI with single point shape
- point = omero.model.PointI()
- point.x = rdouble(x)
- point.y = rdouble(y)
- point.theZ = rint(z)
- 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()
- # shape['theT'] = s.getTheT().getValue()
- # shape['theZ'] = s.getTheZ().getValue()
- if type(s) == omero.model.PolygonI:
- # 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
- print 'xy', cx, cy
- # Create Point with shape_id as text
- create_point(image_id, cx, cy, shape_id)
- except ValueError:
- print "Invalid points", points
- conn.close()
Add Comment
Please, Sign In to add comment