Export geometry of polylines and detected objects

Hello,

I am working with QuPath to extract anatomical features of wood cells from tree-rings. I found QuPath to be a very promising toold and extremely helpful here, but I am still a newbie and have unfortunately no background in JAVA

I would like to export for all detected objects the geometry of the cell and of the nucleus, combined with the x and y coordinates of the centroid to a text file (csv or tsv) for further processing in the R programming language.

With the following code that I found I can already print the coordinates of the outlines of all cells in the console, but I am not sure how to add the ImageID, the centroid coordinates, how to do this for the nuclei and how to export everything as a text file.

import org.locationtech.jts.io.WKTWriter

def annotations = getDetectionObjects()
def writer = new WKTWriter()
annotations.each {
  println writer.write(it.getROI().getGeometry())
}

In addition I want to encompass the analyzed tree-ring with two polylines. The following code prints me the coordinates of the polylines in the console, but also this print-out I would like to have exported as text file, ideally with the ImageID included in the export.

import org.locationtech.jts.io.WKTWriter

def annotations = getAnnotationObjects()
def writer = new WKTWriter()
annotations.each {
  println writer.write(it.getROI().getGeometry())
}

Please see here an example picture (OneDrive link) of the analyzed tree-ring with the detected objects in red and polylines highlighted in yellow.

Thanks for your patience and support!

Best regards,
WoodAnatomist.

Hi WoodAnatomist,
I’m not familiar with the WKTWriter module you’re using there. Whenever I want a csv, I modify this script:
Coding helper scripts for QuPath · GitHub, which is a simplified version of this forum post: Write Polygon Coordinates to text or csv file - #2 by melvingelbard . This method can be a bit complicated- it always takes me a few tries to get the string formatted properly. It looks like this WKTWriter is easier, but I don’t know how to use it, so I can’t be helpful here.

To answer your easier questions, here is some code that can help:
Centroids- it.getROI().getCentroidX() or it.getROI().getCentroidY()
Image name- getProjectEntry().getImageName()
to access nuclei ROIS- in the getDetectionObjects().each{} loop, replace it.getROI() with it.getNucleusROI()

Hope this gets you started!

2 Likes

Hi @WoodAnatomist you might want to try GeoJSON rather than WKTWriter – there are some docs at https://qupath.readthedocs.io/en/0.2/docs/advanced/exporting_annotations.html#shapes

Basically, GeoJSON should be able to represent the coordinates in a very similar way, but also allows additional properties to be stored. QuPath supports writing GeoJSON in a way that includes many/most of these already – although not the image (since it’s not a property of the object), nor the centroid (since it’s computed dynamically from the geometry).

3 Likes

Dear Sara and Pete,

thanks a lot! Both your comments were really helpful. First I tried to figure this it out using WKTWriter, because on first sight it seemed easier to handle in R and I am not really familiar with GeoJSON. However, I saw that some more complex objects had coordinates missing in the exported csv file as if there was a limit how long individual strings of the rows can be.

So I had to try it with GeoJSON and saw that there is an R package (“rjson”), which can import GeoJSON into R and I can take it now from there!

QuPath is great by the way. I hope to find some time to study it more in depth in the future.

2 Likes