How do I detect tissue?#

What will I learn?

  • How to detect tissue regions with a QuPath thresholder.

  • How to detect tissue regions with a QuPath pixel classifier.

  • How to detect tissue regions with an ImageJ auto-threshold method.

A common task in image analysis is to identify regions that are clearly different from their surroundings. This could be a difference in lightness, or an obvious change in hue or colour, among other possibilities.

The annotation tools described in How do I draw annotations? can be useful here, but it can be faster and more reproducible to use a threshold or classifier to segment the image.

We will use the CMU-1.svs image from the OpenSlide test data:

../../_images/1-gui.jpg

Fig. 35 The CMU-1.svs image opened in QuPath#

Thresholding images#

A simple form of region detection is known as thresholding. Here, we aim to find a single value that can “split” the image into two classes of pixel: usually, background pixels and foreground pixels. We typically compare every pixel’s value to this threshold, and based on whether they are above or below the threshold, we assign them to either background or foreground.

In brightfield images, typically we assign dark pixels to the foreground, whereas in fluorescence images, typically dark pixels belong to the background.

Question

  1. Open CMU-1.svs.

  2. Mouse over the background and tissue regions and inspect the pixel values displayed in the bottom right of the viewer.

Is there a value you could use to “split” these two classes of pixels?

QuPath’s thresholder#

What to do

  1. Run Classifer ‣ Pixel classification ‣ Create thresholder.

  2. In the dropdown menu for Channel select Average Channels.

  3. In the dropdown menu for Below threshold select Region*.

  4. Adjust Threshold until the you think the tissue is properly detected (and most of the background isn’t).

  5. Adjust Smoothing sigma until you are happy with the output.

After adjusting these settings, you should see an overlay that previews the result of applying this threshold:

../../_images/2-thresholder_settings.jpg

Fig. 36 Example threshold settings for CMU-1.svs#

Note

In the thresholder, you can adjust a number of additional parameters. These are described in detail in the QuPath docs.

Saving a thresholder#

If you’re happy with the preview overlay of the thresholder and want to apply it using the same parameters to other images, you’ll need to save the thresholder somehow.

QuPath can do this by creating a very simple ‘pixel classifier’, which contains the information about resolution, smoothing, channel and threshold value. This pixel classifier is usually saved in the QuPath project.

What to do

  1. Enter a memorable name in the Classifier name field.

  2. Click Save.

  3. Click Create objects which will present you with more options. Here, you can decide whether to create objects across the full image, within annotated regions, or within the selected regions.

  4. Select Full image and click OK.

../../_images/3-thresholder_selection.jpg

Fig. 37 Thresholder selection sub-window#

After this, you can select another set of options. These give you control over the exact types of objects that will be created, whether holes and fragments are cleaned up, and what to do with existing objects. 5. Click OK on this dialog:

../../_images/4-thresholder_create.jpg

Fig. 38 Thresholder object creation settings#

The final result should be detected tissue regions, including QuPath classifications.

../../_images/5-thresholder_done.jpg

Fig. 39 Results of running QuPath thresholder#

QuPath pixel classifiers#

You can think of a thresholder as a pixel classifier. That is, a classifier that takes pixel values as input and outputs a class name for each pixel in the image depending on its values.

In the case of a thresholder, we “train” the classifier by manually adjusting the parameters that identify pixels as belonging to one class or another. However, QuPath also provides more sophisticated settings for training pixel classifiers.

Classifying regions#

Within the thresholded areas we detected just now, we can identify differing tissue regions. Here, we’ll work to distinguish the regions with densely-packed, strongly-staining nuclei and the rest of the tissue we detected.

../../_images/6-classifier_parents.jpg

Fig. 40 Pixel classifier parent objects#

Within the thresholded areas we detected just now, we can identify differing tissue regions. Here, we’ll work to distinguish the regions with densely-packed, strongly-staining nuclei and the rest of the tissue we detected.

../../_images/7-classifier_pane.jpg

Fig. 41 Pixel classifier pane#

What to do

  1. Draw an annotation in the background (white) region and assign it the class Ignore*.

  2. Draw an annotation in the densely-packed, purple-ish region and assign it the class Negative.

  3. Draw an annotation in the other, red-ish tissue region and assign it the class Positive.

  4. ClickLive prediction to enable an overlay that shows the results of applying the trained pixel classifier to the image.

../../_images/8-classifier_preview.jpg

Fig. 42 Pixel classifier live preview#

You can add more annotations to refine the predictions.

Tip

Generally, it is better to add small and diverse annotations rather than large annotations of similar regions. Providing large amounts of similar regions will only serve to make training slower without increasing accuracy for difficult regions.

You should also aim to have roughly the same number of training samples for each class (shown in the pie chart), or at least proportional to the size of each class in the image.

Note

There are additional options for the pixel classifier, some of which are the same as the thresholder settings shown previously. These are described in detail in the QuPath docs.

Saving and applying a pixel classifier#

Once you are happy with the settings of the pixel classifier, you can save it into the project under a suitable name. As with the thresholder, this will enable some options and make the classifier available for scripting.

What to do

  1. In the Train pixel classifier window, enter a memorable name under Classifier name.

  2. Click Save.

  3. Click Create objects and follow the same process as with the thresholder.
    In this case it will be the current selection since we are doing it inside the annotation, rather than the full image.

../../_images/9-classifier_selection.jpg

Fig. 43 Pixel classifier parent object selection#

  1. Choose options for the objects created:

../../_images/10-classifier_create.jpg

Fig. 44 Pixel classifier object creation settings#

After a short wait (longer with high-resolution classifiers) we are left with regions corresponding to the classes we added to the training annotations earlier:

../../_images/11-classifier_done.jpg

Fig. 45 Results of running QuPath pixel classifier#

Challenge

  1. Open CMU-1-Small-Region.svs.

  2. Load the pixel classifier you just saved (Classify ‣ Pixel classification ‣ Load pixel classifier…)

  3. Apply the pixel classifier to the entire image.

  4. How good are the results?
    Would seeing this result affect how you would train the classifier?
    Would you expect similar results on an image from a different scanner, or of a different tissue section?

Using ImageJ threshold methods in QuPath#

Thresholders and pixel classifiers require manual input. This is fine for small projects with similar images, but identifying a single threshold that works across many images in a project can be challenging. ImageJ provides methods for automatically identifying and applying a threshold that can be useful here.

QuPath includes an ImageJ script runner for sending regions to and from ImageJ with QuPath, and for running ImageJ macros and scripts.

What to do

  1. Run Extensions ‣ ImageJ ‣ ImageJ script runner.

  2. In the script runner, select File ‣ Open example ‣ Auto threshold.ijm.

../../_images/12-imagej_runner_script.jpg

Fig. 46 ImageJ script runner#

  1. For Apply to, select Whole image, and for Return Roi as select Annotation.

  2. Click Run ImageJ macro.

After clicking Run ImageJ macro, QuPath will send an appropriately-sized image to ImageJ, run our macro, and retrieve any ROI or overlay objects as specified in the settings:

../../_images/13-imagej_results.jpg

Fig. 47 Results of ImageJ thresholder in QuPath#

Tip

You can find more information on the ImageJ script runner in the QuPath docs.

Challenge: the biggest circle#

You’ve learned a lot about thresholding images! Now, combine it with your knowledge of QuPath’s measurements for this puzzle.

Puzzle

  1. Open circles.png in QuPath.

  2. Try to find the largest circle.

  3. Find its area in pixels2.