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:
The computed threshold is returned in the AutoThresholdingMsr object.
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=−n∑i=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]=−t∑I1p1[i]×log(p1[i])2 Hb[t]=−I2∑t+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
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 of an image can be computed from its histogram in the following way: mj=n∑i=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: m′j=pj0+pj1, j=0,1,2,3 And preserving the first three moments in g, means the equalities: m′j=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
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 related example
Access to parameter description
For an introduction:
- section Image Segmentation
- section Binarization
- C0=[I1,T] represents the background pixels for which the output intensity is set to 0. Intensities lower than I1 are also set to 0.
- C1=[T+1,I2] represents the object or foreground pixels for which the output intensity is set to 1. Intensities greater than I2 are also set to 1.
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=−n∑i=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]=−t∑I1p1[i]×log(p1[i])2 Hb[t]=−I2∑t+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])
![]() |
![]() |
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])
![]() |
![]() |
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=n∑i=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: m′j=pj0+pj1, j=0,1,2,3 And preserving the first three moments in g, means the equalities: m′j=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.
![]() |
![]() |
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.
![]() |
![]() |
References:
- [1] T. Pun, "Entropic thresholding: A new approach". Computer Graphics and Image Processing, vol. 16, pp. 210-239, 1981.
- [2] J. N. Kapur, P. K. Sahoo, and A. K. C. Wong, "A New Method for Gray-Level Picture Thresholding Using the Entropy of the Histogram". Computer Vision, Graphics and Image Processing , vol. 29, pp. 273-285, Mar. 1985.
- [3] N. Otsu "A Threshold Selection Method from Gray-Level Histograms". IEEE Transactions on Systems, Man, and Cybernetics, vol. 9, no 1, pp. 62-66, Jan. 1979.
- [4] W. H. Tsai. "Moment-Preserving Thresholding: A New Approach". Computer Vision, Graphics, and Image Processing, vol. 29, pp. 377-393, 1985.
- [5] T. W. Ridler, S. Calvard. "Picture Thresholding Using an Iterative Selection Method". IEEE Transactions on Systems, Man, and Cybernetics, vol. 8, no 8, pp. 630-632, Aug. 1978.
- Thresholding
- AutoThresholdingDark
- AutoThresholdingValue
- AutoSegmentation3Phases
- AutoIntensityClassification
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 | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
inputGrayImage |
The input grayscale image. | Image | Grayscale | nullptr | ||||||||
![]() |
rangeMode |
The way to determine the input intensity range.
|
Enumeration | MIN_MAX | |||||||||
![]() |
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} | ||||||||
![]() |
thresholdCriterion |
The criterion to compute the threshold from the histogram.
|
Enumeration | ENTROPY | |||||||||
![]() |
outputBinaryImage |
The output binary image. Its dimensions are forced to the same values as the input. | Image | nullptr | |||||||||
![]() |
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 ) ;