Page 1 of 1

How to add Polygon ROI using python API ?

PostPosted: Thu May 15, 2014 7:06 am
by Olivier Debeir
The question is how to add a polygon to the database using the python API ?

I could be something similar to :

Code: Select all
def add_polygon_roi(conn,imageid,x,y,label):
        """
        add a ROI to the image object inside Omero
        imageid
        x,y : x and y coordinates (e.g. list or numpy arrays)
        """
        image = conn.getObject("Image", imageid)
        # create an ROI, link it to Image
        roi = omero.model.RoiI()
        roi.setImage(image._obj)   

        # add a shape to ROI
        theZ = self.image.getSizeZ() / 2
        theT = 0

        # create a rectangle shape and add to ROI
        xy = np.vstack((x,y)).T

        poly.setPoints( ... ) # <---- ?

        poly.theT = rint(theT)
        poly.textValue = rstring(label)

        roi.addShape(poly)
        r = conn.getUpdateService().saveAndReturnObject(roi)


is there any example similarly to these given here http://www.openmicroscopy.org/site/support/omero5/developers/Python.html#ROIs ?

thanks !

Re: How to add Polygon ROI using python API ?

PostPosted: Thu May 15, 2014 9:53 am
by wmoore
I'm afraid the way we are storing points is kinda ugly at the moment, since we're just storing them in the format that comes from the JHotDraw ROI library used in Insight.

This will be improved in due course, and we'll update all the existing strings to some nicer format with a DB upgrade script or something similar.

In the meantime, you'll need to adopt the existing format, which has 3 sets of 'points' and a 'mask'. These are for support of bezier curves etc, but you can ignore that and simply repeat the same set of points 3 times. E.g.

Here's a triangle I just created in Insight, and then check the DB for the full string:

Code: Select all
"points[442,306, 390,375, 515,381] points1[442,306, 390,375, 515,381] points2[442,306, 390,375, 515,381] mask[0,0,0]"


And here's an example of where we read 'points' and convert to something usable by the script:
https://github.com/ome/scripts/blob/dev ... ph.py#L174

Or for display of shapes in the web:
https://github.com/openmicroscopy/openm ... al.py#L272

Re: How to add Polygon ROI using python API ?

PostPosted: Thu May 15, 2014 1:20 pm
by Olivier Debeir
Ok it works fine,
I used the following function to convert 2 vectors x and y into a compatible string

Code: Select all
def XYtoPointsString(x,y):
    s = ', '.join(['%s,%s'%(str(int(xx)),str(int(yy))) for xx,yy in izip(x,y)])
    z = ','.join('0'*len(x))
    return "points[%s] points1[%s] points2[%s] mask[%s]"%(s,s,s,z)


then

Code: Select all
poly.setPoints(rstring(XYtoPointsString(x,y)))

finish the job

thanks !