We're Hiring!

How to add Polygon ROI using python API ?

General and open developer discussion about using OMERO APIs from C++, Java, Python, Matlab and more! Please new questions at https://forum.image.sc/tags/omero
Please note:
Historical discussions about OMERO. Please look for and ask new questions at https://forum.image.sc/tags/omero

If you are having trouble with custom code, please provide a link to a public repository, ideally GitHub.

How to add Polygon ROI using python API ?

Postby Olivier Debeir » Thu May 15, 2014 7:06 am

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 !
Olivier Debeir
 
Posts: 6
Joined: Thu Apr 10, 2014 11:56 am

Re: How to add Polygon ROI using python API ?

Postby wmoore » Thu May 15, 2014 9:53 am

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
User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm

Re: How to add Polygon ROI using python API ?

Postby Olivier Debeir » Thu May 15, 2014 1:20 pm

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 !
Olivier Debeir
 
Posts: 6
Joined: Thu Apr 10, 2014 11:56 am


Return to Developer Discussion

Who is online

Users browsing this forum: Google [Bot] and 1 guest