Aller au contenu principal

Otsu's method


Otsu's method


In computer vision and image processing, Otsu's method, named after Nobuyuki Otsu (大津展之, Ōtsu Nobuyuki), is used to perform automatic image thresholding. In the simplest form, the algorithm returns a single intensity threshold that separate pixels into two classes, foreground and background. This threshold is determined by minimizing intra-class intensity variance, or equivalently, by maximizing inter-class variance. Otsu's method is a one-dimensional discrete analogue of Fisher's Discriminant Analysis, is related to Jenks optimization method, and is equivalent to a globally optimal k-means performed on the intensity histogram. The extension to multi-level thresholding was described in the original paper, and computationally efficient implementations have since been proposed.

Otsu's method

The algorithm exhaustively searches for the threshold that minimizes the intra-class variance, defined as a weighted sum of variances of the two classes:

σ w 2 ( t ) = ω 0 ( t ) σ 0 2 ( t ) + ω 1 ( t ) σ 1 2 ( t ) {\displaystyle \sigma _{w}^{2}(t)=\omega _{0}(t)\sigma _{0}^{2}(t)+\omega _{1}(t)\sigma _{1}^{2}(t)}

Weights ω 0 {\displaystyle \omega _{0}} and ω 1 {\displaystyle \omega _{1}} are the probabilities of the two classes separated by a threshold t {\displaystyle t} ,and σ 0 2 {\displaystyle \sigma _{0}^{2}} and σ 1 2 {\displaystyle \sigma _{1}^{2}} are variances of these two classes.

The class probability ω { 0 , 1 } ( t ) {\displaystyle \omega _{\{0,1\}}(t)} is computed from the L {\displaystyle L} bins of the histogram:

ω 0 ( t ) = i = 0 t 1 p ( i ) ω 1 ( t ) = i = t L 1 p ( i ) {\displaystyle {\begin{aligned}\omega _{0}(t)&=\sum _{i=0}^{t-1}p(i)\\[4pt]\omega _{1}(t)&=\sum _{i=t}^{L-1}p(i)\end{aligned}}}

For 2 classes, minimizing the intra-class variance is equivalent to maximizing inter-class variance:

σ b 2 ( t ) = σ 2 σ w 2 ( t ) = ω 0 ( t ) ( μ 0 μ T ) 2 + ω 1 ( t ) ( μ 1 μ T ) 2 = ω 0 ( t ) ω 1 ( t ) [ μ 0 ( t ) μ 1 ( t ) ] 2 {\displaystyle {\begin{aligned}\sigma _{b}^{2}(t)&=\sigma ^{2}-\sigma _{w}^{2}(t)=\omega _{0}(t)(\mu _{0}-\mu _{T})^{2}+\omega _{1}(t)(\mu _{1}-\mu _{T})^{2}\\&=\omega _{0}(t)\omega _{1}(t)\left[\mu _{0}(t)-\mu _{1}(t)\right]^{2}\end{aligned}}}

which is expressed in terms of class probabilities ω {\displaystyle \omega } and class means μ {\displaystyle \mu } , where the class means μ 0 ( t ) {\displaystyle \mu _{0}(t)} , μ 1 ( t ) {\displaystyle \mu _{1}(t)} and μ T {\displaystyle \mu _{T}} are:

μ 0 ( t ) = i = 0 t 1 i p ( i ) ω 0 ( t ) μ 1 ( t ) = i = t L 1 i p ( i ) ω 1 ( t ) μ T = i = 0 L 1 i p ( i ) {\displaystyle {\begin{aligned}\mu _{0}(t)&={\frac {\sum _{i=0}^{t-1}ip(i)}{\omega _{0}(t)}}\\[4pt]\mu _{1}(t)&={\frac {\sum _{i=t}^{L-1}ip(i)}{\omega _{1}(t)}}\\\mu _{T}&=\sum _{i=0}^{L-1}ip(i)\end{aligned}}}

The following relations can be easily verified:

ω 0 μ 0 + ω 1 μ 1 = μ T ω 0 + ω 1 = 1 {\displaystyle {\begin{aligned}\omega _{0}\mu _{0}+\omega _{1}\mu _{1}&=\mu _{T}\\\omega _{0}+\omega _{1}&=1\end{aligned}}}

The class probabilities and class means can be computed iteratively. This idea yields an effective algorithm.

