Multiple Annotation Generation Scripting

Hey QuPath community,

In my analysis I am hoping to generate multiple ROIs that are lucked together. The idea behind this is to create an ellipse to cover the region we want to study and then have a larger rectangle around that ROI that is locked in location related to the ellipse. So wherever the ellipse is placed the rectangle would be centered around it.

As an example here are my two separate scripts: Is there a way to lock this rectangle in relation to the ellipse while scripting?

import qupath.lib.objects.PathObjects
import qupath.lib.roi.ROIs
import qupath.lib.regions.ImagePlane
int x = 0
int y = 0
def plane = ImagePlane.getPlane(x, y)
def roi = ROIs.createEllipseROI(0, 0, 6250, 4200, plane)
def annotation = PathObjects.createAnnotationObject(roi)
addObject(annotation)

and

import qupath.lib.objects.PathObjects
import qupath.lib.roi.ROIs
import qupath.lib.regions.ImagePlane
int x = 0
int y = 0
def plane = ImagePlane.getPlane(x, y)
def roi = ROIs.createRectangleROI(0, 0, 10000, 8000, plane)
def annotation = PathObjects.createAnnotationObject(roi)
addObject(annotation)

Thanks in advance for all help and insights into my question!

Please do format your code as code, using the preformatted text option. </> in the menu.

The 0,0 at the beginning of the create( functions are the XY coordinates of the upper left corner. If you have a fixed size, you can subtract the appropriate value from the top left position of the rectangle - but that will be relative to the size and shape of the ellipse.
If you run the two scripts, you can see that they will always put the object in the top left corner of the image, being 0,0. If you use variables instead of fixed numbers, you will replace the 0,0 with whatever is appropriate.

Note that it will only generate objects in the correct orientation at those coordinates - at this point, as far as I am aware, you cannot lock annotations together other than to merge them. A workaround might be to subtract the ellipse from the rectangle then move the hollowed out object. Without knowing more about the analysis pipeline it would be hard to tell if that helps.

You may be able to use affine transforms to move a second object β€œin the same way as the first object,” but that would get into more complicated scripting.

1 Like