ImageBinarization.jl Documentation
Functions
Adaptive Threshold
ImageBinarization.binarize
— Method.binarize(AdaptiveThreshold(; percentage = 15, window_size = 32), img)
Uses a binarization threshold that varies across the image according to background illumination.
Output
Returns the binarized image as an Array{Gray{Bool},2}
.
Details
If the value of a pixel is $t$ percent less than the average of an $s \times s$ window of pixels centered around the pixel, then the pixel is set to black, otherwise it is set to white.
A computationally efficient method for computing the average of an $s \times s$ neighbourhood is achieved by using an integral image.
This algorithm works particularly well on images that have distinct contrast between background and foreground. See [1] for more details.
Options
Various options for the parameters of this function are described in more detail below.
Choices for percentage
You can specify an integer for the percentage
(denoted by $t$ in the publication) which must be between 0 and 100. If left unspecified a default value of 15 is utilised.
Choices for window_size
The argument window_size
(denoted by $s$ in the publication) specifies the size of pixel's square neighbourhood which must be greater than zero. A recommended size is the integer value which is closest to 1/8 of the average of the width and height. You can use the convenience function recommend_size(::AbstractArray{T,2})
to obtain this suggested value. If left unspecified, a default value of 32 is utilised.
#Example
using TestImages
img = testimage("cameraman")
s = recommend_size(img)
binarize(AdaptiveThreshold(percentage = 15, window_size = s), img)
References
- Bradley, D. (2007). Adaptive Thresholding using Integral Image. Journal of Graphic Tools, 12(2), pp.13-21. doi:10.1080/2151237x.2007.10129236
Balanced
ImageBinarization.binarize
— Method.binarize(Balanced(), img)
Binarizes the image using the balanced histogram thresholding method.
Output
Returns the binarized image as an Array{Gray{Bool},2}
.
Details
In balanced histogram thresholding, one interprets a bin as a physical weight with a mass equal to its occupancy count. The balanced histogram method involves iterating the following three steps: (1) choose the midpoint bin index as a "pivot", (2) compute the combined weight to the left and right of the pivot bin and (3) remove the leftmost bin if the left side is the heaviest, and the rightmost bin otherwise. The algorithm stops when only a single bin remains. The last bin determines the sought-after threshold with which the image is binarized.
Let $f_n$ ($n = 1 \ldots N$) denote the number of observations in the $n$th bin of the image histogram. The balanced histogram method constructs a sequence of nested intervals
where for $k = 2 \ldots N-1$
and $I_m = \lfloor \frac{1}{2}\left( \min \left( I_{k-1} \right) + \max \left( I_{k-1} \right) \right) \rfloor$. The final interval $I_{N-1}$ consists of a single element which is the bin index corresponding to the desired threshold.
If one interprets a bin as a physical weight with a mass equal to its occupancy count, then each step of the algorithm can be conceptualised as removing the leftmost or rightmost bin to "balance" the resulting histogram on a pivot. The pivot is defined to be the midpoint between the start and end points of the interval under consideration.
If it turns out that the single element in $I_{N-1}$ equals $1$ or $N$ then the original histogram must have a single peak and the algorithm has failed to find a suitable threshold. In this case the algorithm will fall back to using the UnimodalRosin
method to select the threshold.
Arguments
The function argument is described in more detail below.
img
An AbstractArray
representing an image. The image is automatically converted to Gray
in order to construct the requisite graylevel histogram.
Example
Binarize the "cameraman" image in the TestImages
package.
using TestImages, ImageBinarization
img = testimage("cameraman")
img_binary = binarize(Balanced(), img)
Reference
- “BI-LEVEL IMAGE THRESHOLDING - A Fast Method”, Proceedings of the First International Conference on Bio-inspired Systems and Signal Processing, 2008. Available: 10.5220/0001064300700076
Entropy
ImageBinarization.binarize
— Method.binarize(Entropy(), img)
An algorithm for finding the binarization threshold value using the entropy of the image histogram.
Output
Returns the binarized image as an Array{Gray{Bool},2}
.
Details
This algorithm uses the entropy of a one-dimensional histogram to produce a threshold value.
Let $f_1, f_2, \ldots, f_I$ be the frequencies in the various bins of the histogram and $I$ the number of bins. With $N = \sum_{i=1}^{I}f_i$, let $p_i = \frac{f_i}{N}$ ($i = 1, \ldots, I$) denote the probability distribution of gray levels. From this distribution one derives two additional distributions. The first defined for discrete values $1$ to $s$ and the other, from $s+1$ to $I$. These distributions are
The entropies associated with each distribution are as follows:
Combining these two entropy functions we have
Finding the discrete value $s$ which maximises the function $\psi(s)$ produces the sought-after threshold value (i.e. the bin which determines the threshold).
See Section 4 of [1] for more details on the derivation of the entropy.
Arguments
The function argument is described in more detail below.
img
An AbstractArray
representing an image. The image is automatically converted to Gray
in order to construct the requisite graylevel histogram.
Example
Binarize the "cameraman" image in the TestImages
package.
using TestImages, ImageBinarization
img = testimage("cameraman")
img_binary = binarize(Entropy(), img)
References
- 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, no. 1, p. 140, Jan. 1985.doi:10.1016/s0734-189x(85)90156-2
Intermodes
ImageBinarization.binarize
— Method.binarize(Intermodes(), img)
Under the assumption that the image histogram is bimodal the image histogram is smoothed using a length-3 mean filter until two modes remain. The binarization threshold is then set to the average value of the two modes.
Output
Returns the binarized image as an Array{Gray{Bool},2}
.
Arguments
The function argument is described in more detail below.
img
An AbstractArray
representing an image. The image is automatically converted to Gray
in order to construct the requisite graylevel histogram.
Example
Binarize the "cameraman" image in the TestImages
package.
using TestImages, ImageBinarization
img = testimage("cameraman")
img_binary = binarize(Intermodes(), img)
Reference
- C. A. Glasbey, “An Analysis of Histogram-Based Thresholding Algorithms,” CVGIP: Graphical Models and Image Processing, vol. 55, no. 6, pp. 532–537, Nov. 1993. doi:10.1006/cgip.1993.1040
Minimum Error
ImageBinarization.binarize
— Method.binarize(MinimumError(), img)
Under the assumption that the image histogram is a mixture of two Gaussian distributions the binarization threshold is chosen such that the expected misclassification error rate is minimised.
Output
Returns the binarized image as an Array{Gray{Bool},2}
.
Details
Let $f_i$ $(i=1 \ldots I)$ denote the number of observations in the $i$th bin of the histogram. Then the probability that an observation belongs to the $i$th bin is given by $p_i = \frac{f_i}{N}$ ($i = 1, \ldots, I$), where $N = \sum_{i=1}^{I}f_i$.
The minimum error thresholding method assumes that one can find a threshold $T$ which partitions the data into two categories, $C_0$ and $C_1$, such that the data can be modelled by a mixture of two Gaussian distribution. Let
denote the cumulative probabilities,
denote the means, and
denote the variances of categories $C_0$ and $C_1$, respectively.
Kittler and Illingworth proposed to use the minimum error criterion function
to assess the discreprancy between the mixture of Gaussians implied by a particular threshold $T$, and the piecewise-constant probability density function represented by the histogram. The discrete value $T$ which minimizes the function $J(T)$ produces the sought-after threshold value (i.e. the bin which determines the threshold).
Arguments
The function argument is described in more detail below.
img
An AbstractArray
representing an image. The image is automatically converted to Gray
in order to construct the requisite graylevel histogram.
Example
Binarize the "cameraman" image in the TestImages
package.
using TestImages, ImageBinarization
img = testimage("cameraman")
img_binary = binarize(MinimumError(), img)
References
- J. Kittler and J. Illingworth, “Minimum error thresholding,” Pattern Recognition, vol. 19, no. 1, pp. 41–47, Jan. 1986. doi:10.1016/0031-3203(86)90030-0
- Q.-Z. Ye and P.-E. Danielsson, “On minimum error thresholding and its implementations,” Pattern Recognition Letters, vol. 7, no. 4, pp. 201–206, Apr. 1988. doi:10.1016/0167-8655(88)90103-1
Minimum Intermodes
ImageBinarization.binarize
— Method.binarize(MinimumIntermodes(), img)
Under the assumption that the image histogram is bimodal the histogram is smoothed using a length-3 mean filter until two modes remain. The binarization threshold is then set to the minimum value between the two modes.
Output
Returns the binarized image as an Array{Gray{Bool},2}
.
Arguments
The function argument is described in more detail below.
img
An AbstractArray
representing an image. The image is automatically converted to Gray
in order to construct the requisite graylevel histogram.
Example
Binarize the "cameraman" image in the TestImages
package.
using TestImages, ImageBinarization
img = testimage("cameraman")
img_binary = binarize(MinimumIntermodes(), img)
Reference
- C. A. Glasbey, “An Analysis of Histogram-Based Thresholding Algorithms,” CVGIP: Graphical Models and Image Processing, vol. 55, no. 6, pp. 532–537, Nov. 1993. doi:10.1006/cgip.1993.1040
- J. M. S. Prewitt and M. L. Mendelsohn, “THE ANALYSIS OF CELL IMAGES,” *Annals of the New York Academy of Sciences, vol. 128, no. 3, pp. 1035–1053, Dec. 2006. doi:10.1111/j.1749-6632.1965.tb11715.x
Moments
ImageBinarization.binarize
— Method.binarize(Moments(), img)
The following rule determines the binarization threshold: if one assigns all observations below the threshold to a value z₀ and all observations above the threshold to a value z₁, then the first three moments of the original histogram must match the moments of this specially constructed bilevel histogram.
Output
Returns the binarized image as an Array{Gray{Bool},2}
.
Details
Let $f_i$ $(i=1 \ldots I)$ denote the number of observations in the $i$th bin of the histogram and $z_i$ $(i=1 \ldots I)$ the observed value associated with the $i$th bin. Then the probability that an observation $z_i$ belongs to the $i$th bin is given by $p_i = \frac{f_i}{N}$ ($i = 1, \ldots, I$), where $N = \sum_{i=1}^{I}f_i$.
Moments can be computed from the histogram $f$ in the following way:
The principle of moment-preserving thresholding is to select a threshold value, as well as two representative values $z_0$ and $z_1$ ($z_0 < z_1$), such that if all below-threshold values in $f$ are replaced by $z_0$ and all above-threshold values are replaced by $z_1$, then this specially constructed bilevel histogram $g$ will have the same first three moments as $f$.
Concretely, let $q_0$ and $q_1$ denote the fractions of observations below and above the threshold in $f$, respectively. The constraint that the first three moments in $g$ must equal the first three moments in $f$ can be expressed by the following system of four equations
where the left-hand side represents the moments of $g$ and the right-hand side represents the moments of $f$. To find the desired treshold value, one first solves the four equations to obtain $q_0$ and $q_1$, and then chooses the threshold $t$ such that $q_0 = \sum_{z_i \le t} p_i$.
Arguments
The function argument is described in more detail below.
img
An AbstractArray
representing an image. The image is automatically converted to Gray
in order to construct the requisite graylevel histogram.
Example
Binarize the "cameraman" image in the TestImages
package.
using TestImages, ImageBinarization
img = testimage("cameraman")
img_binary = binarize(Moments(), img)
Reference
[1] W.-H. Tsai, “Moment-preserving thresolding: A new approach,” Computer Vision, Graphics, and Image Processing, vol. 29, no. 3, pp. 377–393, Mar. 1985. doi:10.1016/0734-189x(85)90133-1
Niblack
ImageBinarization.binarize
— Method.binarize(Niblack(; window_size = 7, bias = 0.2), img)
Applies Niblack adaptive thresholding [1] under the assumption that the input image is textual.
Output
Returns the binarized image as an Array{Gray{Bool},2}
.
Details
The input image is binarized by varying the threshold across the image, using a modified version of Niblack's algorithm [2]. A threshold $T$ is defined for each pixel based on the mean $m$ and standard deviation $s$ of the intensities of neighboring pixels in a window around it. This threshold is given by
where $k$ is a user-defined parameter weighting the influence of the standard deviation on the value of $T$.
Note that Niblack's algorithm is highly sensitive to variations in the gray values of background pixels, which often exceed local thresholds and appear as artifacts in the binarized image. The Sauvola algorithm included in this package implements an attempt to address this issue [2].
Arguments
img
An image which is binarized according to a per-pixel adaptive threshold into background (0) and foreground (1) pixel values.
window_size
(denoted by $w$ in the publication)
The threshold for each pixel is a function of the distribution of the intensities of all neighboring pixels in a square window around it. The side length of this window is $2w + 1$, with the target pixel in the center position.
bias
(denoted by $k$ in the publication)
A user-defined biasing parameter. This can take negative values.
Example
Binarize the "cameraman" image in the TestImages
package.
using TestImages, ImageBinarization
img = testimage("cameraman")
img_binary = binarize(Niblack(window_size = 9, bias = 0.2), img)
References
- Wayne Niblack (1986). An Introduction to Image Processing. Prentice-Hall, Englewood Cliffs, NJ: 115-16.
- J. Sauvola and M. Pietikäinen (2000). "Adaptive document image binarization". Pattern Recognition 33 (2): 225-236. doi:10.1016/S0031-3203(99)00055-2
Otsu
ImageBinarization.binarize
— Method.binarize(Otsu(), img)
Under the assumption that the image histogram is bimodal the binarization threshold is set so that the resultant between-class variance is maximal.
Output
Returns the binarized image as an Array{Gray{Bool},2}
.
Details
Let $f_i$ $(i=1 \ldots I)$ denote the number of observations in the $i$th bin of the image histogram. Then the probability that an observation belongs to the $i$th bin is given by $p_i = \frac{f_i}{N}$ ($i = 1, \ldots, I$), where $N = \sum_{i=1}^{I}f_i$.
The choice of a threshold $T$ partitions the data into two categories, $C_0$ and $C_1$. Let
denote the cumulative probabilities,
denote the means, and
denote the variances of categories $C_0$ and $C_1$, respectively. Furthermore, let
represent the overall mean,
the between-category variance, and
the within-category variance, respectively.
Finding the discrete value $T$ which maximises the function $\sigma_b^2(T)$ produces the sought-after threshold value (i.e. the bin which determines the threshold). As it turns out, that threshold value is equal to the threshold decided by minimizing the within-category variances criterion $\sigma_w^2(T)$. Furthermore, that threshold is also the same as the threshold calculated by maximizing the ratio of between-category variance to within-category variance.
Arguments
The function argument is described in more detail below.
img
An AbstractArray
representing an image. The image is automatically converted to Gray
in order to construct the requisite graylevel histogram.
Example
Binarize the "cameraman" image in the TestImages
package.
using TestImages, ImageBinarization
img = testimage("cameraman")
img_binary = binarize(Otsu(), img)
Reference
- Nobuyuki Otsu (1979). “A threshold selection method from gray-level histograms”. IEEE Trans. Sys., Man., Cyber. 9 (1): 62–66. doi:10.1109/TSMC.1979.4310076
Polysegment
ImageBinarization.binarize
— Method.binarize(Polysegment(), img)
Uses the polynomial segmentation technique to group the image pixels into two categories (foreground and background).
Details
The approach involves constructing a univariate second-degree polynomial such that the two roots of the polynomial represent the graylevels of two cluster centers (i.e the foreground and background). Pixels are then assigned to the foreground or background depending on which cluster center is closest.
Arguments
The function argument is described in more detail below.
img
An AbstractArray
representing an image. The image is automatically converted to Gray
.
Example
Binarize the "cameraman" image in the TestImages
package.
using TestImages, ImageBinarization
img = testimage("cameraman")
img_binary = binarize(Polysegment(), img)
Reference
- R. E. Vidal, "Generalized Principal Component Analysis (GPCA): An Algebraic Geometric Approach to Subspace Clustering and Motion Segmentation." Order No. 3121739, University of California, Berkeley, Ann Arbor, 2003.
Sauvola
ImageBinarization.binarize
— Method.binarize(Sauvola(; window_size = 7, bias = 0.2), img)
Applies Sauvola–Pietikäinen adaptive image binarization [1] under the assumption that the input image is textual.
Output
Returns the binarized image as an Array{Gray{Bool},2}
.
Details
The input image is binarized by varying the threshold across the image, using a modified version of Niblack's algorithm [2]. Niblack's approach was to define a threshold $T$ for each pixel based on the mean $m$ and standard deviation $s$ of the intensities of neighboring pixels in a window around it, given by
where $k$ is a user-defined parameter weighting the influence of the standard deviation on the value of $T$.
Niblack's algorithm is highly sensitive to variations in the gray values of background pixels, which often exceed local thresholds and appear as artifacts in the binarized image. Sauvola and Pietikäinen [1] introduce the dynamic range $R$ of the standard deviation (i.e. its maximum possible value in the color space), such that the threshold is given by
This adaptively amplifies the contribution made by the standard deviation to the value of $T$.
The Sauvola–Pietikäinen algorithm is implemented here using an optimization proposed by Shafait, Keysers and Breuel [3], in which integral images are used to calculate the values of $m$ and $s$ for each pixel in constant time. Since each of these data structures can be computed in a single pass over the source image, runtime is significantly improved.
Arguments
img
An image which is binarized according to a per-pixel adaptive threshold into background (0) and foreground (1) pixel values.
window_size
(denoted by $w$ in the publication)
The threshold for each pixel is a function of the distribution of the intensities of all neighboring pixels in a square window around it. The side length of this window is $2w + 1$, with the target pixel in the center position.
bias
(denoted by $k$ in the publication)
A user-defined biasing parameter. This can take negative values, though values in the range [0.2, 0.5] are typical.
Example
Binarize the "cameraman" image in the TestImages
package.
using TestImages, ImageBinarization
img = testimage("cameraman")
img_binary = binarize(Sauvola(window_size = 9, bias = 0.2), img)
References
- J. Sauvola and M. Pietikäinen (2000). "Adaptive document image binarization". Pattern Recognition 33 (2): 225-236. doi:10.1016/S0031-3203(99)00055-2
- Wayne Niblack (1986). An Introduction to Image Processing. Prentice-Hall, Englewood Cliffs, NJ: 115-16.
- Faisal Shafait, Daniel Keysers and Thomas M. Breuel (2008). "Efficient implementation of local adaptive thresholding techniques using integral images". Proc. SPIE 6815, Document Recognition and Retrieval XV, 681510 (28 January 2008). doi:10.1117/12.767755
Unimodal Rosin
ImageBinarization.binarize
— Method.binarize(UnimodalRosin(), img)
Uses Rosin's Unimodal threshold algorithm to binarize the image.
Output
Returns the binarized image as an Array{Gray{Bool},2}
.
Details
This algorithm first selects the bin in the image histogram with the highest frequency. The algorithm then searches from the location of the maximum bin to the last bin of the histogram for the first bin with a frequency of 0 (known as the minimum bin.). A line is then drawn that passes through both the maximum and minimum bins. The bin with the greatest orthogonal distance to the line is chosen as the threshold value.
Assumptions
This algorithm assumes that:
- The histogram is unimodal.
- There is always at least one bin that has a frequency of 0. If not, the algorithm will use the last bin as the minimum bin.
If the histogram includes multiple bins with a frequency of 0, the algorithm will select the first zero bin as its minimum. If there are multiple bins with the greatest orthogonal distance, the leftmost bin is selected as the threshold.
Arguments
The function argument is described in more detail below.
img
An AbstractArray
representing an image. The image is automatically converted to Gray
in order to construct the requisite graylevel histogram.
Example
Compute the threshold for the "moonsurface" image in the TestImages
package.
using TestImages, ImageBinarization
img = testimage("moonsurface")
img_binary = binarize(UnimodalRosin(), img)
Reference
- P. L. Rosin, “Unimodal thresholding,” Pattern Recognition, vol. 34, no. 11, pp. 2083–2096, Nov. 2001.doi:10.1016/s0031-3203(00)00136-9
Yen
ImageBinarization.binarize
— Method.binarize(Yen(), img)
Computes the binarization threshold value using Yen's maximum correlation criterion for bilevel thresholding.
Output
Returns the binarized image as an Array{Gray{Bool},2}
.
Details
This algorithm uses the concept of entropic correlation of a gray level histogram to produce a threshold value.
Let $f_1, f_2, \ldots, f_I$ be the frequencies in the various bins of the histogram and $I$ the number of bins. With $N = \sum_{i=1}^{I}f_i$, let $p_i = \frac{f_i}{N}$ ($i = 1, \ldots, I$) denote the probability distribution of gray levels. From this distribution one derives two additional distributions. The first defined for discrete values $1$ to $s$ and the other, from $s+1$ to $I$. These distributions are
The entropic correlations associated with each distribution are
Combining these two entropic correlation functions we have
Finding the discrete value $s$ which maximises the function $\psi(s)$ produces the sought-after threshold value (i.e. the bin which determines the threshold).
Arguments
The function argument is described in more detail below.
img
An AbstractArray
representing an image. The image is automatically converted to Gray
in order to construct the requisite graylevel histogram.
Example
Binarize the "cameraman" image in the TestImages
package.
using TestImages, ImageBinarization
img = testimage("cameraman")
img_binary = binarize(Yen(), img)
Reference
- Yen JC, Chang FJ, Chang S (1995), “A New Criterion for Automatic Multilevel Thresholding”, IEEE Trans. on Image Processing 4 (3): 370-378, doi:10.1109/83.366472