You are on page 1of 4

Optimal Pixel Classification

• Segmentation is a process of pixel classification.

• For example: classify pixels into “dark” and “light” classes to distinguish dark
objects from a light background.

• In general, pixels are classified based on some property (such as gray level) that
can be measured.

• If the probabilities with which the classes occur and the probability density
functions are both known, a classification criterion that minimizes the expected
classification error can be derived.

• This is a standard problem in pattern recognition.

• The simplest case is when there are only two classes (e.g., light/dark) and the
classification is based on a single property value, Z (e.g., gray scale).

• Basically the objective is to segment the pixels of an image into two classes (or
sets):
S1 , S 2

• The probabilities that the two classes occur are (e.g., 40% and 60%):

P(S1 ), P(S 2 ) where: P(S 2 ) = 1 − P(S1 )

• The probability density functions of Z (gray scale) with respect to the two classes
are given by:

f (Z | S1 ), f (Z | S 2 )

• The segmentation problem consists of determining a threshold, T, that results in


an optimal pixel classification subject to the following rule:

if Z < T then the pixel belongs to class S1


if Z ≥ T then the pixel belongs to class S2
• The probability of incorrectly classifying a pixel that actually belongs to class S2
is given by:

T
P(T | S 2 ) = ∫ f (Z | S )dz2
−∞

• The probability of incorrectly classifying a pixel that actually belongs to class S1


is given by:

T ∞
1 − P(T | S1 ) = 1 − ∫ f (Z | S )dz = ∫ f (Z | S )dz
1 1
−∞ T

• The probability of incorrectly classifying a point is given by:

PM = P(S1 )[1 − P(T | S1 )] + P(S 2 )P(T | S 2 )

• In order to determine the optimal threshold, T, calculate the derivative of PM with


respect to T and set it equal to zero. This yields:

P(S1 ) f (T | S1 ) = P(S 2 ) f (T | S 2 )

• This is based on the second fundamental theorem of calculus:

x
f (t )dt = f ( x )
d
dx ∫a

• In the case where f (T | S1 ) and f (T | S 2 ) are Gaussian distributions, the optimal


threshold, T, can be derived as follows:
2 2
1 ⎛ T − μ1 ⎞ 1 ⎛ T − μ2 ⎞
− ⎜⎜ ⎟ − ⎜⎜ ⎟⎟
P (S 1 ) = P (S 2 )
1 2 ⎝ σ 1 ⎟⎠ 1 2⎝ σ 2 ⎠
e e
σ 1 2π σ 2 2π

2
1 ⎛ T −μ2 ⎞
− ⎜⎜ ⎟⎟ 2 2

P(S1 )σ 2 e 2⎝ σ 2 1 ⎛ T − μ1 ⎞ 1 ⎛ T − μ 2 ⎞

⎠ ⎟ − ⎜ ⎟⎟
2 ⎜⎝ σ 1 ⎟⎠ 2 ⎜⎝ σ 2
= = e ⎠
P(S 2 )σ 1
2
1 ⎛ T − μ1 ⎞
− ⎜⎜ ⎟⎟
2 σ
e ⎝ 1 ⎠
⎡ P(S1 )σ 2 ⎤ 1 ⎛ T − μ1 ⎞
2 2
1 ⎛ T − μ2 ⎞
ln ⎢ ⎥ = ⎜⎜ ⎟⎟ − ⎜⎜ ⎟⎟
⎣ P(S 2 )σ 1 ⎦ 2 ⎝ σ 1 ⎠ 2⎝ σ2 ⎠

⎡ P(S1 )σ 2 ⎤
2σ 1 σ 2 ln ⎢ ⎥ = σ 2 (T − μ1 ) − σ 1 (T − μ 2 )
2 2 2 2 2 2

⎣ P(S 2 )σ 1 ⎦

• This is a quadratic with respect to the optimal threshold T that can be solved as
follows:

⎡ P(S1 )σ 2 ⎤
Let: K = 2σ 1 σ 2 ln ⎢
2 2

⎣ P(S 2 )σ 1 ⎦

• Substituting in to the quadratic above:

K = σ 2 (T − μ1 ) − σ 1 (T − μ 2 )
2 2 2 2

0 = σ 2 (T − μ1 ) − σ 1 (T − μ 2 ) − K
2 2 2 2

(
0 = σ 2 T 2 − 2Tμ1 + μ1 − σ 1 T 2 − 2Tμ1 + μ1 − K
2 2
) 2
( 2
)
• Gathering the coefficients of T0, T1, and T2:

( 2
) (
0 = − σ 1 + σ 2 T 2 + − 2σ 2 μ1 + 2σ 1 μ 2 T + σ 2 μ1 − σ 1 μ 2 − K
2 2 2
) ( 2 2 2 2
)

• Which yields:

0 = AT 2 + BT + C

with: (
A = − σ1 + σ 2
2 2
)
(
B = − 2σ 2 μ1 + 2σ 1 μ 2
2 2
)
(
C = σ 2 μ1 − σ 1 μ 2 − K
2 2 2 2
)
• This is the basis of various techniques such as Kittler and Illingworth’s method
based on minimizing Kullback Information Distance.
Otsu’s Method

• Best threshold suggested by Otsu is the threshold corresponding to the minimum


sum of weighted within-group variances:

σ W2 = P(S1 )σ 12 + P(S 2 )σ 22

• This is equivalent to selecting a threshold that maximizes the squared difference


between the group means (which is related to the between-group variance).
• The sum of the between-group variance and the within-group variances is a
constant.
• A simple Matlab implementation of Otsu’s Mthod is given below.

n = W*H;
wgv_min = 1.0E9;

for T=1:256

BX2 = 0;
BX = 0;
BN = 0;

WX2 = 0;
WX = 0;
WN = 0;

for I=1:256
p=double(x_hist(I));
if I<T
BX = BX + p*(I-1);
BX2 = BX2 + p*(I-1)*(I-1);
BN = BN + p;
else
WX = WX + p*(I-1);
WX2 = WX2 + p*(I-1)*(I-1);
WN = WN + p;
end;
end;
v1 = (BN*BX2 - BX*BX) / (BN*(BN-1));
ps1= BN/n;

v2 = (WN*WX2 - WX*WX) / (WN*(WN-1));


ps2= WN/n;

wgv = ps1*v1 + ps2*v2;

if wgv<wgv_min
wgv_min=wgv;
thr=T;
end;
end;

You might also like