Algorithm

  1. Compute histogram and probabilities of each intensity level
  2. Set up initial ω i ( 0 ) {\displaystyle \omega _{i}(0)} and μ i ( 0 ) {\displaystyle \mu _{i}(0)}
  3. Step through all possible thresholds t = 1 , {\displaystyle t=1,\ldots } maximum intensity
    1. Update ω i {\displaystyle \omega _{i}} and μ i {\displaystyle \mu _{i}}
    2. Compute σ b 2 ( t ) {\displaystyle \sigma _{b}^{2}(t)}
  4. Desired threshold corresponds to the maximum σ b 2 ( t ) {\displaystyle \sigma _{b}^{2}(t)}

MATLAB implementation

histogramCounts is a 256-element histogram of a grayscale image different gray-levels (typical for 8-bit images). level is the threshold for the image (double).

Matlab has built-in functions graythresh() and multithresh() in the Image Processing Toolbox which are implemented with Otsu's method and Multi Otsu's method, respectively.

Python implementation

This implementation requires the NumPy library.

Python libraries dedicated to image processing such as OpenCV and Scikit-image propose built-in implementations of the algorithm.

Limitations and variations

Otsu's method performs well when the histogram has a bimodal distribution with a deep and sharp valley between the two peaks.

Like all other global thresholding methods, Otsu's method performs badly in case of heavy noise, small objects size, inhomogeneous lighting and larger intra-class than inter-class variance. In those cases, local adaptations of the Otsu method have been developed.

