User:Wyc25013/sandbox

In computer vision and image processing, Otsu's method, named after Nobuyuki Otsu (大津展之), is used to automatically perform clustering-based image thresholding, or, the reduction of a graylevel image to a binary image. The algorithm assumes that the image contains two classes of pixels following bi-modal histogram (foreground pixels and background pixels), it then calculates the optimum threshold separating the two classes so that their combined spread (intra-class variance) is minimal, or equivalently (because the sum of pairwise squared distances is constant), so that their inter-class variance is maximal. Consequently, Otsu's method is roughly a one-dimensional, discrete analog of Fisher's Discriminant Analysis.

The extension of the original method to multi-level thresholding is referred to as the Multi Otsu method.

Method
In Otsu's method we exhaustively search for the threshold that minimizes the intra-class variance (the variance within the class), defined as a weighted sum of variances of the two classes:


 * $$\sigma^2_w(t)=\omega_0(t)\sigma^2_0(t)+\omega_1(t)\sigma^2_1(t)$$

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

The class probability $$\omega_{0,1}(t)$$ is computed from the $$L$$ histograms:


 * $$\omega_0(t)=\sum_{i=0}^{t-1} p(i)$$
 * $$\omega_1(t)=\sum_{i=t}^{L-1} p(i)$$

Otsu shows that minimizing the intra-class variance is the same as maximizing inter-class variance:


 * $$\sigma^2_b(t)=\sigma^2-\sigma^2_w(t)=\omega_0(\mu_0-\mu_T)^2+\omega_1(\mu_1-\mu_T)^2=\omega_0(t)\omega_1(t)\left[\mu_0(t)-\mu_1(t)\right]^2$$

which is expressed in terms of class probabilities $$\omega$$ and class means $$\mu$$.

while the class mean $$\mu_{0,1,T}(t)$$ is:


 * $$\mu_0(t)=\sum_{i=0}^{t-1} ip(i)/\omega_0$$
 * $$\mu_1(t)=\sum_{i=t}^{L-1} ip(i)/\omega_1$$
 * $$\mu_T = \sum_{i=0}^{L-1} ip(i)$$

The following relations can be easily verified:


 * $$\omega_0\mu_0+\omega_1\mu_1= \mu_T$$
 * $$\omega_0+\omega_1=1$$

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 $$\omega_i(0)$$ and $$\mu_i(0)$$
 * 3) Step through all possible thresholds $$t = 1 \ldots $$ maximum intensity
 * 4) Update $$\omega_i$$ and $$\mu_i$$
 * 5) Compute $$\sigma^2_b(t)$$
 * 6) Desired threshold corresponds to the maximum $$\sigma^2_b(t)$$

MATLAB implementation
total is the number of pixels in the given image. 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).

Notice that in Matlab we don't need to implement Otsu's method ourselves because Matlab have built-in functions  and   in Image Processing Toolbox which are implemented with Otsu's method and Multi Otsu's method, respectively.

Another approach with vectorized method (could be easily converted into python matrix-array version for GPU processing) The implementation has a little redundancy of computation. But since Otsu method is fast, the implementation is acceptable and easy to understand. While in some environment, since we employ vectorisation form, loop computation might be faster. This method can be easily converted to multi-threshould method, with architecture minimum heap—children labels.

Limitations
Otsu’s method exhibits the relatively good performance if the histogram can be assumed to have bimodal distribution and assumed to possess deep a and sharp valley between two peaks. But if the object area is small compared with the background area, the histogram no longer exhibits bimodality. And if the variances of the object and the background intensities are large compared to the mean difference, or the image is severely corrupted by additive noise, the sharp valley of the gray level histogram is degraded. Then the possibly incorrect threshold determined by Otsu’s method results in the segmentation error. (Here we define the object size to be the ratio of the object area to the entire image area and the mean difference to be the difference of the average intensities of the object and the background)

From the experimental results, the performance of global thresholding techniques including Otsu’s method is shown to be limited by the small object size, the small mean difference, the large variances of the object and the background intensities, the large amount of noise added, and so on.

Improvements
There are many improvements focusing on different limitations for Otsu's method. One famous and effective way is known as two-dimensional Otsu's method. In this approach, the gray-level value of each pixel as well as the average value of its immediate neighborhood is studied so that the binarization results are greatly improved, especially for those image corrupted by noise.

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


 * $$P_{ij} = f_{ij} / N, \sum_{i=0}^{L-1}\sum_{j=0}^{L-1}P_{ij}=1$$

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

The probabilities of two classes can be denoted as:
 * $$\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}$$

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


 * $$\mu_0=[\mu_{0i}, \mu_{0j}]^T = \left[\sum_{i=0}^{s-1}\sum_{j=0}^{t-1}iP_{ij}/\omega_0,\sum_{i=0}^{s-1}\sum_{j=0}^{t-1}jP_{ij}/\omega_0\right]^T$$


 * $$\mu_1=[\mu_{1i}, \mu_{1j}]^T = \left[\sum_{i=s}^{L-1}\sum_{j=t}^{L-1}iP_{ij}/\omega_1,\sum_{i=s}^{L-1}\sum_{j=t}^{L-1}jP_{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$$

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


 * $$\omega_0+\omega_1 \cong 1$$
 * $$\omega_0\mu_0+\omega_1\mu_1 \cong \mu_T$$

The inter-class discrete matrix is defined as


 * $$S_b = \sum_{k=0}^1\omega_k[(\mu_k-\mu_T)(\mu_k-\mu_T)^T]$$

The trace of discrete matrix could be expressed as


 * $$tr(S_b) = \omega_0[(\mu_{0i}-\mu_{Ti})^2+(\mu_{0j}-\mu_{Tj})^2]+\omega_1[(\mu_{1i}-\mu_{Ti})^2+(\mu_{1j}-\mu_{Tj})^2] = \frac{(\mu_{Ti}\omega_0-\mu_i)^2 + (\mu_{Tj}\omega_0-\mu_j)^2}{\omega_0(1-\omega_0)}$$

where


 * $$\mu_i = \sum_{i=0}^s\sum_{j=0}^tiP_{ij}$$
 * $$\mu_j = \sum_{i=0}^s\sum_{j=0}^tjP_{ij}$$

Similar to one-dimensional Otsu's method, the optimal threshold $$(s,t)$$ is obtained by maximizing $$tr(S_b)$$.

Algorithm
The $$s$$ and $$t$$ is obtained iteratively which is similar with one-dimensional Otsu's method. The values of $$s$$ and $$t$$ are changed till we obtain the maximum of $$tr(S_b)$$, that is

Notice that for evaluating $$tr(S_b)$$, we can use a fast recursive dynamic programming algorithm to improve time performace. However, even with the dynamic programming approach, 2d Otsu's method still has large time complexity. Therefore, many researches have been done to reduce the computation cost.

Matlab Implementation
function inputs and output:

hists is a $$256\times 256$$ 2D-histogram of grayscale value and neighborhood average grayscale value pair.

total is the number of pairs in the given image.

threshold is the threshold obtained.