0% found this document useful (0 votes)
7 views23 pages

Machine_Learning_Course_Shashi_Lecture_03

The document discusses image processing techniques, focusing on histogram equalization and thresholding methods for enhancing image contrast and removing noise. It explains the mathematical foundations of histogram equalization, including cumulative distribution functions and probability density functions, as well as various thresholding techniques like binary, normal, and Otsu thresholding. Additionally, it introduces region representation using quad trees for efficient image segmentation.

Uploaded by

chadchampion183
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views23 pages

Machine_Learning_Course_Shashi_Lecture_03

The document discusses image processing techniques, focusing on histogram equalization and thresholding methods for enhancing image contrast and removing noise. It explains the mathematical foundations of histogram equalization, including cumulative distribution functions and probability density functions, as well as various thresholding techniques like binary, normal, and Otsu thresholding. Additionally, it introduces region representation using quad trees for efficient image segmentation.

Uploaded by

chadchampion183
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

IDC410

A course on Image Processing and


Machine Learning
(Lecture 03)

Shashikant Dugad,
IISER Mohali

Shashikant R Dugad, IISER Mohali 1


o n
a ti
lis
u a
Eq
a m
o gr
s t
H i

Shashikant R. Dugad, IISER Mohali 2


Image Contrast: Histogram Equalization
● Consider a gray scale image of size N x N, thus, having N2 pixels. Position of a pixel is
denoted by (y, x)

● If the intensity, I(y, x), of a pixel is represented by m bits then.


○ the maximum intensity is Imax = (2m - 1)

● Image contrast is optimized by doing smart intensity transformation function!


○ I’(y,x) = T(I(x,y)) T is a function of intensity transformation

● For a given original image, obtain intensity histogram, n = h(I), where, a) n is number of
pixels having intensity I and b) range of intensity is 0<= I < (2m - 1) èTotal # of bins in
histogram are (2m - 1)
○ Note: ∑ 𝒏 = ∑ 𝒉(𝑰) = 𝑵𝟐
𝒏
● Probability of a pixel having intensity I is: 𝒑 𝑰 = 𝑵𝟐
where, ∑ 𝒑(𝑰) = 𝟏

Shashikant R. Dugad, IISER Mohali 3


Image Contrast: Histogram Equalization
● Let us apply following constraints on te intensity transformation function
○ T(I) must be a strictly increasing function. This makes it an injective (one-to-one)
function.
○ 0 ≤ T( I ) ≤ L-1. This makes T(I) surjective.

● The above two conditions make T(I) a bijective function, thus inversible
○ Therefore, there exist a function that provides, I = T-1(I’)

● Cumulative Distribution Function (CDF) for input image can be defined as:
𝑰 𝑰
𝟏
𝑭𝒓 𝑰 = 𝑷(𝒓 ≤ 𝑰) = * 𝒑𝒓 𝒊 = 𝟐 * 𝒏𝒊
𝑵
𝒊,𝟎 𝒊,𝟎

● CDF for final image can be written as,


𝑭𝒔 𝒙 = 𝑷 𝒔 ≤ 𝒙 = 𝑷 𝑻 𝒓 ≤ 𝒙 = 𝑷 𝒓 ≤ 𝑻0𝟏 𝒙 = 𝑭𝒓(𝑻0𝟏 (𝒙))

Shashikant R. Dugad, IISER Mohali 4


distribution function (CDF) of r is Fr (x). Now the CDF of s will be :
Image Contrast: Histogram Equalization
FS(x) = P (s ≤ x) = P (T (r) ≤ x) = P (r ≤ T − 1(x)) = F r(T − 1(x)).

We put the first condition of T(r) precisely to make the above step hold true. The second
condition is needed as s is the intensity value for the output image and so must be between o
and (L-1).

So, a pdf of s can be obtained by differentiating FS( x ) with respect to x. We get the following
relation:
ps (s) = pr (r) dr
ds

Now, if we define the transformation function as follows:


r
s = T (r) = (L − 1) ∫0 pr (x)dx

Then using this function gives us a uniform pdf for s.


ds r
dr
= (L − 1) drd ∫0 pr (x)dx = (L − 1)pr (r)

The above step used Leibnitz’s integral rule.


