HOME

TheInfoList



OR:

In computer vision and image processing, Otsu's method, named after , 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 ''k''-means clustering is a method of vector quantization, originally from signal processing, that aims to partition ''n'' observations into ''k'' clusters in which each observation belongs to the cluster with the nearest mean (cluster centers o ...
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: :\sigma^2_w(t)=\omega_0(t)\sigma^2_0(t)+\omega_1(t)\sigma^2_1(t) Weights \omega_ and \omega_ are the probabilities of the two classes separated by a threshold t ,and \sigma^2_ and \sigma^2_ are variances of these two classes. The class probability \omega_(t) is computed from the L bins of the histogram: : \begin \omega_0(t) & =\sum_^ p(i)\\ pt\omega_1(t) & =\sum_^ p(i) \end For 2 classes, minimizing the intra-class variance is equivalent to maximizing inter-class variance: : \begin \sigma^2_b(t) & =\sigma^2-\sigma^2_w(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)\right2 \end which is expressed in terms of class probabilities \omega and class means \mu, where the class means \mu_0(t), \mu_1(t) and \mu_T are: : \begin \mu_0(t) & = \frac \\ pt\mu_1(t) & = \frac \\ \mu_T & = \sum_^ ip(i) \end The following relations can be easily verified: : \begin \omega_0\mu_0+\omega_1\mu_1 & = \mu_T \\ \omega_0+\omega_1 & =1 \end The class probabilities and class means can be computed iteratively. This idea yields an effective algorithm.


Algorithm

# Compute histogram and probabilities of each intensity level # Set up initial \omega_i(0) and \mu_i(0) # Step through all possible thresholds t = 1, \ldots maximum intensity ## Update \omega_i and \mu_i ## Compute \sigma^2_b(t) # Desired threshold corresponds to the maximum \sigma^2_b(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). function level = otsu(histogramCounts) total = sum(histogramCounts); % total number of pixels in the image %% OTSU automatic thresholding top = 256; sumB = 0; wB = 0; maximum = 0.0; sum1 = dot(0:top-1, histogramCounts); for ii = 1:top wF = total - wB; if wB > 0 && wF > 0 mF = (sum1 - sumB) / wF; val = wB * wF * ((sumB / wB) - mF) * ((sumB / wB) - mF); if ( val >= maximum ) level = ii; maximum = val; end end wB = wB + histogramCounts(ii); sumB = sumB + (ii-1) * histogramCounts(ii); end end 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. import numpy as np def compute_otsu_criteria(im, th): # create the thresholded image thresholded_im = np.zeros(im.shape) thresholded_im m >= th= 1 # compute weights nb_pixels = im.size nb_pixels1 = np.count_nonzero(thresholded_im) weight1 = nb_pixels1 / nb_pixels weight0 = 1 - weight1 # if one the classes is empty, eg all pixels are below or above the threshold, that threshold will not be considered # in the search for the best threshold if weight1

0 or weight0

0: return np.inf # find all pixels belonging to each class val_pixels1 = im hresholded_im

1
val_pixels0 = im hresholded_im

