Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polygon python training example #4412

Merged
merged 5 commits into from Jan 24, 2016
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 50 additions & 21 deletions examples/Training/python/ROIs.py
Expand Up @@ -12,6 +12,7 @@
"""

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
Expand All @@ -31,24 +32,37 @@
# We are using the core Python API and omero.model objects here, since ROIs
# are not yet supported in the Python Blitz Gateway.
#
# In this example, we create an ROI with a rectangular shape and attach it to
# an image.
# 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
print ("Adding a rectangle at theZ: %s, theT: %s, X: %s, Y: %s, width: %s,"
" height: %s" % (theZ, theT, x, y, width, height))

# create an ROI, link it to Image
roi = omero.model.RoiI()
# use the omero.model.ImageI that underlies the 'image' wrapper
roi.setImage(image._obj)

# create a rectangle shape and add to ROI
# 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)
Expand All @@ -57,9 +71,8 @@
rect.theZ = rint(theZ)
rect.theT = rint(theT)
rect.textValue = rstring("test-Rectangle")
roi.addShape(rect)

# create an Ellipse shape and add to ROI
# create an Ellipse shape (added to ROI below)
ellipse = omero.model.EllipseI()
ellipse.cx = rdouble(y)
ellipse.cy = rdouble(x)
Expand All @@ -68,9 +81,13 @@
ellipse.theZ = rint(theZ)
ellipse.theT = rint(theT)
ellipse.textValue = rstring("test-Ellipse")
roi.addShape(ellipse)

# create a line shape and add to ROI
# 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)
Expand All @@ -79,21 +96,33 @@
line.theZ = rint(theZ)
line.theT = rint(theT)
line.textValue = rstring("test-Line")
roi.addShape(line)
createROI(image, [line])

# create a point shape and add to ROI
# 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")
roi.addShape(point)


# Save the ROI (saves any linked shapes too)
r = updateService.saveAndReturnObject(roi)

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need to set it to something other than what's described at http://www.openmicroscopy.org/Schemas/Documentation/Generated/OME-2015-01/ROI_xsd.html#Polygon_Points then we probably need a card.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtbc : I think this is the older format. @jburel / @dominikl : is the support for the newer available / carded?

# 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.
# =================================================================
Expand Down