Otsu
Otsu
Otsu
Example
Thresholding
Thresholding is used to extract an object from its
background by assigning an intensity value T(threshold)
for each pixel such that each pixel is either classified as
an object point or a background point
In general T = T [ x, y, p( x, y ), f ( x, y )]
If T is a function of f(x,y) only
Global thresholding
Global Thresholding
When the modes of histogram can be
clearly distinguished
Global Thresholding
200
200
150
150
100
100
50
50
60
80
100
50
20 40 60 80 100
t=23 sc=2.3539
50
20
20
40
40
60
60
80
80
100
20
40
60
80
100
100
20
40
60
80
100
Global Thresholding
An automated procedure for bimodal histograms
Initial threshold is
the average of the
gray values
Clear bimodal
histogram
Download the image
(a) and choose
initial thresholds
from 30 ~ 220
Plot the values of
final thresholds vs.
initial thresholds
Repeat the
experiment using an
image with three
major modes
Fixed Thresholding
In fixed (or global) thresholding, the threshold value is held
constant throughout the image:
Determine a single threshold value by treating each
pixel independently of its neighborhood.
0
Fixed thresholding is of the form: g(x,y) =
f(x,y)<T
1 f(x,y)>=T
Assumes high-intensity pixels are of interest, and lowintensity pixels are not.
and
in
Matlab Implementation
Matlab function opthr
addpath C:\Zhang\scm3511\week3\matlab
im = imread('boy.jpg');
I = rgb2gray(im);
I=double(I);
T=opthr(I);
bim=(I>T);
subplot(1,2,1), imshow(I, gray(256));
subplot(1,2,2), imshow(bim)
Optimal Thresholding
Optimal Thresholding
In optimal thresholding, a criterion function is devised that
yields some measure of separation between regions.
A criterion function is calculated for each intensity and
that which maximizes this function is chosen as the
threshold.
Otsus thresholding chooses the threshold to minimize the
intraclass variance of the thresholded black and white pixels.
Otsu: Assumptions
Histogram (and the image) are bimodal.
No use of spatial coherence, nor any other
notion of object structure.
Assumes stationary statistics, but can be
modified to be locally adaptive. (exercises)
Assumes uniform illumination (implicitly),
so the bimodal brightness behavior arises
from object appearance differences only.
(t ) = q1 (t ) (t ) + q 2 (t ) (t )
2
w
2
1
2
2
q1 (t) = P(i)
q2 (t) =
i =1
P(i)
i = t +1
iP(i)
1 (t) =
i =1 q1 (t)
iP(i)
2 (t) =
i =t +1 q2 (t )
P(i)
(t) = [i 1 (t)]
q1 (t)
i=1
2
2
1
P(i)
(t) = [i 2 (t)]
q2 (t)
i =t +1
2
2
Between/Within/Total Variance
The book gives the details, but the basic idea
is that the total variance does not depend on
threshold (obviously).
For any given threshold, the total variance is
the sum of the within-class variances
(weighted) and the between class variance,
which is the sum of weighted squared
distances between the class means and the
grand mean.
2
w
Within-class,
from before
Between-class, B (t)
Finally...
Initialization...
Recursion...
q1 (t + 1) = q1 (t) + P(t + 1)
2 (t + 1) =
q1 (t + 1)1 (t + 1)
1 q1 (t + 1)
>>n=imread('nodules1.tif');
>> tn=graythresh(n)
tn = 0.5804
>> imshow(im2bw(n,tn))
r=imread('rice.tif');
graythresh;
subplot(211);
imshow;
subplot(212);
imshow(im2bw(r,tr))
graythresh
Global image threshold using Otsu's method
Syntax
level = graythresh(I)
[level EM] = graythresh(I)
Description
computes a global threshold (level) that can be used to
convert an intensity image to a binary image with im2bw. level is a normalized
intensity value that lies in the range [0, 1].
level = graythresh(I)
The graythresh function uses Otsu's method, which chooses the threshold to
minimize the intraclass variance of the black and white pixels.
Multidimensional arrays are converted automatically to 2-D arrays using reshape.
The graythresh function ignores any nonzero imaginary part of I.
returns the effectiveness metric, EM, as the second
output argument. The effectiveness metric is a value in the range [0 1] that indicates
the effectiveness of the thresholding of the input image. The lower bound is attainable
only by images having a single gray level, and the upper bound is attainable only by
two-valued images.
[level EM] = graythresh(I)
Class Support
The input image I can be of class uint8, uint16, int16, single,or double and it
must be nonsparse. The return value level is a double scalar. The effectiveness
metric EM is a double scalar.
Examples
I = imread('coins.png');
level = graythresh(I);
BW = im2bw(I,level);
imshow(BW)
Reference
[1] Otsu, N., "A Threshold Selection Method from Gray-Level Histograms," IEEE
Transactions on Systems, Man, and Cybernetics, Vol. 9, No. 1, 1979, pp. 62-66.