Automatic thresholding (histogram-based)
Prerequisites
Before starting this lesson, you should be familiar with:
Learning Objectives
After completing this lesson, learners should be able to:
Understand how an image histogram can be used to derive a threshold
Apply automatic threshold to distinguish foreground and background pixels
Motivation
The manual determination of a threshold value is tedious and subjective. This is problematic as it reduces the reproducibility of the results and may preclude determining threshold values for many different images as the dataset becomes large. It is therefore important to know about reproducible mathematical approaches to automatically determine threshold values for image segmentation.
Concept map
graph TD
I("Image") --> H("Histogram")
H -- algorithm --> T("Threshold value")
Figure
![](/training-resources/figures/auto_threshold.png)
Key points
- Most auto thresholding methods do two class clustering
- If the histogram is bimodal, most automated methods will perform well
- If the histogram has more than two peaks, automated methods could produce noisy results
Activities
- Manual vs. auto thresholding
- Open xy_8bit__nuclei_without_offset.tif and xy_8bit__nuclei_with_offset.tif
- Explore the differences in doing manual and auto thresholding
Show activity for:
ImageJ GUI
- Manual vs. auto thresholding
- Open xy_8bit__nuclei_without_offset.tif
- [ Image > Adjust > Threshold… ]
- [X]
Dark Background
- Selecting
Lower threshold level = 20
is a good example. Note down this value- Press
Reset
- Don’t press
Set
orApply
- Open xy_8bit__nuclei_with_offset.tif
- [ Image > Adjust > Threshold… ]
- [X]
Dark Background
- Selecting
Lower threshold level = 20
now does not work (i.e. all pixels are foreground)- Here, selecting
Lower threshold level = 40
works- Press
Reset
- Select window xy_8bit__nuclei_without_offset.tif
- [ Image > Adjust > Auto Threshold ]
Method = Try all
- [X]
White objects on black background
- Keep all other options unchecked
- Press
OK
- Select window xy_8bit__nuclei_with_offset.tif
- [ Image > Adjust > Auto Threshold ]
Method = Try all
- [X]
White objects on black background
- Keep all other options unchecked
- Press
OK
- In auto thresholding several methods produce acceptable results and one can get rid of selecting manual threshold values for each different image
Exercises
- Auto thresholding on image stack
- Open xyz_8bit__nuclei_autothresh.tif
- Select any threshold method and observe the differences in segmentation when you use the histogram computed from all images in 3D stack
ImageJ GUI
- Auto thresholding on stack
- Open xyz_8bit__nuclei_autothresh.tif
- [ Image > Duplicate… ]
Title = nohist
- [X]
Duplicate stack
- Select window
nohist
- [ Image > Adjust > Auto Threshold ]
Method = Try all
- [X]
White objects on black background
- [X]
Stack
- Press
OK
- It can be seen that for many methods, background is also segmented. This is due to the fact that in this case auto threshold algorithms is treating each slice separately (segmentation is done based on slice histogram)
- Select a method e.g. Otsu and repeat the above process using
Method = Otsu
- [X]
Stack
- [X]
Show threshold values in log window
- Press
OK
- It can be seen that Otsu’s method is calculating threshold for each individual slice
- Select window
xyz_8bit__nuclei_autothresh.tif
- [ Image > Duplicate… ]
Title = hist
- [X]
Duplicate stack
- Select window
hist
and repeat the procedure above using
Method = Otsu
- [X]
Stack
- [X]
Use stack histogram
- [X]
Show threshold values in log window
- Press
OK
- It can be observed that now one threshold value (i.e. 91, see log window) is used for binarization and background is not segmented
Assessment
True or False
- Using stack histogram yields only one threshold value for binarization when applying auto thresholding
- Auto thresholding gives better segmentation results than manual thresholding in the presence of noise
Solution
- True
- False
Follow-up material
Recommended follow-up modules:
Learn more:
Some common automatic thresholding methods can be studied here Imagej.net Auto-threshold