Access StarDist detection objects in QuPath script

Hi, I was trying to calculate the Feret angle for StarDist detection result. I adopted the method mentioned in this post: Export cell boundaries
Here is my script :

import qupath.tensorflow.stardist.StarDist2D
import qupath.imagej.objects.*

// Specify the model directory (you will need to change this!)
def pathModel = '/Users/mihaoyang/Desktop/TMA-Project-Alex/he_heavy_augment'

def stardist = StarDist2D.builder(pathModel)
      .threshold(0.4)              // Prediction threshold
      .normalizePercentiles(1, 99) // Percentile normalization
      .pixelSize(0.5)              // Resolution for detection
      .measureShape()
      .measureIntensity()
      .build()

// Run detection for the selected objects
def imageData = getCurrentImageData()
def pathObjects = getSelectedObjects()
if (pathObjects.isEmpty()) {
    Dialogs.showErrorMessage("StarDist", "Please select a parent object!")
    return
}
stardist.detectObjects(imageData, pathObjects)
println 'Done!'


getCellObjects().parallelStream().forEach {
    def ml = it.getMeasurementList()
    def roi = it.getNucleusROI()
    //for 0.1.2
    //def roiIJ = ROIConverterIJ.convertToIJRoi(roi, 0, 0, 1)
    roiIJ = IJTools.convertToIJRoi(roi, 0, 0, 1)
    def angle = roiIJ.getFeretValues()[1]
    ml.putMeasurement('Nucleus angle', angle)
    ml.close()
}
fireHierarchyUpdate()

This script could run withour error but the ‘Nucleus angle’ is not in the measurement list. Then I assume there must be something wrong with the getCellObjects. So that I changed this class to getDetectionObjects.
This time the script could not run with an error message:

ERROR: MissingMethodException at line 26: No signature of method: qupath.lib.objects.PathDetectionObject.getNucleusROI() is applicable for argument types: () values:

This issue is also related to data exporting, as I also want to export the cell boundaries. Looks like the way to do this is quite different from using the built-in segmentation algorithms.

Whelp, two things.

  1. I thought I fixed this before posting, and really have memories of it… but apparently I didn’t and have now edited it in the previous post. Please use each for this, not parallelStream().forEach
    Not the source of your problem here, but could cause problems down the line.
  2. You likely don’t have a nucleus in your cells, depending on how you ran StarDist. A cell in QuPath has a nucleus and a cytoplasm. If you generated just nuclei with StarDist, they are detections, so there is no nucleus to get. You can replace that line with
    def roi = it.getROI()
1 Like

Thank you, but the Nucleus angle measurement still not showing up…

1 Like

Right, assuming you don’t have cells, you also can’t use:

getCellObjects().parallelStream().forEach {

since you have no cell objects. Have you verified whether you have cells? If you don’t try getDetectionObjects().each{
*edited for completeness. Just in case.

Yes change getCellObjects() to getDetectionObjects() is the solution. Looks like the detection results of StarDist returns a list of detections named ‘Detection’ rather than ‘Cell’.

1 Like

Whether they are cells depends on your choice of settings; you could, instead, set a cell expansion and add measurements. The readthedocs page demonstrates that in the last example.

A ‘cell’ is a special type of detected, used when you need both a nucleus ROI and an outer membrane ROI: https://qupath.readthedocs.io/en/latest/docs/concepts/objects.html

@Research_Associate @petebankhead Understood, thank you!

1 Like

Hi pete,
I am new user of Qupath 0.2.3. I found my segmentation in tissue sample is not okay. So, I wanna try on with stardist. I tried to follow your reference but I cannot work stardist in my Qupath. DO u have any suggestion details “how to access stardist to work with qupth”. Thanks

Could you please provide more details about what is not working?

Is there an error when you try to use a StarDist script?

Can you confirm that you built QuPath from Gradle?

Is tensorflow installed (in Help->Extensions)?

3 Likes

Basically, when I wanna use stardist in Qupath, what steps I should do first? I download gardle in the qupath directory but when I wrote the script like import qupath extension tensorflow stardist, it had errors.

Thanks

Hi @HAY_MAR_OO,

It is a bit unclear whether you built QuPath with or without TensorFlow (required for stardist). Did you follow these steps exactly (this page might also be useful)? Note that if you already had QuPath working on your computer without TensorFlow, you will have to install it again, this time with TensorFlow (the Gradle command to build it is gradlew clean build createPackage -Ptensorflow-cpu=true).

Have you managed to install QuPath with TensorFlow already? If not, could you copy paste the errors that you get here?

PS: To check whether TensorFlow is installed, you can check QuPath under Help > Installed extensions, where all extensions are listed.

1 Like

Yes,I did not install TensorFlow yet.I have this error. I simply don’t know where to put Gradle command (gradlew clean build createPackage -Ptensorflow-cpu=true` ).
Thanks

It looks like your build was successful though? But I suppose you used the normal command (which exclude TensorFlow, as mentioned in the build log).
Instead of using this command:

gradlew clean build createPackage

Use this command:

gradlew clean build createPackage -Ptensorflow-cpu=true
1 Like


It shows this error again.

With thanks

The log mentions that What went wrong is that:

Creating a package requires jpackage in JDK 14, but the current JDK version is 11

Basically, you have to get a newer version of the JDK, you can find more info here, with a link to AdoptOpenJDK where you will find the relevant file.

1 Like

Problem solved. successfully installed tensor flow. I run script from Stardist . Running has no error but it did not change my nuclear segmentation better.