How do I count positive cells?#
What will I learn?
How to annotate tumour regions
How to detect cells (again)
How to inspect the results
How to adjust parameters
How to find the parameters you used earlier
How to classify cells as positive or negative
How to detect & classify in one step
OS-2.ndpi shows a tissue sample with different cell types, stained for Ki67 – a marker of cell proliferation.
The positive cells are brown (stained with DAB), the negative cells are blue (stained with haematoxylin).
The question we want to answer is:
What percentage of tumour cells are positive for Ki67?
Cells divide, and it’s normal for some to be positive for Ki67. But understanding how quickly the tumour cells are dividing can help determine the most appropriate treatment for several types of cancers.
One approach we can take is to
Draw an annotation that we think only contains tumour cells.
Calculate the percentage of cells inside the annotation that are positive.
That’s what we’ll do in this chapter. The manual annotation is laborious, but it can be the best approach if you don’t have too many images and you want full control over defining what cells are counted.
Later, we’ll draw a bigger annotation that contains a mix of tumour and non-tumour cells. Then we’ll need to add an extra step to train QuPath to tell the difference.
How to annotate tumour regions#
What to do
Open
OS-2.ndpiChoose Brightfield H-DAB as the image type
Draw an annotation around a small region containing tumour cells
Use one of the tools introduced in How do I draw annotations?
We won’t be diagnosing or treating anyone based on these results: you don’t need to do this with pathologist-level accuracy. If you aren’t confident you can tell which are the tumour cells, aim to annotate cells that look distinctively bigger.
Aim to create a single annotation that surrounds at least 100 cells.
Fig. 49 Annotation surrounding tumour cells#
Tip
I’d suggest using the polygon or brush tool. These can draw complex shapes and are easier to control than the wand.
How to detect cells#
You can use exactly the same method to detect cells in this image as in the previous image.
This works because the image type has been set to Brightfield H-DAB: the image type tells QuPath the kind of staining you have, and the cell detection uses this information.
What to do
Open
Click Run to run the cell detection in your tumour annotation
Fig. 50 Cell detection in Ki67 image#
The order matters!
You need to set the image type correctly before you open the dialog.
If you change the image type after opening the dialog, then the parameters won’t automatically update. You’ll need to close the dialog, then once more run .
How to inspect the results#
QuPath’s cell detection is designed to do a reasonable job on a lot of images – and the default parameters are often ok for brightfield images. Still, it might not do a great job on your images.
It’s important to inspect the results. You can toggle how the cells are displayed, to help get a feeling for how accurate the detection is.
What to do
Pan and zoom in/out to see if the cell detection looks good to you
Press the F key repeatedly to fill/unfill your cells
Press the D key repeatedly to show/hide your cells (detections)
Right-click on the image and explore the options in the popup menu
Fig. 51 Changing how cells are displayed to help check the results#
Question
Where in QuPath’s user interface can you see the number of cells inside your annotation?
How to adjust parameters#
The Cell detection dialog contains a lot of parameters. As we’ve seen, sometimes changing one parameter can make the results a lot better.
You can hover the mouse over each parameter to see a description. Deciphering these may require some background in image processing – but some are quite intuitive.
Choosing the detection image#
What to do
Open

