Loading [MathJax]/jax/output/CommonHTML/jax.js
ImageDev

AutoThresholdingBright

Computes and applies an automatic threshold on a gray level image to detect bright particles on a dark background.

Access to parameter description

For an introduction: This algorithm computes an automatic threshold on a grayscale image; that is, it separates the input image in two classes of pixels from an input range [I1,I2]. Four methods of classification are available to determine C0 and C1: Entropy, Factorisation, Moments, and Isodata.
The computed threshold is returned in the AutoThresholdingMsr object.

Entropy

The entropy principle defines 2 classes in the image histogram by minimizing the total classes entropy. For a more theoretical context, one can refer to references [1] and [2].
Considering the first-order probability histogram of an image and assuming that all symbols in the following equation are statistically independent, its entropy (in the Shannon meaning) is defined as: H=ni=0p[i]×log(p[i])2 Where n+1 is the number of grayscales, p[i] the probability of the i level occurrence, and log(x)2 is the binary logarithm.

Let us denote t as the value of the threshold, and [I1,I2] the search interval. One can define two partial entropies: Hw[t]=tI1p1[i]×log(p1[i])2 Hb[t]=I2t+1p2[i]×log(p2[i])2 Where p1[i] defines the probability of the i level occurrence in the range [I1,t] and p2[i] defines the probability of the i level occurrence in the range [t+1,I2]. We search the threshold value T which minimizes the sum S(t)=Hw[t]+Hb[t]: T=argmint(Hw[t]+Hb[t])


Figure 1. Example of thresholding using the entropy method

Factorization

The factorization method is based on the Otsu criterion [3], i.e., minimizing the within-class variance of classes C0=[I1,t] and C1=[t+1,I2]: σ2W[t]=w0[t]×σ20[t]+w1[t]×σ21[t] Where w0[t] and w1[t] are the occurrence probabilities, σ20 and σ21 the variances, of classes C0 and C1.

A faster and equivalent approach is to maximize the between-class variance: σ2B[t]=w0[t]×w1[t]×(μ0[t]μ1[t])2 Where μ0 and μ1 are respectively the mean of the class C0 and C1.
The within-class variance calculation is based on the second-order statistics (variances), while the between-class variance calculation is based on the first order statistics (means). It is therefore simpler and faster to use this last optimization criterion. We then search the value T which maximizes the between-class variance such as: T=argmint(σ2B[t])


Figure 2. Example of thresholding using the factorization method

Moments

The moment method uses the moment-preserving bi-level thresholding described by W.H.Tsai in [4].
Moments of an image can be computed from its histogram in the following way: mj=ni=0p[zi]j Where p[zi] is the probability of occurrence of grayscale zi.

For the following we note f as the original grayscale image and g as the thresholded image. Image f can be considered as a blurred version of an ideal bi-level image which consists of pixels with only two gray values: z0 and z1.

The moment-preserving thresholding principle is to select a threshold value such that if all below-threshold gray values of the original image are replaced by z0 and all above threshold gray values replaced by z1, then the first three moments of the original image are preserved in the resulting bi-level image. Image g so obtained may be regarded as an ideal unblurred version of f.
Let p0 and p1 denote the fractions of the below-threshold pixels and the above-threshold pixels in f, respectively, then the first three moments of g are: mj=pj0+pj1, j=0,1,2,3 And preserving the first three moments in g, means the equalities: mj=mj, j=0,1,2,3 To find the desired threshold value T, we can first solve the four equations system to obtain p0 and p1, and then choose T as the p0-tile of the histogram of f. Note that z0 and z1 also will be obtained simultaneously as part of the solutions of system.



Figure 3. Example of thresholding using the moment-preserving method

Isodata

The Isodata method implements an iterative global thresholding algorithm, which is based on the gray value histogram of the data [5].

From an initial threshold t, a new threshold T is computed as the mean value of the averages of both classes: T=μ0[t]+μ1[t]2 Where μ0 and μ1 are respectively the mean of the class C0 and C1.
This process is reiterated until convergence; that is, when the new threshold T is not significantly different than the previous one t.



Figure 4. Example of thresholding using the Isodata method


References: See also
See related example

Function Syntax

This function returns a AutoThresholdingBrightOutput structure containing outputBinaryImage and outputMeasurement.
// Output structure of the autoThresholdingBright function.
struct AutoThresholdingBrightOutput
{
    /// The output binary image. Its dimensions are forced to the same values as the input.
    std::shared_ptr< iolink::ImageView > outputBinaryImage;
    /// The computed threshold value.
    AutoThresholdingMsr::Ptr outputMeasurement;
};

// Function prototype
AutoThresholdingBrightOutput autoThresholdingBright( std::shared_ptr< iolink::ImageView > inputGrayImage, AutoThresholdingBright::RangeMode rangeMode, iolink::Vector2d intensityInputRange, AutoThresholdingBright::ThresholdCriterion thresholdCriterion, std::shared_ptr< iolink::ImageView > outputBinaryImage = NULL, AutoThresholdingMsr::Ptr outputMeasurement = NULL );

Class Syntax

Parameters

Parameter Name Description Type Supported Values Default Value
input
inputGrayImage
The input grayscale image. Image Grayscale nullptr
input
rangeMode
The way to determine the input intensity range.
MIN_MAX The histogram is computed between the minimum and the maximum of the image.
OTHER The histogram is computed between user-defined bounds [a,b].
Enumeration MIN_MAX
input
intensityInputRange
The input intensity range [a,b] inside which the threshold is searched. This parameter is ignored if the range mode is set to MIN_MAX. Vector2d Any value {0.f, 255.f}
input
thresholdCriterion
The criterion to compute the threshold from the histogram.
ENTROPY The measure of dispersion used in the algorithm is the entropy of the intensity distribution.
FACTORISATION The measure of dispersion used in the algorithm is the variance of the intensity distribution (also known as Otsu method).
MOMENTS The measure of dispersion used in the algorithm is the moments of the intensity distribution.
ISODATA The measure of dispersion used in the algorithm is the isodata of the intensity distribution.
Enumeration ENTROPY
output
outputBinaryImage
The output binary image. Its dimensions are forced to the same values as the input. Image nullptr
output
outputMeasurement
The computed threshold value. AutoThresholdingMsr nullptr

Object Examples

std::shared_ptr< iolink::ImageView > polystyrene = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene.tif" );

AutoThresholdingBright autoThresholdingBrightAlgo;
autoThresholdingBrightAlgo.setInputGrayImage( polystyrene );
autoThresholdingBrightAlgo.setRangeMode( AutoThresholdingBright::RangeMode::MIN_MAX );
autoThresholdingBrightAlgo.setIntensityInputRange( {0, 255} );
autoThresholdingBrightAlgo.setThresholdCriterion( AutoThresholdingBright::ThresholdCriterion::ENTROPY );
autoThresholdingBrightAlgo.execute();

std::cout << "outputBinaryImage:" << autoThresholdingBrightAlgo.outputBinaryImage()->toString();
std::cout << "threshold: " << autoThresholdingBrightAlgo.outputMeasurement()->threshold( 0 ) ;

Function Examples

std::shared_ptr< iolink::ImageView > polystyrene = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene.tif" );

auto result = autoThresholdingBright( polystyrene, AutoThresholdingBright::RangeMode::MIN_MAX, {0, 255}, AutoThresholdingBright::ThresholdCriterion::ENTROPY );

std::cout << "outputBinaryImage:" << result.outputBinaryImage->toString();
std::cout << "threshold: " << result.outputMeasurement->threshold( 0 ) ;