Example of creating polygonROI

I’m using m12 release of QuPath 0.2.0 and trying to script a polygon roi creation. The data is in xml file and I parse it to get a list of double x and y coordinates. With the code below, I get an error about missing method - somehow my arguments are not correct? I suspect the ArrayList is the issue, would be great to get an example.

// get all vertex, and convert to ROI

  •        List<double> xCoords = []*
  •        List<double> yCoords = []*

// def xCoords = []

  • // def yCoords = *
  •        nVertexList = nVertice.getChildNodes();*
  •        for (int j=0; j<nVertexList.getLength(); j++) {*
  •            Node nVertex = nVertexList.item(j);*
  •            nVertex.normalize();*
  •            if (nVertex.getNodeType() == Node.ELEMENT_NODE) {*
  •                Element xy = (Element)nVertex;*
  •                xCoords << Double.parseDouble(xy.getAttribute("X"));*
  •                yCoords << Double.parseDouble(xy.getAttribute("Y"));*
  •                *
  •            }*
  •        }*
  •        // create the polygon roi*
  •        def plane = ImagePlane.getPlane(0, 0);*
  •        def polyROI = ROIs.createPolygonROI(xCoords, yCoords, plane);*
  •        print polyROI;*
  •        annotationList << PathObjects.createAnnotationObject(roi)*

ERROR: MissingMethodException at line 74: No signature of method: static qupath.lib.roi.ROIs.createPolygonROI() is applicable for argument types: (ArrayList, ArrayList, qupath.lib.regions.ImagePlane) values: [[2449.08, 2874.87, 2874.87, 2449.08, 2449.08], [16045.84, 16045.84, …], …]

Possible solutions: createPolygonROI(java.util.List, qupath.lib.regions.ImagePlane), createPolygonROI(double, double, qupath.lib.regions.ImagePlane), createPolygonROI([D, [D, qupath.lib.regions.ImagePlane), createPolylineROI(java.util.List, qupath.lib.regions.ImagePlane), createPolylineROI(double, double, qupath.lib.regions.ImagePlane), createPolylineROI([D, [D, qupath.lib.regions.ImagePlane)

Slightly guessing because I haven’t run the code, but you can try something like

def polyROI = ROIs.createPolygonROI(xCoords as double[], yCoords as double[], plane);

That’s the smallest change that I think could work (using some Groovy magic).

That worked! Thanks much!

I’m curious why the cast was needed since the parameter was already declared of that type?

1 Like

Not quite - you had a list and Java wanted an array.

Fortunately, Groovy is really good at swapping between things that are almost-but-not-quite the same (via as)… whereas Java just complains and stubbornly refuses to understand unless you give it exactly what it wants.