Shashikant UsingIISER
R. Dugad, the Mohali
above derivative, we get: 5
Then using this function gives us a uniform pdf for s.
ds r
Image Contrast: Histogram Equalization
dr
= (L − 1) drd ∫0 pr (x)dx = (L − 1)pr (r)

The above step used Leibnitz’s integral rule. Using the above derivative, we get:
ps (s) = pr (r) dr
ds
= pr (r) 1
(L−1)pr (r)
= 1
L−1

So the pdf of s is uniform. This is what we want.

Now, we extend the above continuous case to the discrete case. The natural replacement of the
integral sign is the summation. Hence, we are left with the following histogram equalization
transformation function.
(L−1)
sk = T (rk ) = (L − 1) ∑kj=0 pr (rj ) = N2
∑kj=0 nj

Since s must have integer values, any non-integer value obtained from the above function is
rounded off to the nearest integer.

Shashikant R. Dugad, IISER Mohali 6


Histogram Equalization: Actual Calculation
Image Size: 8 x 9 Pixel intensity: 3 bits L=8
Probbility
Pixel Intensity in Frequency Cumulative Pixel Intensity (I')
I*f Distribution (L-1) *CPDF I' * f
Original Image (I) (f) PDF (CPDF) in Final Image
Function (PDF)
0 2 0 0.02778 0.02778 0.19444 0 0
1 4 4 0.05556 0.08333 0.58333 1 4

2 6 12 0.08333 0.16667 1.16667 1 6

3 8 24 0.11111 0.27778 1.94444 2 16


4 10 40 0.13889 0.41667 2.91667 3 30
5 12 60 0.16667 0.58333 4.08333 4 48

6 14 84 0.19444 0.77778 5.44444 5 70


7 16 112 0.22222 1.00000 7.00000 7 112

Total = 72 336 1 Total = 286


Average Intensity = 4.6667 Average Intensity = 4

Shashikant R. Dugad, IISER Mohali 7


Image Contrast Results
CHAPrnR4. ~AGEF~TEmNG
EAR SYSTEMS 115
A B

Figure 4.2: The original image has very poor contrast since the gr
A: Original
are in a very small range. Histogram scaling improves the contrast b
gaps in the final histogram. Top: Original
B: Histogram Scaling image and histogram.
Image and resulting histogram after histogram scaling.
C: Histogram Equalization
find the value k2 such that
C Shashikant R.
k2-1 k2 8
3:The
Theoriginal
originalimage
image has
has very
very poor
poor contrast
contrast theDugad,
since the
since grayIISER
gray Mohali
values
values
.L Pi :::; ql + q2 < .L Pi.
Image before and after Histogram Equalization

https://en.wikipedia.org/wiki/Histogram_equalization
Source:
Shashikant R. Dugad, IISER Mohali 9
n g
d i
h ol
e s
h r
T

Shashikant R. Dugad, IISER Mohali 10


Noise Removal: Thresholding

● Binary Thresholding: ● Iterative Threshold


M(i, j) = 2m-1 if pixel intensity P(i, j) > p0
● Double Thresholding for Region
M(i, j) = 0 if pixel intensity P(i, j) <= p0
Growing

● Normal Thresholding:
● Otsu Thresholding
M(i, j) = P(i, j) if pixel intensity P(i, j) > p0

M(i, j) = 0 if pixel intensity P(i, j) <= p0 ● Adaptive Thresholding

● Regional Thresholding:
M(i, j) = P(i, j) if pixel intensity p0 < P(i, j) < p1
M(i, j) = 0 if pixel intensity P(i, j) <= p0 || P(i, j) >= p1

Shashikant R. Dugad, IISER Mohali 11


Iterative Threshold
1. Select an initial estimate of the threshold μ, say, the average intensity of the
image.
∑𝒏𝒊,𝟎 ∑𝒎
𝒋,𝟎 𝑴(𝒊, 𝒋)
μ=
𝒏 ∗𝒎
2. Partition the image into two groups, R1 and R2, using the threshold μ .
3. Calculate the mean grey values μ1 and μ2 of the partitions R1 and R2

4. Select the new Threshold


μ𝟏 + μ𝟐
μ=
𝟐
5. Repeat step 2-4 until μ1 and μ2 (therefore, μ) are approximately same in
successive iterations

Shashikant R. Dugad, IISER Mohali 12


Double Thresholding for Region Growing
1. Select two thresholds μ1 and μ2 (You may use the prescription on previous
slide to get μ1 and μ2)

2. Partition the image into three regions:


• R1: containing all pixels with gray values below μ1 ,
• R2: containing all pixels with gray values between μ1 and μ2 inclusive and
• R3, containing all pixels with gray values above μ2 .

3. Visit each pixel assigned to region R2. If the pixel has a neighbour in region
R1, then reassign the pixel to region R1

4. Repeat step 3 until no pixels are reassigned.

5. Reassign any pixels left in region R2 to region R3.


Shashikant R. Dugad, IISER Mohali 13
3. REGION REPRESENTATION

'500

3500

3000

25001
1
. .
(c)
87

(d)

.
(c) (d)
(a) (b)

(c) (d) (e) (e) (f) (f)


Figure 3.8: An image that is difficult to segment using a single threshold
Figure 3.8: An image that is difficult to segment using a single threshold
and its segmentation using double thresholding. (a) Original image. (b)
and its segmentation using double Histogram thresholding. (a)image.
of original Original image.
(c) Core region,(b)
Tl = 70. (d) Results after
Histogram of original image. (c) Core growing region,
the coreTlregion
= 70.
using(d) Results
a second after T2 = 155. Compare this
threshold,
growing the core region using a second threshold, T2 = 155. Compare this
with the results using regular thresholding shown in (e) and (f). (e) Threshold
with the results using regular thresholding = T2. (f) Tl :S Threshold :S T2. Note that it is impossible to segment the
shown in (e) and (f). (e) Threshold
entire right square without using region growing threshold.
(e) = T2. (f) Tl :S Threshold
(f) :S T2. Note that it is impossible to segment the
igure 3.8: An image thatis entire right
difficult to square
segment using awithout using region growing threshold.
single threshold
nd its segmentation using double thresholding. (a) Original image. (b)
Shashikant R. Dugad, IISER Mohali 14
istogram of original image. (c) Core region, Tl = 70. (d) Results after
owing the core region using a second threshold, T2 = 155. Compare this
Regions

Regions are used in many contexts and can be represented in many


alternative forms. Different representations are suitable in different
applications. Some applications require computations only for a
single region, while others require relationships among different
regions of an image. In this section, we will discuss a few commonly
used representations of regions and study their features. It must be
mentioned here that regions can also be represented as closed
contours.

Shashikant R. Dugad, IISER Mohali 15


Region: Quad Tree
A quad tree contains three types of nodes: white, black, and gray. A quad
tree is obtained by recursive splitting of an image. A region in an image is
split into four subregions of identical size, if all points in the region are
either white or black, then this region is no longer considered as a
candidate for splitting; if it contains pixels of both kinds, it is considered to
be a gray-region and is further split into four subregions. An image
obtained using this recursive splitting is represented in a tree structure.
The splitting process is repeated until there are no gray regions in the tree.
Each node in this structure is either a leaf node or has four children, thus
the name quad tree.

Shashikant R. Dugad, IISER Mohali 16


3.3. REGION REPRESENTATION 2 3 4
91
(b)

(a)

2 3 4

(d)
(b)
Figure 3.10: The building of a quad tree. (a) Original image, "gray region."
(d)
(b) Original split into four subregions (the left node in the tree corresponds to
the top left region in the image). Note that two of these regions are also gray
regions. (c) Splitting the gray regions from (b) into four subregions. One of
Figure 3.10: The building of a quad tree. (a)is Original
these regions image,
still a gray region. "gray
(d) Splitting of the region."
last gray region and
the final quad tree.
(b) Original split into four subregions (the left node in the tree corresponds to
the top left region in the image). Note that two of these regions are also gray
regions. (c) Splitting the gray regions from (b) into four subregions. One of
these regions is still a gray region. (d) Splitting of the last gray region and
the final quad tree. Shashikant R. Dugad, IISER Mohali 17
Otsu Thresholding Algorithm

● Source: https://en.wikipedia.org/wiki/Otsu%27s_method

● In image processing, Otsu's method, named after Nobuyuki Otsu, is used to


perform automatic image thresholding.

● The algorithm returns a single intensity threshold that separate pixels into
two classes, foreground and background.

● The threshold is determined by minimizing intra-class intensity variance, or


equivalently, by maximizing inter-class variance.

Shashikant R. Dugad, IISER Mohali 18


Otsu Thresholding Algorithm
● The algorithm exhaustively searches for the threshold that minimizes the intra-class
variance, (maximises intra class variance) which is defined as a weighted sum of variances
of the two classes:

𝝈𝟐𝝎 𝒕 = 𝝎𝟎 𝒕 𝝈𝟐𝟎 𝒕 + 𝝎𝟏 𝒕 𝝈𝟐𝟏 𝒕

● Weights ω0 and ω1 are the probabilities of the two classes separated by a threshold t, σ02
and σ12 are variances in each of of these two classes

𝐭'𝟏 𝐋'𝟏
𝐧𝐢
𝐩 𝐢 = . 𝛚𝟎 𝐭 = 2 𝐩 𝐢 . 𝐚𝐧𝐝. 𝛚𝟏 𝐭 = 2 𝐩 𝐢 𝐚𝐧𝐝. 𝛚𝟎 𝒕 + 𝛚𝟏 𝐭 = 𝟏
𝐍𝐱𝐌
𝐢%𝟎 𝐢%𝐭

Shashikant R. Dugad, IISER Mohali 19


07/01/2025, 15:02 Otsu's method - Wikipe

which is expressed in terms of class probabilities and class


Otsu Thresholding
, and Algorithm
are:
07/01/2025, 15:02 Otsu's method - Wikipedia

which is expressed in terms of class probabilities and class means , where the class me
● μ0, μ1 and μT are the means of regions a) below threshold t, b) above threshold t and
, and are:
entire region respectively,

