Guest User

Untitled

a guest
Jan 30th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import omero
  5. from omero.rtypes import rdouble, rint, rstring
  6. from omero.gateway import BlitzGateway
  7.  
  8. USERNAME = 'username'
  9. PASSWORD = 'password'
  10. HOST = 'localhost'
  11. PORT = 4064
  12.  
  13. image_id = 76904
  14.  
  15. conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT)
  16. conn.connect()
  17. updateService = conn.getUpdateService()
  18.  
  19. # We have a helper function for creating an ROI and linking it to new shapes
  20. def create_roi(i_id, shapes):
  21. # create an ROI, link it to Image
  22. roi = omero.model.RoiI()
  23. # use the omero.model.ImageI that underlies the 'image' wrapper
  24. roi.setImage(omero.model.ImageI(i_id, False))
  25. for shape in shapes:
  26. roi.addShape(shape)
  27. # Save the ROI (saves any linked shapes too)
  28. return updateService.saveAndReturnObject(roi)
  29.  
  30. def create_point(i_id, x, y, text, z=0, t=0):
  31. # create an ROI with single point shape
  32. point = omero.model.PointI()
  33. point.x = rdouble(x)
  34. point.y = rdouble(y)
  35. point.theZ = rint(z)
  36. point.theT = rint(t)
  37. point.textValue = rstring(text)
  38. create_roi(i_id, [point])
  39.  
  40.  
  41. # Process Shapes (ONLY Polygons supported) to find centre point and
  42. # create a Point for each, with the Polygon ID as the Point label
  43. roi_service = conn.getRoiService()
  44. result = roi_service.findByImage(image_id, None)
  45. for roi in result.rois:
  46. print "ROI: ID:", roi.getId().getValue()
  47. for s in roi.copyShapes():
  48. shape_id = s.getId().getValue()
  49. # shape['theT'] = s.getTheT().getValue()
  50. # shape['theZ'] = s.getTheZ().getValue()
  51. if type(s) == omero.model.PolygonI:
  52. # find bounding box and centre
  53. points = s.getPoints().val.split(" ");
  54. try:
  55. x_coords = [float(p.split(",")[0]) for p in points if len(p) > 0]
  56. y_coords = [float(p.split(",")[1]) for p in points if len(p) > 0]
  57. min_x = min(x_coords)
  58. max_x = max(x_coords)
  59. min_y = min(y_coords)
  60. max_y = max(y_coords)
  61. cx = (min_x + max_x) / 2
  62. cy = (min_y + max_y) / 2
  63. print 'xy', cx, cy
  64.  
  65. # Create Point with shape_id as text
  66. create_point(image_id, cx, cy, shape_id)
  67. except ValueError:
  68. print "Invalid points", points
  69.  
  70. conn.close()
Add Comment
Please, Sign In to add comment