forked from ome/openmicroscopy
/
ROIs.py
executable file
·187 lines (164 loc) · 6.16 KB
/
ROIs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 University of Dundee & Open Microscopy Environment.
# All Rights Reserved.
# Use is subject to license terms supplied in LICENSE.txt
#
"""
FOR TRAINING PURPOSES ONLY!
"""
import omero
from omero.model.enums import UnitsLength
from omero.rtypes import rdouble, rint, rstring
from omero.gateway import BlitzGateway
from Parse_OMERO_Properties import USERNAME, PASSWORD, HOST, PORT
from Parse_OMERO_Properties import imageId
imageId = int(imageId)
# Create a connection
# =================================================================
conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT)
conn.connect()
updateService = conn.getUpdateService()
# Create ROI.
# =================================================================
# We are using the core Python API and omero.model objects here, since ROIs
# are not yet supported in the Python Blitz Gateway.
#
# First we load our image and pick some parameters for shapes
x = 50
y = 200
width = 100
height = 50
image = conn.getObject("Image", imageId)
theZ = image.getSizeZ() / 2
theT = 0
# We have a helper function for creating an ROI and linking it to new shapes
def createROI(img, shapes):
# create an ROI, link it to Image
roi = omero.model.RoiI()
# use the omero.model.ImageI that underlies the 'image' wrapper
roi.setImage(img._obj)
for shape in shapes:
roi.addShape(shape)
# Save the ROI (saves any linked shapes too)
updateService.saveObject(roi)
# Another helper for generating the color integers for shapes
def rgbToRGBInt(red, green, blue):
""" Convert an R,G,B value to an int """
RGBInt = (red << 16) + (green << 8) + blue
return int(RGBInt)
# create a rectangle shape (added to ROI below)
print ("Adding a rectangle at theZ: %s, theT: %s, X: %s, Y: %s, width: %s,"
" height: %s" % (theZ, theT, x, y, width, height))
rect = omero.model.RectangleI()
rect.x = rdouble(x)
rect.y = rdouble(y)
rect.width = rdouble(width)
rect.height = rdouble(height)
rect.theZ = rint(theZ)
rect.theT = rint(theT)
rect.textValue = rstring("test-Rectangle")
# create an Ellipse shape (added to ROI below)
ellipse = omero.model.EllipseI()
ellipse.cx = rdouble(y)
ellipse.cy = rdouble(x)
ellipse.rx = rdouble(width)
ellipse.ry = rdouble(height)
ellipse.theZ = rint(theZ)
ellipse.theT = rint(theT)
ellipse.textValue = rstring("test-Ellipse")
# Create an ROI containing 2 shapes on same plane
# NB: OMERO.insight client doesn't support this
# The ellipse is removed later (see below)
createROI(image, [rect, ellipse])
# create an ROI with single line shape
line = omero.model.LineI()
line.x1 = rdouble(x)
line.x2 = rdouble(x+width)
line.y1 = rdouble(y)
line.y2 = rdouble(y+height)
line.theZ = rint(theZ)
line.theT = rint(theT)
line.textValue = rstring("test-Line")
createROI(image, [line])
# create an ROI with single point shape
point = omero.model.PointI()
point.cx = rdouble(x)
point.cy = rdouble(y)
point.theZ = rint(theZ)
point.theT = rint(theT)
point.textValue = rstring("test-Point")
createROI(image, [point])
def pointsToString(points):
""" Returns strange format supported by Insight """
points = ["%s,%s" % (p[0], p[1]) for p in points]
csv = ", ".join(points)
return "points[%s] points1[%s] points2[%s]" % (csv, csv, csv)
# create an ROI with a single polygon, setting colors and lineWidth
polygon = omero.model.PolygonI()
polygon.theZ = rint(theZ)
polygon.theT = rint(theT)
polygon.fillColor = rint(rgbToRGBInt(255, 0, 255))
polygon.strokeColor = rint(rgbToRGBInt(0, 255, 0))
polygon.strokeWidth = omero.model.LengthI(10, UnitsLength.PIXEL)
points = [[10, 20], [50, 150], [200, 200], [250, 75]]
polygon.points = rstring(pointsToString(points))
createROI(image, [polygon])
# Retrieve ROIs linked to an Image.
# =================================================================
roiService = conn.getRoiService()
result = roiService.findByImage(imageId, None)
for roi in result.rois:
print "ROI: ID:", roi.getId().getValue()
for s in roi.copyShapes():
shape = {}
shape['id'] = s.getId().getValue()
shape['theT'] = s.getTheT().getValue()
shape['theZ'] = s.getTheZ().getValue()
if s.getTextValue():
shape['textValue'] = s.getTextValue().getValue()
if type(s) == omero.model.RectangleI:
shape['type'] = 'Rectangle'
shape['x'] = s.getX().getValue()
shape['y'] = s.getY().getValue()
shape['width'] = s.getWidth().getValue()
shape['height'] = s.getHeight().getValue()
elif type(s) == omero.model.EllipseI:
shape['type'] = 'Ellipse'
shape['cx'] = s.getCx().getValue()
shape['cy'] = s.getCy().getValue()
shape['rx'] = s.getRx().getValue()
shape['ry'] = s.getRy().getValue()
elif type(s) == omero.model.PointI:
shape['type'] = 'Point'
shape['cx'] = s.getCx().getValue()
shape['cy'] = s.getCy().getValue()
elif type(s) == omero.model.LineI:
shape['type'] = 'Line'
shape['x1'] = s.getX1().getValue()
shape['x2'] = s.getX2().getValue()
shape['y1'] = s.getY1().getValue()
shape['y2'] = s.getY2().getValue()
elif type(s) in (
omero.model.MaskI, omero.model.LabelI, omero.model.PolygonI):
print type(s), " Not supported by this code"
# Do some processing here, or just print:
print " Shape:",
for key, value in shape.items():
print " ", key, value,
print ""
# Remove shape from ROI
# =================================================================
result = roiService.findByImage(imageId, None)
for roi in result.rois:
for s in roi.copyShapes():
# Find and remove the Shape we added above
if s.getTextValue() and s.getTextValue().getValue() == "test-Ellipse":
print "Removing Shape from ROI..."
roi.removeShape(s)
roi = updateService.saveAndReturnObject(roi)
# Close connection:
# =================================================================
# When you are done, close the session to free up server resources.
conn._closeSession()