● For 2 classes, minimizing the intra-class variance is equivalent to maximizing


The following relationsinter-
can be easily
class variance

𝝈𝟐𝒃 𝒕 = 𝝈𝟐 − 𝝆𝟐𝝎 𝒕 =The 𝝎𝟎following 𝟐


(𝒕)(𝝁𝟎 − 𝝁relations
𝑻 ) + 𝝎𝟏can
(𝒕)(𝝁 )𝟐 .
− 𝝁𝑻verified:
be𝟏easily
= 𝝎𝟎 (𝒕)𝝎𝟏 (𝒕)[𝝁𝟎 𝒕 − 𝝁𝟏 (𝒕)]𝟐
The class probabilities and class me
The following relations can be easily verified:
algorithm.

The class probabilities and class means can be computed itera


Shashikant R. Dugad, IISER Mohali Algorithm 20
algorithm.
Otsu Thresholding: Implementation

● Step 1: Compute histogram of a given image and obtain probabilities [p(i)]


at each intensity level i

● Step 2: Obtain ω0, ω1, μ0 and μ1 (hence σb) at each intensity value of t
varying between 0 to L-1

● Select that value of t as the final threshold at which σb(t) has maximum
value

Shashikant R. Dugad, IISER Mohali 21


nd
a-
er- By Pikez33
ete http://en.wikipedia.org/wiki/File:Image_processing_post_otsus_algorithm.jpg,
ks An example imagehttps://commons.wikimedia.org/w/index.php?curid=10634740
thresholded
thod
k-
on
using Otsu's algorithm

d image processing, Otsu's method,


nal
Otsu
ve ( ⼤ 津 展 之 , Ōtsu Nobuyuki), is
matic image thresholding.[1] In the
gorithm returns a single intensity
pixels into two classes, foreground and
old is determined by minimizing intra-
or equivalently, by maximizing inter-
at
method is a one-dimensional discrete
m
criminant analysis, is related to Jenks An example image thresholded
Original
nd is equivalent image
to a globally optimal k- using Otsu's algorithm
the intensity histogram. The extension
ding was described in the original
By Lucas(CA)
onally efficient - Own work,
implementations have CC BY-SA 4.0,
es
5]
https://commons.wikimedia.org/w/index.php?curid=671
of
44384

dof
Shashikant R. Dugad, IISER Mohali 22
ively searches for the threshold that
Adaptive Thresholding

Ø ADAPTIVE_THRESH_MEAN_C:
Threshold value is the mean of
neighbourhood area.

Ø ADAPTIVE_THRESH_GAUSSIAN_C:
Threshold value is the weighted sum of
neighbourhood values where weights
are a gaussian window.

You might also like