0
# compute variance of these classes var0 = np.var(val_pixels0) if len(val_pixels0) > 0 else 0 var1 = np.var(val_pixels1) if len(val_pixels1) > 0 else 0 return weight0 * var0 + weight1 * var1 im = # load your image as a numpy array. # For testing purposes, one can use for example im = np.random.randint(0,255, size = (50,50)) # testing all thresholds from 0 to the maximum of the image threshold_range = range(np.max(im)+1) criterias = ompute_otsu_criteria(im, th) for th in threshold_range # best threshold is the one minimizing the Otsu criteria best_threshold = threshold_range p.argmin(criterias)
Python libraries dedicated to image processing such as
OpenCV OpenCV (''Open Source Computer Vision Library'') is a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel, it was later supported by Willow Garage then Itseez (which was later acquired by In ...
and
Scikit-image scikit-image (formerly scikits.image) is an open-source image processing library for the Python programming language. It includes algorithms for segmentation, geometric transformations, color space manipulation, analysis, filtering, morphology, ...
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 distribution In statistics, a normal distribution or Gaussian distribution is a type of continuous probability distribution for a real-valued random variable. The general form of its probability density function is : f(x) = \frac e^ The parameter \mu ...
s 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 test A statistical hypothesis test is a method of statistical inference used to decide whether the data at hand sufficiently support a particular hypothesis. Hypothesis testing allows us to make probabilistic statements about population parameters. ...
s (to which Otsu's method is heavily connected) can perform correctly even when the working assumptions are not fully satisfied. However, 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 discrete 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 (i, j). Each pair belongs to one of the L\times L possible 2-dimensional bins. The total number of occurrences (frequency), f_, of a pair (i, j), divided by the total number of pixels in the image N, defines the joint probability mass function in a 2-dimensional histogram: :P_ = \frac N, \qquad \sum_^\sum_^ P_=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: : \begin \omega_0 & = \sum_^ \sum_^ P_ \\ \omega_1 & = \sum_^ \sum_^ P_ \end The intensity mean value vectors of two classes and total mean vector can be expressed as follows: : \begin \mu_0 & = mu_, \mu_T = \left sum_^ \sum_^ i \frac, \sum_^\sum_^ j \frac \rightT \\ \mu_1 & = mu_, \mu_T = \left sum_^\sum_^ i \frac, \sum_^\sum_^ j \frac \rightT \\ \mu_T & = mu_, \mu_T = \left sum_^ \sum_^ i P_, \sum_^\sum_^ j P_\rightT \end In most cases the probability off-diagonal will be negligible, so it is 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_^1\omega_k \mu_k-\mu_T)(\mu_k-\mu_T)^T/math> The trace of the discrete matrix can be expressed as : \begin & \operatorname(S_b) \\ pt= & \omega_0 \mu_-\mu_)^2 + (\mu_-\mu_)^2+ \omega_1 \mu_-\mu_)^2 + (\mu_-\mu_)^2\\ pt= & \frac \end where :\mu_i = \sum_^\sum_^iP_ :\mu_j = \sum_^\sum_^jP_ Similar to one-dimensional Otsu's method, the optimal threshold (s,t) is obtained by maximizing \operatorname(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 \operatorname(S_b), that is max,s,t = 0; for ss: 0 to L-1 do for tt: 0 to L-1 do evaluate tr(S_b); if tr(S_b) > max max = tr(S,b); s = ss; t = tt; end if end for end for return s,t; Notice that for evaluating \operatorname(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_, sum over i * P_, and sum over j * P_, 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\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. function threshold = otsu_2D(hists, total) maximum = 0.0; threshold = 0; helperVec = 0:255; mu_t0 = sum(sum(repmat(helperVec',1,256).*hists)); mu_t1 = sum(sum(repmat(helperVec,256,1).*hists)); p_0 = zeros(256); mu_i = p_0; mu_j = p_0; for ii = 1:256 for jj = 1:256 if jj

1 if ii

1 p_0(1,1) = hists(1,1); else p_0(ii,1) = p_0(ii-1,1) + hists(ii,1); mu_i(ii,1) = mu_i(ii-1,1)+(ii-1)*hists(ii,1); mu_j(ii,1) = mu_j(ii-1,1); end else p_0(ii,jj) = p_0(ii,jj-1)+p_0(ii-1,jj)-p_0(ii-1,jj-1)+hists(ii,jj); % THERE IS A BUG HERE. INDICES IN MATLAB MUST BE HIGHER THAN 0. ii-1 is not valid mu_i(ii,jj) = mu_i(ii,jj-1)+mu_i(ii-1,jj)-mu_i(ii-1,jj-1)+(ii-1)*hists(ii,jj); mu_j(ii,jj) = mu_j(ii,jj-1)+mu_j(ii-1,jj)-mu_j(ii-1,jj-1)+(jj-1)*hists(ii,jj); end if (p_0(ii,jj)

0) continue; end if (p_0(ii,jj)

total) break; end tr = ((mu_i(ii,jj)-p_0(ii,jj)*mu_t0)^2 + (mu_j(ii,jj)-p_0(ii,jj)*mu_t1)^2)/(p_0(ii,jj)*(1-p_0(ii,jj))); if ( tr >= maximum ) threshold = ii; maximum = tr; end end end end


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 In statistics, maximum likelihood estimation (MLE) is a method of estimating the parameters of an assumed probability distribution, given some observed data. This is achieved by maximizing a likelihood function so that, under the assumed stati ...
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 \eta_1 using the Otsu’s method. Based on threshold \eta_1, the algorithm calculates mean \mu_^ of pixels above \eta_1 and mean \mu_^ of pixels below \eta_1. Then the algorithm tentatively separates the image into three classes (hence the name triclass), with the pixels above the upper mean \mu_^ designated as the temporary foreground F class and pixels below the lower mean \mu_^ designated as the temporary background B class. Pixels fall between mu_^, \mu_^/math> 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 \eta_2. The algorithm then calculates the mean \mu_^ of pixels in the TBD region that are above \eta_2 and the mean \mu_^ of pixels in the TBD region that are below \eta_2. Pixels in the TBD region that are greater than the upper mean \mu_^ are added to the temporary foreground F. And pixels in the TBD region that are less than the lower mean \mu_^ are added to the temporary background B. Similarly, a new TBD region is obtained, which contains all the pixels falling between mu_^, \mu_^/math>. 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 \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

{{reflist


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

using Otsu's method to do the threshold

with a working example and Java implementation

in
ITK Itk is a framework for building mega-widgets using the Incr Tcl incr Tcl (commonly stylised as '' ncr Tcl/nowiki>'', and often abbreviated to ''itcl'') is a set of object-oriented extensions for the Tcl programming language. It is widely us ...

Otsu Thresholding in C#
– a straightforward C# implementation with explanation



Image segmentation Statistical deviation and dispersion