back home next
Ex 5.1: QuPath built-in method • Ex 5.3: Segmentation quality control

5. Cells detection¶

5.2 Use StarDist in QuPath¶

Depending on the staining, segmenting nuclei can be a pretty tough task, particularly if you try to stick to QuPath built-in method. To address that issue, several deep-learning architectures exist and have a QuPath plugin so we can use them from inside the software. The first that we are going to try is StarDist.

StarDist exists for both fluo and IHC and works by trying to find star-convex polygons.

Before starting this exercise, make sure that you installed the StarDist plugin for QuPath and that you downloaded the models (as it is explained in the requirements).

5.2.1 Count nuclei in mouse spleen (again)¶

Goals:¶

  • In a later exercise, we will try to find a way to quantify how good is a segmentation. For this reason, use in this exercise the same images that you processed in Ex: 5.1.2 Count nuclei in mouse spleen.
  • We need to count nuclei using StarDist.
  • To do so, we will need to:
    • Open StarDist's template script
    • Provide the path of the correct model
    • Tune the model's settings

Required data:¶

Folder Description Location License
NuInsSeg H&E images of mouse spleen Kaggle: NuInsSeg CC-BY-4.0

A. Use StarDist's template¶

  • In the top-menu bar, go to "Extensions" > "StarDist" > "StarDist H&E nuclei detection script". This should open a script template that we will be able to use after adjusting the different parameters.
  • You can modify it as much as you want and save it, the template will be left untouched when you will reload it.
a. Provide the model's path¶
  • The first thing to change is the model's path (modelPath). In the models that you downloaded, one should be named "he_heavy_augment.pb". You must provide it's full absolute path within the quotes.

Warning

If you are a Windows user, whenever you must insert a path in quotes, you have to either double each antislash or replace them with slashes. For example, if your path is "C:\Users\somebody\Desktop\he_heavy_augment.pb", you must change it to either "C:\\Users\\somebody\\Desktop\\he_heavy_augment.pb" or "C:/Users/somebody/Desktop/he_heavy_augment.pb".

b. Adjust the settings¶
  • The next step will be to change the settings of the StarDist object.
  • Just as we did with the other method, this will be an iterative process, you won't find the correct ones on the first try.
  • To search for the settings, run your different attempts on a small QP rect rectangle to avoid waiting for too long between each try.
  • Before running the script, make sure that your testing annotation is active (drawn in yellow in the viewer).
  • As many times as it takes, you will:
    • Run the script for this image.
    • If the objects are over segmented: increase the pixel size, the model is looking for smaller objects, we can find a way around that by downsampling the image.
    • If the dim/blurry objects are forgotten: lower the probability threshold, these objects have a lower "probability" in the output of inference.
c. Classify the cells¶
  • In the previous exercise, we used the QP groovy recursive_set_class.groovy script to attribute a class to our segmented nuclei.
  • Using the StarDist object, we don't need it anymore! A method is present to do so right off the bat. You can look for it in StarDist's QuPath API: a good keyword to look for it would be something about "class", "classification" or "classify".
  • Use the method that you just found to give the StarDist class to your to-be-created objects.
  • If you struggle finding your settings, a starting point could be:
def stardist = StarDist2D
    .builder(modelPath)
    .normalizePercentiles(1, 99)
    .threshold(0.3)
    .pixelSize(0.5)
    .measureShape()
    .measureIntensity()
    .classify("StarDist")
    .build()

B. Run the script for the project¶

a. Prepare the script¶
  • StarDist's template script is configured by default to run on the active object. You can see it at the line stating: def pathObjects = QP.getSelectedObjects().
  • So, before we reach this line in the script, we must have some active annotation in our image.
  • Wherever you want before this line, you must add the instruction to create a full image annotation as we did before.
  • If you struggle finding it back, the instruction is: createFullImageAnnotation(true).
b. Launch the script¶
  • Before running the script for the whole project, don't forget to remove the annotation in which you made your tests.
  • Using the QP more options "more options" button, run the script for the whole project.
  • Once the execution is done, don't forget to save the project.
  • If you wish, you can export the measurements and count the cells in LibreOffice, but it is not required.

Note

Save this project and keep it clean, we will reuse it in the next exercise.