Compare the Hematoxylin and Optical density sum channels
Select the Show grayscale checkbox so they are easier to compare
A common change to try when optimizing cell detection in brightfield images is to switch the ‘Detection image’:
Hematoxylin OD uses digital stain separation to try to focus only on the nuclear staining
Optical density sum uses staining intensity, regardless of colour
Question
Which channel do you think we should use for detecting nuclei?
Hint: Decide which channel shows both the positive and the negative cell nuclei clearly.
Neither option is inherently better. The one that works best will depend upon the staining, which is why both options exist. But by looking at the channels individually, you can make an educated guess which will be better for your images.
What to do
Change the Detection image parameter from Hematoxylin OD to Optical density sum
Press Run to detect the cells again
Inspect the results - do they look better or worse?
Fig. 52 Cell detection using the haematoxylin (stain-separated) image#
Fig. 53 Cell detection using the optical density sum image#
Choosing the requested pixel size#
What to do
Explore what happens when you change the the Requested pixel size parameter
Use values of
0.25,0.5(the default),1.0and2.0
Check how the requested pixel size affects the number of detected cells and the accuracy of their boundaries
Sometimes the best way to understand what a parameter does is to draw a small annotation and the experiment by setting it to different values.
Question
Which requested pixel size would you prefer to use?
How to find the parameters you used earlier#
What to do
Click on the Workflow tab
Click on some of the entries that say ‘Cell detection’
Check the parameter values at the bottom of the side bar
Double-click on the first entry that says ‘Cell detection’
If you’re changing parameters a lot, it’s easy to lose track of what you’ve done. Fortunately, the Workflow tab in QuPath contains a log.
This log doesn’t contain absolutely everything (e.g., it doesn’t log when you draw an annotation), but it does log when you press Run in the cell detection dialog.
You can click on rows of the workflow table to find the parameters used when you ran cell detection in the past. Double-clicking on an entry should launch the cell detection dialog, with parameters initialized to match the values that were set at the time.
Fig. 54 Viewing the recorded workflow to see previous commands & their parameters#
How to classify cells as positive or negative#
Once we’re reasonably happy with detection, we can move on to distinguish between cells that are positive and negative.
QuPath can do that by assigning a classification to the cell (often called a class for short). Then it can automatically count the number of cells with each class.
We first want to create space where we expect these counts to appear.
What to do
Click on the Annotations tab – you should be able to see ‘Num detections’ at the bottom
Click and drag the separator to make sure there are some empty rows in the measurement table below ‘Num detections’
Fig. 55 Measurement table for the selected annotation#
Now we can actually assign the classes.
What to do
Run
For ‘Measurement’, select Nucleus: DAB OD mean
Check that new measurements appear, including ‘Positive %’ under Annotations
Fig. 56 Setting cell classes based on DAB stain values#
Ki67 is a nuclear marker and the staining is DAB – so we want to classify cells as positive or negative based on Nucleus: DAB OD mean.
Question
What measurement would you use for a marker located in the cytoplasm, not the nucleus?
Question
What percentage of cells are considered Ki67 positive in your annotation?
The initial results will use a threshold value to decide when the nucleus is ‘brown enough’ to be called positive. Depending upon staining, this value might not always be appropriate. The slider allows us to change the threshold.
What to do
Adjust the slider for ‘Threshold 1+’
See that the cells change colour and ‘Positive %’ also changes
Click Apply when you’ve got a threshold that seems appropriate
Fig. 57 Ki67 cell classification with a low threshold#
Fig. 58 Ki67 cell classification with a high threshold#
Question
How does the percentage of positive cells change if the threshold is low or high?
Tip
For Ki67, we normally apply one threshold: a cell can be positive or negative.
For some biomarkers (e.g. oestrogen receptor), it’s common to apply multiple thresholds – often 3. This can distinguish between cells with weak, moderate and strong positive staining.
That’s why QuPath provides 3 sliders to set thresholds.
How to count cells in the annotation tab#
Now that you have detected some cells, you can find out what the proportion of positive cells is within the region you annotated.
What to do
Ensure the analysis pane
is open (Shift+A).Click the Annotations tab within the analysis pane.
Select the annotation you are interested in, either in the annotation list or by clicking it in the viewer.
View the details of the annotation within the Measurements pane.
Fig. 59 Annotation measurements pane#
How to detect & classify in one step#
Because finding the percentage of positive cells is such a common task, there’s a single command that combines these two steps into one.
What to do
Create a new annotation containing only tumor cells.
Open .
Compare the options in the dialog to the other dialogs you have seen in this chapter.
Click Run to run the positive cell detection in your tumour annotation.
Fig. 60 Positive cell detection for Ki67#
Question
What percentage of cells are considered Ki67 positive in this new annotation? Is it higher or lower than the previous example?
How to compare cell counts across annotations#
What to do
Use .
If you want to make it a bit more concise, deselect the toggle buttons Show object IDs and Show images.
Click on the column head Positive % to sort by this column (you may also want to drag it to the left side of the table).
If you want to compare multiple annotations, it would get a bit tedious to click on one in the annotation panel, note down the number of detections, and then repeat these steps for every annotation.
Luckily, we can instead view a table summarizing multiple annotations.
Fig. 61 Annotation measurements table#