Binarization
Prerequisites
Before starting this lesson, you should be familiar with:
Learning Objectives
After completing this lesson, learners should be able to:
Describe the relationship between an intensity image and a derived binary image.
Apply a threshold to distinguish foreground and background pixels
Motivation
One strategy to detect objects or specific regions in images is to first distinguish so-called background pixels, which do not contain objects or interesting regions, from foreground pixels, which mark the areas of interest. This process is called two class semantic segmentation and is often referred to as image binarization. The foreground regions can then be further processed, e.g to detect objects or perform intensity measurements.
Concept map
Example figure
Image thresholding
A common algorithm for binarization is thresholding. A threshold value t
is chosen, either manually or automatically,
and all pixels with intensities below t
are set to 0, whereas pixels with intensities >= t
are set to the value for the foreground.
Depending on the software the foreground value can be different (e.g. 1 in MATLAB or 255 in ImageJ).
p_im(x,y) < t
-> p_bin(x,y) = 0
p_im(x,y) >= t
-> p_bin(x,y) = 1
It is also possible to define an interval of threshold values, i.e. a lower and upper threshold value. Pixels with intensities within this interval belong to the foreground.
Activity
- Open the binary image xy_8bit_binary__nuclei.tif.
- Discuss the image data type and the pixel values.
- Open the image xy_8bit__two_cells.tif and binarize it by applying a manually defined threshold.
ImageJ GUI
- Open a binary image xy_8bit_binary__nuclei.tif and discuss data-type (data-type is unsigned 8-bit values, minimal value is 0, maximal value is 255)
- Open binary file from MATLAB xy_8bit_binary__two_cells_matlabstyle.tif lower and highest value (data-type is unsigned 8-bit values, minimal value is 0, maximal value is 255)
- Verify that binary options are set up correctly.
- [Process > Binary > Options ..] [x] Black background
- [ File > Open… ] xy_8bit__two_cells.tif
- [ Image > Adjust > Threshold… ] or [Ctrl-Shift-T], vary lower threshold and discuss what happens to preview image
- Choose a value so that both cells are foreground, e.g. 49-255
- Press [Apply] discuss resulting image
- [ File > Open… ] xy_8bit__two_cells.tif
- Apply a higher threshold so that only the high intensity level nuclei remains
ImageJ Macro
open("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__two_cells.tif"); setThreshold(49, 255); setOption("BlackBackground", true); run("Convert to Mask"); open("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__two_cells.tif"); setThreshold(88, 255); setOption("BlackBackground", true); run("Convert to Mask");
ImageJ Jython
from ij import IJ, ImagePlus from ij.plugin import Thresholder inputImage = IJ.openImage("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__two_cells.tif") IJ.setRawThreshold(inputImage, 44, 255, None) binaryImage = ImagePlus('Binary image 2 nuclei', Thresholder.createMask(inputImage)) binaryImage.show() IJ.setRawThreshold(inputImage, 88, 255, None) binaryImage_1 = ImagePlus('Binary image 1 nuclei', Thresholder.createMask(inputImage)) binaryImage_1.show()
MATLAB
%These matlab scripts illustrate separating the foreground from the %background using a threshold value provided by the user %Prompt user for a threshold value thres_val = input('Enter a threshold value: '); % Prompt user to choose an image, e.g. xy_8bit__two_cells.tif [file, path] = uigetfile("*.tif"); %Read input image in_image = imread(fullfile(path, file)); %display input image figure; imagesc(in_image); %Binarize input image with the threshold valuein_image bin_image = uint8(in_image>= thres_val); % Display binary image figure; imagesc(bin_image);
KNIME
Python
import numpy as np import matplotlib.pyplot as plt from skimage import io # load the image from file image_file = "/image_data/xy_8bit__two_cells.tif" image = io.imread(image_file) # binarize the image, so that all values larger than the threshold are foreground threshold_value = 60 binarized = image > threshold_value # display the original and the binarized image fig, ax = plt.subplots(2) ax[0].imshow(image) ax[0].set_title("Image") ax[1].imshow(binarized) ax[1].set_title("Binarized") # For associated course material in jupyter, go to https://nbviewer.jupyter.org/github/embl-bio-it/image-analysis-with-python/blob/carpentry/image-analysis-session/image-binarization.ipynb#Image-Binarization # You can also spin up an interactive binder session: https://gke.mybinder.org/v2/gh/embl-bio-it/image-analysis-with-python/carpentry?filepath=image-analysis-session/image-binarization.ipynb
Exercises
Show exercises for:ImageJ GUI
Open image xy_8bit__PCNA.tif and
- Find a threshold value so that there are 2 foreground nuclei.
- Find a threshold value so that only the bright dots remain
- Find threshold interval so that only the boundary of the nuclei remains.
Solution
[File > Open…] xy_8bit__PCNA.tif [Image > Adjust > Threshold…] or [Ctrl-Shift-T] opens the threshold menu
- Lower threshold, i.e. upper slider, at about 5
- Lower threshold at about 44
- Lower threshold at about 4, upper threshold at about 4-5
ImageJ Macro
Open image xy_8bit__PCNA.tif and
- Find a threshold value so that there are 2 foreground nuclei.
- Find a threshold value so that only the bright dots remain
- Find threshold interval so that only the boundary of the nuclei remains.
Solution
open("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__PCNA.tif") selectWindow("xy_8bit__PCNA.tif"); run("Duplicate...", "title=[2 nuclei]"); selectWindow("xy_8bit__PCNA.tif"); run("Duplicate...", "title=[boundary]"); selectWindow("xy_8bit__PCNA.tif"); run("Duplicate...", "title=[dots]"); selectWindow("2 nuclei"); setThreshold(5, 255); setOption("BlackBackground", true); run("Convert to Mask"); selectWindow("boundary"); setThreshold(4, 4); setOption("BlackBackground", true); run("Convert to Mask"); selectWindow("dots"); setThreshold(44, 255); setOption("BlackBackground", true); run("Convert to Mask");
ImageJ Jython
Open image xy_8bit__PCNA.tif and
- Find a threshold value so that there are 2 foreground nuclei.
- Find a threshold value so that only the bright dots remain
- Find threshold interval so that only the boundary of the nuclei remains.
Solution
from ij import IJ, ImagePlus from ij.plugin import Thresholder # image is xy_8bit_PCNA.tif inputImage = IJ.openImage("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__PCNA.tif") IJ.setRawThreshold(inputImage, 5, 255, None) binaryImage = ImagePlus('Binary image 2 nuclei', Thresholder.createMask(inputImage)) binaryImage.show() IJ.setRawThreshold(inputImage, 4, 4, None) binaryImage_boundary = ImagePlus('Binary boundary', Thresholder.createMask(inputImage)) binaryImage_boundary.show() IJ.setRawThreshold(inputImage, 44, 255, None) binaryImage_brightdots = ImagePlus('Binary bright dots', Thresholder.createMask(inputImage)) binaryImage_brightdots.show()
Solution with input parameters
#@ Integer (label="Lower threshold") thr1 #@ Integer (label="Upper threshold") thr2 from ij import IJ, ImagePlus from ij.plugin import Thresholder # image is xy_8bit_PCNA.tif should be already open inputImage = IJ.openImage("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__PCNA.tif") IJ.setRawThreshold(inputImage, thr1, thr2, None) binaryImage = ImagePlus('Thresholded image', Thresholder.createMask(inputImage)) binaryImage.show()
Assessment
Fill in the blanks
- Pixels in a binary image can have maximally ___ different values.
- If the threshold is larger than the maximal pixel value in the intensity image, all pixels in the binary image have a value of ___.
Solution
- Pixels in a binary image can have maximally 2 different values.
- If the threshold is larger than the maximal pixel value in the intensity image, all pixels in the binary image have a value of 0.
True or False
- There is only one correct threshold value in order to convert an intensity image into a binary image.
- Binary images are always unsigned 8-bit where the foreground is 255.
Solution
- There is only one correct threshold value in order to convert an intensity image into a binary image. False
- Binary images are always unsigned 8-bit where the foreground is 255. False
Follow-up material
We recommend reading these modules next:
Learn more: