Generating RectangleROI from plugin (RectangleROI vs ROIs class)

I’m updating a plugin for a third party GUI that utilizes QuPath classes. The developer who initially created the plugin made it in early 2018, when the RectangleROI constructor was still public. Below is the old method within our plugin for interfacing with QuPath WCD that converts a JSON object containing the ROI params to a new RectangleROI:

public static RectangleROI JSon2Roi(String json) {
		try {
			JSONObject jobj = new JSONObject(json);
			double x = jobj.getDouble(ROIX);
			double y = jobj.getDouble(ROIY);
			double width = jobj.getDouble(ROIWIDTH);
			double height = jobj.getDouble(ROIHEIGHT);
// Having trouble returning the new ROIs type as a RectangleROI...
			return new RectangleROI(x, y, width, height);

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

I’ve tried changing the method to public static ROIs... with the same param, and casting it differently:
PathArea createRectangleROI = ROIs.createRectangleROI(x, y, width, height, null);

I can’t seem to return a new RectangleROI or ROI object correctly even when working strictly from the ROIs.class

Assuming you have 2D images (i.e. not z-stacks or time series), try using

import qupath.lib.regions.ImagePlane
import qupath.lib.roi.ROIs

double x = 0
double y = 10
double width = 20
double height = 30

def plane = ImagePlane.getDefaultPlane()
ROIs.createRectangleROI(x, y, width, height, plane)

See ImagePlane.java.

1 Like

First off, thank you so much for being so involved with third-party developers @petebankhead. It is a godsend having such a thriving community here. I’m working with a 2D Brightfield image with no regard for z-stacks, and is alone in time.

I did those proposed changes within the existing method, errors on return type RectangleROI and ROIs.createRectangleROI(params);`

I was able to get at least the errors to go away by changing the method type from ROIs to PathArea, this seemed to check out fine by the IDE. Also, just noticed the ability to use def, that’s pretty Groovy (heh).

	public static PathArea JSon2Roi(String json) {

			double x = ROIX
			double y = ROIY
			double width = ROIWIDTH
			double height = ROIHEIGHT

			ImagePlane plane = ImagePlane.getDefaultPlane();

			return ROIs.createRectangleROI(x, y, width, height, plane);

This appears as if it will return the same results as back when RectangleROI constructors were public. Updating a plugin that hasn’t been touched since early 2018 has its shares of challenges, but having gone through and looked back at the commits on QuPath the updates have really been remarkable as far as interoperability.

I’ll have to wait to test it in our application, as this is a plugin for a .NET (C#) application, I have to mess around with the clunky IKVM.NET in order to utilize the plugin I’m creating to see if the results come out as expected.

1 Like

Sounds good! Forgot to mention I wrote a blog post explaining some of the changes from a scripting point of view: https://petebankhead.github.io/qupath/2019/08/21/scripting-in-v020.html

Very helpful article! I hadn’t thought about the scripting tools as everything is being implemented in a different GUI but thankfully your blog post led me to: qupath.lib.scripting.QP

If subclassing QP.class wasn’t problematic - as you state in the javadoc - it would be my one stop shop!