Otsu's method


In computer vision and image processing, Otsu's method, named after Nobuyuki Otsu, 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 analog 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:
Weights and are the probabilities of the two classes separated by a threshold ,and and are variances of these two classes.
The class probability is computed from the bins of the histogram:
For 2 classes, minimizing the intra-class variance is equivalent to maximizing inter-class variance:
which is expressed in terms of class probabilities and class means, where the class means, and are:
The following relations can be easily verified:
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 and
  3. Step through all possible thresholds maximum intensity
  4. # Update and
  5. # Compute
  6. Desired threshold corresponds to the maximum

    MATLAB or Octave implementation

histogramCounts is a 256-element histogram of a grayscale image different gray-levels.
level is the threshold for the image.

function level = otsu
total = sum; % total number of pixels in the image
%% OTSU automatic thresholding
top = 256;
sumB = 0;
wB = 0;
maximum = 0.0;
sum1 = dot;
for ii = 1:top
wF = total - wB;
if wB > 0 && wF > 0
mF = / wF;
val = wB * wF * * ;
if
level = ii;
maximum = val;
end
end
wB = wB + histogramCounts;
sumB = sumB + * histogramCounts;
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.

Limitations

Otsu's method exhibits the relatively good performance if the histogram can be assumed to have bimodal distribution and assumed to possess a deep 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.
Empirical results show that the performance of global thresholding techniques used for object segmentation are limited by small object size, the small mean difference between foreground and background pixels, large variances of the pixels that belong to the object and those that belong to the background, the large amount of noise, etc.

Improvements

Various extensions have been developed to address limitations of Otsu's method. One popular extension 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 discrete values and the average gray level is also divided into the same values. Then a pair is formed: the pixel gray level and the average of the neighborhood. Each pair belongs to one of the possible 2-dimensional bins. The total number of occurrences,, of a pair, divided by the total number of pixels in the image, defines the joint probability mass function in a 2-dimensional histogram:
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:
The intensity mean value vectors of two classes and total mean vector can be expressed as follows:
In most cases the probability off-diagonal will be negligible, so it is easy to verify:
The inter-class discrete matrix is defined as
The trace of the discrete matrix can be expressed as
where
Similar to one-dimensional Otsu's method, the optimal threshold is obtained by maximizing.

Algorithm

The and is obtained iteratively which is similar with one-dimensional Otsu's method. The values of and are changed till we obtain the maximum of, that is

max,s,t = 0;
for ss: 0 to L-1 do
for tt: 0 to L-1 do
evaluate tr;
if tr > max
max = tr;
s = ss;
t = tt;
end if
end for
end for
return s,t;

Notice that for evaluating, 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, sum over,
and sum over, then the runtime complexity is the maximum of, O).
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 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
maximum = 0.0;
threshold = 0;
helperVec = 0:255;
mu_t0 = sum;
mu_t1 = sum;
p_0 = zeros;
mu_i = p_0;
mu_j = p_0;
for ii = 1:256
for jj = 1:256
if jj 1
if ii 1
p_0 = hists;
else
p_0 = p_0 + hists;
mu_i = mu_i+*hists;
mu_j = mu_j;
end
else
p_0 = p_0+p_0-p_0+hists;
mu_i = mu_i+mu_i-mu_i+*hists;
mu_j = mu_j+mu_j-mu_j+*hists;
end
if
continue;
end
if
break;
end
tr = -p_0^2 + -p_0^2)/;
if
threshold = ii;
maximum = tr;
end
end
end
end