How can I draw a ROI using groovy script?

713 views
Skip to first unread message

Murali S

unread,
Apr 26, 2017, 9:34:43 AM4/26/17
to QuPath users
I am new to QuPath. I found that its a great tool.
I am having list of xy coordinates like [(x1, y1), (x2, y2), ...]. Now I want to draw a ROI (polygon) for those points through scripting. I am not able to find the correct method to draw it. Please help me to fix this.

Also if possible try to give me an example here. It will be great for me.

Thank you,
Murali S.

Pete

unread,
Apr 28, 2017, 1:15:54 AM4/28/17
to QuPath users
There currently isn't a 'helper' method to create the polygon regions of interest very concisely in a script, but there is some information about the steps you need at https://github.com/qupath/qupath/issues/61 

You'll need to import the corresponding Java classes, create your region of interest, create an object to display, and then add the object.

Your script might look something like this:


import qupath.lib.objects.*
import qupath.lib.roi.*

// Any vertices
def x = [100, 200, 300] as float[]
def y = [150, 350, 250] as float[]

// Create object
def roi = new PolygonROI(x, y, -1, 0, 0)
def pathObject = new PathAnnotationObject(roi)

// Add object to hierarchy
addObject(pathObject)

Murali S

unread,
Apr 28, 2017, 2:22:42 AM4/28/17
to QuPath users
Thank you Pete. It is the solution for my issue.

Murali S

unread,
May 10, 2017, 3:42:55 AM5/10/17
to QuPath users
Can we add annotations class for the region while drawing it?

Pete

unread,
May 10, 2017, 1:24:32 PM5/10/17
to QuPath users
Does the 'Auto set' button (under list of classifications in the Annotations tab) do what you want?
This enables all new annotations to automatically have a specified classification.

Murali S

unread,
Jun 19, 2017, 8:47:09 AM6/19/17
to QuPath users
I was on the vacation. I achieved my requirement by using following code.

def r = 255
def g = 0
def b = 0
def className = "Benign"
QP
.addObject(new PathAnnotationObject(new PolygonROI(x,y, -1, 0, 0), new PathClass(className,qupath.lib.common.ColorTools.makeRGB(r, g, b))))

Thank you Pete.

Thanks & Regards,
Murali S.

Pete

unread,
Jun 19, 2017, 4:51:15 PM6/19/17
to QuPath users
Good that you solved the problem.  Just one thing: the constructor for PathClass is not public, and shouldn't really be used directly (even if Groovy allows it).

Rather, you can obtain PathClass objects using PathClassFactory.getPathClass(String name, Integer color).

This ensures that classifications with the same name are represented by the same object - which is required for some commands to work as expected (e.g. when checking if classifications are equal).
Reply all
Reply to author
Forward
0 new messages