Moreover, the mathematical grounding of Otsu's method models the histogram of the image as a mixture of two Normal distributions with equal variance and equal size. Otsu's thresholding may however yield satisfying results even when these assumptions are not met, in the same way statistical tests (to which Otsu's method is heavily connected) can perform correctly even when the working assumptions are not fully satisfied.

Several variations of Otsu's methods have been proposed to account for more severe deviations from these assumptions, such as the Kittler-Illingworth method.

A variation for noisy images

A popular local adaptation is the two-dimensional Otsu's method, which performs better for the object segmentation task in noisy images. Here, the intensity value of a given pixel is compared with the average intensity of its immediate neighborhood to improve segmentation results.

At each pixel, the average gray-level value of the neighborhood is calculated. Let the gray level of the given pixel be divided into L {\displaystyle L} discrete values and the average gray level is also divided into the same L {\displaystyle L} values. Then a pair is formed: the pixel gray level and the average of the neighborhood ( i , j ) {\displaystyle (i,j)} . Each pair belongs to one of the L × L {\displaystyle L\times L} possible 2-dimensional bins. The total number of occurrences (frequency), f i j {\displaystyle f_{ij}} , of a pair ( i , j ) {\displaystyle (i,j)} , divided by the total number of pixels in the image N {\displaystyle N} , defines the joint probability mass function in a 2-dimensional histogram:

P i j = f i j N , i = 0 L 1 j = 0 L 1 P i j = 1 {\displaystyle P_{ij}={\frac {f_{ij}}{N}},\qquad \sum _{i=0}^{L-1}\sum _{j=0}^{L-1}P_{ij}=1}

And the 2-dimensional Otsu's method is developed based on the 2-dimensional histogram as follows.

The probabilities of two classes can be denoted as:

ω 0 = i = 0 s 1 j = 0 t 1 P i j ω 1 = i = s L 1 j = t L 1 P i j {\displaystyle {\begin{aligned}\omega _{0}&=\sum _{i=0}^{s-1}\sum _{j=0}^{t-1}P_{ij}\\\omega _{1}&=\sum _{i=s}^{L-1}\sum _{j=t}^{L-1}P_{ij}\end{aligned}}}

The intensity mean value vectors of two classes and total mean vector can be expressed as follows:

μ 0 = [ μ 0 i , μ 0 j ] T = [ i = 0 s 1 j = 0 t 1 i P i j ω 0 , i = 0 s 1 j = 0 t 1 j P i j ω 0 ] T μ 1 = [ μ 1 i , μ 1 j ] T = [ i = s L 1 j = t L 1 i P i j ω 1 , i = s L 1 j = t L 1 j P i j ω 1 ] T μ T = [ μ T i , μ T j ] T = [ i = 0 L 1 j = 0 L 1 i P i j , i = 0 L 1 j = 0 L 1 j P i j ] T {\displaystyle {\begin{aligned}\mu _{0}&=[\mu _{0i},\mu _{0j}]^{T}=\left[\sum _{i=0}^{s-1}\sum _{j=0}^{t-1}i{\frac {P_{ij}}{\omega _{0}}},\sum _{i=0}^{s-1}\sum _{j=0}^{t-1}j{\frac {P_{ij}}{\omega _{0}}}\right]^{T}\\\mu _{1}&=[\mu _{1i},\mu _{1j}]^{T}=\left[\sum _{i=s}^{L-1}\sum _{j=t}^{L-1}i{\frac {P_{ij}}{\omega _{1}}},\sum _{i=s}^{L-1}\sum _{j=t}^{L-1}j{\frac {P_{ij}}{\omega _{1}}}\right]^{T}\\\mu _{T}&=[\mu _{Ti},\mu _{Tj}]^{T}=\left[\sum _{i=0}^{L-1}\sum _{j=0}^{L-1}iP_{ij},\sum _{i=0}^{L-1}\sum _{j=0}^{L-1}jP_{ij}\right]^{T}\end{aligned}}}

In most cases the probability off-diagonal will be negligible, so it is easy to verify:

ω 0 + ω 1 1 {\displaystyle \omega _{0}+\omega _{1}\cong 1}
ω 0 μ 0 + ω 1 μ 1 μ T {\displaystyle \omega _{0}\mu _{0}+\omega _{1}\mu _{1}\cong \mu _{T}}

The inter-class discrete matrix is defined as

S b = k = 0 1 ω k [ ( μ k μ T ) ( μ k μ T ) T ] {\displaystyle S_{b}=\sum _{k=0}^{1}\omega _{k}[(\mu _{k}-\mu _{T})(\mu _{k}-\mu _{T})^{T}]}

The trace of the discrete matrix can be expressed as

tr ( S b ) = ω 0 [ ( μ 0 i μ T i ) 2 + ( μ 0 j μ T j ) 2 ] + ω 1 [ ( μ 1 i μ T i ) 2 + ( μ 1 j μ T j ) 2 ] = ( μ T i ω 0 μ i ) 2 + ( μ T j ω 0 μ j ) 2 ω 0 ( 1 ω 0 ) {\displaystyle {\begin{aligned}&\operatorname {tr} (S_{b})\\[4pt]={}&\omega _{0}[(\mu _{0i}-\mu _{Ti})^{2}+(\mu _{0j}-\mu _{Tj})^{2}]+\omega _{1}[(\mu _{1i}-\mu _{Ti})^{2}+(\mu _{1j}-\mu _{Tj})^{2}]\\[4pt]={}&{\frac {(\mu _{Ti}\omega _{0}-\mu _{i})^{2}+(\mu _{Tj}\omega _{0}-\mu _{j})^{2}}{\omega _{0}(1-\omega _{0})}}\end{aligned}}}

where

μ i = i = 0 s 1 j = 0 t 1 i P i j {\displaystyle \mu _{i}=\sum _{i=0}^{s-1}\sum _{j=0}^{t-1}iP_{ij}}
μ j = i = 0 s 1 j = 0 t 1 j P i j {\displaystyle \mu _{j}=\sum _{i=0}^{s-1}\sum _{j=0}^{t-1}jP_{ij}}

Similar to one-dimensional Otsu's method, the optimal threshold ( s , t ) {\displaystyle (s,t)} is obtained by maximizing tr ( S b ) {\displaystyle \operatorname {tr} (S_{b})} .

Algorithm

The s {\displaystyle s} and t {\displaystyle t} is obtained iteratively which is similar with one-dimensional Otsu's method. The values of s {\displaystyle s} and t {\displaystyle t} are changed till we obtain the maximum of tr ( S b ) {\displaystyle \operatorname {tr} (S_{b})} , that is

Notice that for evaluating tr ( S b ) {\displaystyle \operatorname {tr} (S_{b})} , we can use a fast recursive dynamic programming algorithm to improve time performance. However, even with the dynamic programming approach, 2d Otsu's method still has large time complexity. Therefore, much research has been done to reduce the computation cost.

If summed area tables are used to build the 3 tables, sum over P i j {\displaystyle P_{ij}} , sum over i P i j {\displaystyle i*P_{ij}} , and sum over j P i j {\displaystyle j*P_{ij}} , then the runtime complexity is the maximum of (O(N_pixels), O(N_bins*N_bins)). Note that if only coarse resolution is needed in terms of threshold, N_bins can be reduced.

MATLAB implementation

function inputs and output:

hists is a 256 × 256 {\displaystyle 256\times 256} 2D-histogram of grayscale value and neighborhood average grayscale value pair.

total is the number of pairs in the given image.it is determined by the number of the bins of 2D-histogram at each direction.

threshold is the threshold obtained.

A variation for unbalanced images

When the levels of gray of the classes of the image can be considered as Normal distributions but with unequal size and/or unequal variances, assumptions for the Otsu algorithm are not met. The Kittler-Illingworth algorithm (also known as Minimum Error thresholding) is a variation of Otsu's method to handle such cases. There are several ways to mathematically describe this algorithm. One of them is to consider that for each threshold being tested, the parameters of the Normal distributions in the resulting binary image are estimated by Maximum likelihood estimation given the data.

While this algorithm could seem superior to Otsu's method, it introduces new parameters to be estimated, and this can result in the algorithm being over-parametrized and thus unstable. In many cases where the assumptions from Otsu's method seem at least partially valid, it may be preferable to favor Otsu's method over the Kittler-Illingworth algorithm, following Occam's razor.

Iterative Triclass Thresholding Based on the Otsu's Method

One limitation of the Otsu’s method is that it cannot segment weak objects as the method searches for a single threshold to separate an image into two classes, namely, foreground and background, in one shot. Because the Otsu’s method looks to segment an image with one threshold, it tends to bias toward the class with the large variance. Iterative triclass thresholding algorithm is a variation of the Otsu’s method to circumvent this limitation. Given an image, at the first iteration, the triclass thresholding algorithm calculates a threshold η 1 {\displaystyle \eta _{1}} using the Otsu’s method. Based on threshold η 1 {\displaystyle \eta _{1}} , the algorithm calculates mean μ u p p e r [ 1 ] {\displaystyle \mu _{upper}^{[1]}} of pixels above η 1 {\displaystyle \eta _{1}} and mean μ l o w e r [ 1 ] {\displaystyle \mu _{lower}^{[1]}} of pixels below η 1 {\displaystyle \eta _{1}} . Then the algorithm tentatively separates the image into three classes (hence the name triclass), with the pixels above the upper mean μ u p p e r [ 1 ] {\displaystyle \mu _{upper}^{[1]}} designated as the temporary foreground F {\displaystyle F} class and pixels below the lower mean μ l o w e r [ 1 ] {\displaystyle \mu _{lower}^{[1]}} designated as the temporary background B {\displaystyle B} class. Pixels fall between [ μ l o w e r [ 1 ] , μ u p p e r [ 1 ] ] {\displaystyle [\mu _{lower}^{[1]},\mu _{upper}^{[1]}]} are denoted as a to-be-determined (TBD) region. This completes the first iteration of the algorithm. For the second iteration, the Otsu’s method is applied to the TBD region only to obtain a new threshold η 2 {\displaystyle \eta _{2}} . The algorithm then calculates the mean μ u p p e r [ 2 ] {\displaystyle \mu _{upper}^{[2]}} of pixels in the TBD region that are above η 2 {\displaystyle \eta _{2}} and the mean μ l o w e r [ 2 ] {\displaystyle \mu _{lower}^{[2]}} of pixels in the TBD region that are below η 2 {\displaystyle \eta _{2}} . Pixels in the TBD region that are greater than the upper mean μ u p p e r [ 2 ] {\displaystyle \mu _{upper}^{[2]}} are added to the temporary foreground F {\displaystyle F} . And pixels in the TBD region that are less than the lower mean μ l o w e r [ 2 ] {\displaystyle \mu _{lower}^{[2]}} are added to the temporary background B {\displaystyle B} . Similarly, a new TBD region is obtained, which contains all the pixels falling between [ μ l o w e r [ 2 ] , μ u p p e r [ 2 ] ] {\displaystyle [\mu _{lower}^{[2]},\mu _{upper}^{[2]}]} . This completes the second iteration. The algorithm then proceeds to the next iteration to process the new TBD region until it meets the stopping criterion. The criterion is that, when the difference between Otsu’s thresholds computed from two consecutive iterations is less than a small number, the iteration shall stop. For the last iteration, pixels above η n {\displaystyle \eta _{n}} are assigned to the foreground class and pixels below the threshold are assigned to the background class. At the end, all the temporary foreground pixels are combined to constitute the final foreground. All the temporary background pixels are combined to become the final background. In implementation, the algorithm involves no parameter except for the stopping criterion in terminating the iterations. By iteratively applying the Otsu’s method and gradually shrinking the TBD region for segmentation, the algorithm can obtain a result that preserves weak objects better than the standard Otsu’s method does.

References

External links

  • Implementation of Otsu's thresholding method as GIMP-plugin using Script-Fu (a Scheme-based language)
  • Lecture notes on thresholding – covers the Otsu method
  • A plugin for ImageJ using Otsu's method to do the threshold
  • A full explanation of Otsu's method with a working example and Java implementation
  • Implementation of Otsu's method in ITK
  • Otsu Thresholding in C# – a straightforward C# implementation with explanation
  • Otsu's method using MATLAB
  • Otsu Thresholding with scikit-image in Python


Text submitted to CC-BY-SA license. Source: Otsu's method by Wikipedia (Historical)


INVESTIGATION