You are on page 1of 95

Digital Image Processing, 3rd ed.

Gonzalez & Woods


www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering
Chapter 3: Intensity transformations and spatial filtering

Generic form:
g(x, y) = T [f (x, y)]

new intensity value at (x,y) is a function of the


old intensity value at
(x,y), or Sxy

© 1992–2008 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Example of spatial filtering:


blurring by 3x3 neighborhood

Create a 3x3 mask, and run it over the whole image to generate the new
image (can also use conv2)

Here I have
run the mask
only on the
left half of
the
picture.
© 1992–2008 R. C. Gonzalez & R. E. W ood
s
Tuesday, February 1, 2011
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Example of spatial filtering:


blurring by 3x3 neighborhood

Create a 3x3 mask, and run it over the whole image to generate the new
image (can also use conv2)

Here I have
run the mask
Example of a blurred
only on the
image by a 3x3
left half of mask
the picture.
© 1992–2008 R. C. Gonzalez & R. E. W ood
s
Tuesday, February 1, 2011
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering
Simplest example:
1-pixel neighborhood (the pixel itself), in which case we operate
directly on the intensity of the pixel.

This is called point processing as opposed to


neighborhood processing.

Typical operations we will perform:


Image enhancement
contrast stretching
Is the task of manipulating an image so
thresholding that the result is more suitable for specific
applications.
Mostly visual (subjective), no general
© 1992–2008 R. C. Gonzalez & R. E. Woods
quantitative assessment.
Tuesday, February 1, 2011
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

Contrast stretching (sigmoid):

low intensity areas are mapped


to even lower
high intensity areas are mapped
to even higher
medium intensity areas are
“stretched”.

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

Contrast stretching (sigmoid):

low intensity areas are mapped


to even lower
high intensity areas are mapped
to even higher
medium intensity areas are
“stretched”.

© 1992–2008 R. C. Gonzalez & R. E. Woods Original contrast stretched


Tuesday, February 1, 2011
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

Contrast stretching (sigmoid):

low intensity areas are mapped


to even lower
high intensity areas are mapped
to even higher
medium intensity areas are
“stretched”.

© 1992–2008 R. C. Gonzalez & R. E. Woods Original contrast stretched


Tuesday, February 1, 2011
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Thresholding: we choose a threshold parameter k: intensities below


this parameters are set to 0, those above are set to 1.
This produces a binary image.

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Thresholding: we choose a threshold parameter k: intensities below


this parameters are set to 0, those above are set to 1.
This produces a binary image.

Thresholded image (setting to 1 the


values above 150, 0 the others)

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Other maps of the type s=T(r)


Negative map: s = (L − 1) − r

useful when one is interested in gray/white areas and the black areas
are dominating

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Other maps of the type s=T(r)


Negative map: s = (L − 1) − r

useful when one is interested in gray/white areas and the black areas
are dominating

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Other maps of the type s=T(r)


Negative map: s = (L − 1) − r

useful when one is interested in gray/white areas and the black areas
are dominating

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Other maps of the type s=T(r)


Negative map: s = (L − 1) − r

useful when one is interested in gray/white areas and the black areas
are dominating

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Log map: s = c log(1 + r)


maps lower intensities to a
wider range, while white
intensities are
mapped to a smaller range

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Log map: s = c log(1 + r)


maps lower intensities to a
wider range, while white
intensities are
mapped to a smaller range

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

Power-law (gamma) map:


s = cr γ

These subsume the log and


inverse log, but are more
powerful, as just varying the
gamma one gets a whole family
of transformations.

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

Power-law (gamma) map: gamma=0.2

s = cr γ

These subsume the log and


inverse log, but are more
powerful, as just varying the
gamma one gets a whole family
of transformations.

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

Power-law (gamma) map: gamma=0.2

s = cr γ

These subsume the log and


inverse log, but are more
powerful, as just varying the
gamma one gets a whole family
of transformations.

gamma=3
© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

Piecewise linear transformations:

Contrast stretching by
piecewise linear transformations.

(r1, s1)
(r2, s2)
Control points

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

r1=0=s1
r2=L-1=s2
Identity

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

r1=r2
s1=0, s2=L-1
Thresholding

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

r1=r2
s1=0, s2=L-1
Thresholding

Other combinations give different effects on the image.

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Other combinations give different effects on the image.

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Intensity level slicing:


to highlight a specific range of intensities.

Most popularly implemented as:


showing white in one range and black in the rest
showing white in one range and graytone in the rest

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Bit slicing:

2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0

0 1 0 1 0 0 1 0

82=64+16+2
© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering
The reconstructed image is obtained as
2^0*bit_plane1 + 2^1*bit_plane2 + …. + 2^7*bit_plane8
bit plane 1

The most
significant planes
are often those with
high bit number
© 1992–2008 R. C. Gonzalez & R. E. Woods
bit plane 8
Tuesday, February 1, 2011
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering
Histogram processing

Histogram: is a discrete function that counts how many pixels have a


given intensity value.

h(r k ) = nk Number of pixels that have such


intensity value

k-th intensity value

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering
Histogram processing

Histogram: is a discrete function that counts how many pixels have a


given intensity value.

h(r k ) = nk Number of pixels that have such


intensity value

k-th intensity value

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

It is typical to scale nk by MN, the number of pixels. The corresponding number represents the
probability of a pixel having the given intensity.

nk
p(r k ) = p(r k ) = 1
MN k
nk = M N
k Probability that a random pixels has intensity r k .

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

© 1992–2008 R. C. Gonzalez & R. E. Woods

Tuesday, February 1, 2011


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

It is typical to scale nk by MN, the number of pixels. The corresponding number represents the probability of a
pixel having the given intensity.

nk
∑ p ( r k )=1
∑ nk= MN p ( r k )  =  
MN k
k
Probability that a random pixels has intensity rk.

© 1992–2008 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Image enhancement by histogram equalization

Consider an intensity transformation s = ( r input intensity level, s


output intensity level). Let us assume that

• T is monotonically increasing that is T


(r1) ≤ T (r2) for r1≤r2
(strictly increasing if T −1 is required)

• T maps [0,L − 1] to [0,L − 1]


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

We can identify the intensity level to a random variable in [0,L − 1].


Then the function p = h/MN is the probability density function of such
random variable.
Denote by

•pr the probability distribution of the original image (r-variable) and

•ps the probability distribution of the image with intensity s = . These

two distributions will generally be different.

Thus, given the change of variables s = , the relation between ps and


pr is given by the formula

𝑝 𝑠 ( 𝑠 )=𝑝𝑟 ( 𝑟 ) | |
𝑑𝑟
𝑑𝑠
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

𝑝 𝑠 ( 𝑠 )=𝑝𝑟 ( 𝑟 )| |
𝑑𝑟
𝑑𝑠
Where does it come from?

ps (s)
p r (r) (Recall, probability
distributions are
positive)
dr ds
r s
s=

The gray regions must have the same area (we are just relabeling the variables)
𝒑 𝒓 ( 𝒓 ) 𝒅𝒓 =𝒑 𝒔 ( 𝒔 ) 𝒅𝒔
(take the absolute value is for the case when T is decreasing)
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

| | 𝑑𝑟 1
𝑝 𝑠 ( 𝑠 )=𝑝𝑟 ( 𝑟 ) 𝑝 𝑠 ( 𝑠 )=𝑝𝑟 ( 𝑟 ) ′ (𝑟)
𝑑𝑠 𝑇

(Alternative formulation)

Example. Consider the transformation


𝒓
s   = 𝑻 ( 𝒓 )   =  ( L   −   1 ) ∫ 𝝆𝒓 ( 𝝎 ) 𝒅 𝝎
𝟎

The transformation is strictly increasing because pr is a positive function.


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

| |𝑑𝑟 1
𝑝 𝑠 ( 𝑠 )=𝑝𝑟 ( 𝑟 ) 𝑝 𝑠 ( 𝑠 )=𝑝𝑟 ( 𝑟 ) ′ (𝑟)
𝑑𝑠 𝑇

(Alternative formulation)

Examp l e. Consider the transformation


𝒓
s   = 𝑻 ( 𝒓 )   =  ( L   −.   1 ) ∫ 𝝆𝒓 ( 𝝎 ) 𝒅 𝝎
𝟎

The transformation is strictly increasing because pr is a positive function.

Cumulative probability
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

| |𝑑𝑟 1
𝑝 𝑠 ( 𝑠 )=𝑝𝑟 ( 𝑟 ) 𝑝 𝑠 ( 𝑠 )=𝑝𝑟 ( 𝑟 ) ′ (𝑟)
𝑑𝑠 𝑇

(Alternative formulation)

Examp l e. Consider the transformation


𝒓
s   = 𝑻 ( 𝒓 )   =  ( L   −.   1 )   ∫ 𝝆 𝒓 ( 𝝎 ) 𝒅 𝝎
𝟎

The transformation is strictly increasing because pr is a positive function.

Cumulative probability
Scaling factor so that T(L-1) = L-1
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

What is 𝒑 𝒔 ( 𝒔 ) ?

1
Let’s use the formula 𝑝 𝑠 ( 𝑠 )=𝑝𝑟 ( 𝑟 ) ′ (𝑟)
𝑇

We derive with respect to

′ 𝑑
𝑇 (𝑟 )= 𝑇 ( 𝑟 )=( 𝐿 − 1) 𝑝 𝑟 ( 𝑟 )
𝑑𝑟

Hence
1 1 1 The uniform
𝑝 𝑠 ( 𝑠 )= 𝑝𝑟 ( 𝑟 ) ′ =𝑝 𝑟 (𝑟 ) =
|𝑇 (𝑟 )| |( 𝐿 −1)𝑝 𝑟 (𝑟 )| 𝐿 −1
distribution!
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

For discrete density functions, the equivalent of


𝒓
s  = 𝑻 ( 𝒓 ) = ( L   − 1 ) ∫ 𝝆𝒓 ( 𝝎 ) 𝒅 𝝎
𝟎

is obtained by replacing the integral by a sum,


𝐤
𝒔 𝒌  =𝑻 (𝒓 𝒌 ) =  ( L − 1) ∑ 𝐩 𝐫 (𝐫 𝐣)
𝐣=𝟎
𝐤
(L − 1)

𝐌𝐍 𝐣=𝟎
∑ 𝐧𝐣 k = 0 , …, L  −  1

This transformation is called histogram equalization/linearization.


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Example:
𝐤
𝒔 𝒌  =𝑻 (𝒓 𝒌 ) =  ( L − 1) ∑ 𝐩 𝐫 (𝐫 𝐣)
𝐣=𝟎
𝐤
(L − 1)

𝐌𝐍 𝐣=𝟎
∑ 𝐧𝐣 k = 0 , …, L  −  1
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Example:
k
s k  =T (r k ) = ( L − 1) ∑ p r (r j )
j=0
k
(L − 1)

MN j=0
∑ nj k = 0 , … , L  −  1

s0 = (L − 1)pr(r0) = 7 · 0.19 = 1.33

s1 = 7 · (0.19 + 0.25) = 3.08

s3 = 7 · (0.19 + 0.25 + 0.21) = 4.55


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Example:
k
s k  =T (r k ) = ( L − 1) ∑ p r (r j )
j=0
k
(L − 1)

MN j=0
∑ nj k = 0 , … , L  −  1

s0 = (L − 1)pr(r0) = 7 · 0.19 = 1.33 s0 = 1

s1 = 3
s1 = 7 · (0.19 + 0.25) = 3.08
s2 = 5
s3 = 7 · (0.19 + 0.25 + 0.21) = 4.55
and so on...
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Example:
k
s k  =T (r k ) = ( L − 1) ∑ p r (r j )
j=0
k
(L − 1)

MN j=0
∑ nj k = 0 , … , L  −  1

s0 = (L − 1)pr(r0) = 7 · 0.19 = 1.33 s0 = 1

s1 = 3
s1 = 7 · (0.19 + 0.25) = 3.08
s2 = 5
s3 = 7 · (0.19 + 0.25 + 0.21) = 4.55
and so on...
Since the sk is an intensity transformation, it must be an integer
value. This rounding causes the resulting histogram not necessarily
© 1992–2008. R.C. Gonzalez & R. E. Woods to result into a uniform histogram.
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Though the resulting histogram is not uniform, the final result is that
the intensity values are spread out (span a wider range).

This results in an image that has a enhanced contrast.


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Though the resulting histogram is not uniform, the final result is that
the intensity values are spread out (span a wider range).

This results in an image that has a enhanced contrast.

NB. Note that the resulting image might reduce the actual number of intensity
levels (because of discretization).
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Seljord lake
(Norwegian equivalent
of Loch Ness)

Picture taken with a cell


phone camera (low contrast)
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Seljord lake
(Norwegian equivalent
of Loch Ness)

Picture taken with a cell


phone camera (low contrast)

Before and after


histogram equalization
© 1992–2008 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Seljord lake
(Norwegian equivalent
of Loch Ness)

Picture taken with a cell


phone camera (low contrast)

Before and after


histogram equalization
© 1992–2008 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Matlab command
for histogram Faces
equalization: on
Mars
histeq
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Not in all cases


contrast
enhancement
gives a visual
enhancement
of the picture

(Enhancement is
a visual property,
hence partly
subjective).
For landscapes, it works
generally
well. For portraits,
depends
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

dark image

light image

low contrast

high contrast
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Histogram matching

As we have seen, the histogram equalization does not necessarily


“enhance” equally well all the images.

Example: the face

Assume that we have found the “winning formula”, that is, a


histogram that works well for enhancing a given type of images.

We might wish to apply an intensity transformation to match the


prescribed histogram (probability density distribution).
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

pr(r) pz(z)
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

equidistribution

pr(r) pz(z)
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

equidistribution

T (r)

pr(r) pz(z)
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

equidistribution

T (r) G(z)

pr(r) pz(z)
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

equidistribution

T (r) G(z) G−1(s)

pr(r) pz(z)
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

equidistribution

T (r) G(z) G−1(s)

pr(r) pz(z)
z = G−1 (s) = G−1 (T (r))

Therefore, to match the histograms we have to compute the


equidistribution of the probability density of first image and then the
inverse using the equidistribution of the probability density of the
given histogram.
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

equidistribution

T (r) G(z) G−1(s)

pr(r) pz(z)
z = G−1 (s) = G−1 (T (r))

In practice, this requires the computation of the equalization of


the first image and then G−1

For discrete probabilities, it is ``exactly’’ the same.


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Example:

k
s k  =T (r k ) = ( L − 1) ∑ p r (r j )
j=0 G(z0) = 0(0)
s0 = 1
G(z1) = 0(0)
k s1 = 3 (L-1)*cum.prob.
(L − 1)

MN j=0
∑ n j k = 0 , … , L  −  1
s2 = 5 G(z2) = 0(0)
=7*0.15=1.05
(L-1)*cum.prob.
=7*(0.15+0.2)
s3 = 6 G(z3) = 1(1.05) =2.45

G(z4) = 2(2.45)
s4 = 6
G(z5) = 5(4.55)
s5 = 7
G(z6) = 6(5.95)
s6 = 7
s7 = 7 G(z7) = 7(7)
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering
G(z0) = 0(0)

G(z1) = 0(0)

G(z2) = 0(0)

G(z3) = 1(1.05)

G(z4) = 2(2.45)

G(z5) = 5(4.55)

G(z6) = 6(5.95)

G(z7) = 7(7)

Now we can compute the inverse of G for the range of interest.


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering
G(z0) = 0(0)

G(z1) = 0(0)

G(z2) = 0(0)

G(z3) = 1(1.05)

G(z4) = 2(2.45)

G(z5) = 5(4.55)

G(z6) = 6(5.95)

G(z7)
Now we can compute the inverse of G for the range of interest.
= 7(7)

s0 = 1

s1 = 3

s2 = 5

s3 = 6

s4 = 6

s5 = 7

s6 = 7

s7 = 7
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering
G(z0) = 0(0)

G(z1) = 0(0)

G(z2) = 0(0)

G(z3) = 1(1.05)

G(z4) = 2(2.45)

G(z5) = 5(4.55)

G(z6) = 6(5.95)

G(z7) = 7(7)

Now we can compute the inverse of G for the range of interest.


s0 = 1

s1 = 3

s2 = 5

s3 = 6

s4 = 6

s5 = 7

s6 = 7

s7 = 7
© 1992–2008 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering
G(z0) = 0(0)

G(z1) = 0(0)

G(z2) = 0(0)

G(z3) = 1(1.05)

G(z4) = 2(2.45)

G(z5) = 5(4.55)

G(z6) = 6(5.95)

G(z7) = 7(7)

Now we can compute the inverse of G for the range of interest.


s0 = 1

s1 = 3

s2 = 5

s3 = 6

s4 = 6

s5 = 7

s6 = 7

s7 = 7
© 1992–2008 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering
G(z0) = 0(0)

G(z1) = 0(0)

G(z2) = 0(0)

G(z3) = 1(1.05)

G(z4) = 2(2.45)

G(z5) = 5(4.55)

G(z6) = 6(5.95)

G(z7) = 7(7)

Now we can compute the inverse of G for the range of interest.


s0 = 1

s1 = 3

s2 = 5

s3 = 6

s4 = 6

s5 = 7

s6 = 7

s7 = 7
© 1992–2008 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering
G(z0) = 0(0)

G(z1) = 0(0)

G(z2) = 0(0)

G(z3) = 1(1.05)

G(z4) = 2(2.45)

G(z5) = 5(4.55)

G(z6) = 6(5.95)

G(z7) = 7(7)

Now we can compute the inverse of G for the range of interest.


s0 = 1

s1 = 3

s2 = 5

s3 = 6

s4 = 6

s5 = 7

s6 = 7

s7 = 7
© 1992–2008 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering
G(z0) = 0(0)

G(z1) = 0(0)

G(z2) = 0(0)

G(z3) = 1(1.05)

G(z4) = 2(2.45)

G(z5) = 5(4.55)

G(z6) = 6(5.95)

G(z7) = 7(7)

Now we can compute the inverse of G for the range of interest.


s0 = 1

s1 = 3

s2 = 5

s3 = 6

s4 = 6

s5 = 7

s6 = 7

s7 = 7
© 1992–2008 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering
G(z0) = 0(0)

G(z1) = 0(0)

G(z2) = 0(0)

G(z3) = 1(1.05)

G(z4) = 2(2.45)

G(z5) = 5(4.55)

G(z6) = 6(5.95)

G(z7) = 7(7)

Now we can compute the inverse of G for the range of interest.


s0 = 1

s1 = 3

s2 = 5

s3 = 6

s4 = 6

s5 = 7

s6 = 7

s7 = 7
© 1992–2008 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering
G(z0) = 0(0)

G(z1) = 0(0)

G(z2) = 0(0)

G(z3) = 1(1.05)

G(z4) = 2(2.45)

G(z5) = 5(4.55)

G(z6) = 6(5.95)

G(z7) = 7(7)

Now we can compute the inverse of G for the range of interest.


s0 = 1

s1 = 3

s2 = 5

s3 = 6

s4 = 6

s5 = 7

s6 = 7

s7 = 7
© 1992–2008 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering
G(z0) = 0(0)

G(z1) = 0(0)

G(z2) = 0(0)

G(z3) = 1(1.05)

G(z4) = 2(2.45)

G(z5) = 5(4.55)

G(z6) = 6(5.95)

G(z7) = 7(7)

Now we can compute the inverse of G for the range of interest.


s0 = 1

s1 = 3

s2 = 5

s3 = 6

s4 = 6

s5 = 7

s6 = 7

s7 = 7
© 1992–2008 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering
G(z0) = 0(0)

G(z1) = 0(0)

G(z2) = 0(0)

G(z3) = 1(1.05)

G(z4) = 2(2.45)

G(z5) = 5(4.55)

G(z6) = 6(5.95)

G(z7) = 7(7)

Now we can compute the inverse of G for the range of interest.


s0 = 1

s1 = 3

s2 = 5

s3 = 6

s4 = 6

s5 = 7

s6 = 7

s7 = 7
© 1992–2008 R. C. Gonzalez & R. E. Woods
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering
G(z0) = 0(0)

G(z1) = 0(0)

G(z2) = 0(0)

G(z3) = 1(1.05)

G(z4) = 2(2.45)

G(z5) = 5(4.55)

G(z6) = 6(5.95)

G(z7) = 7(7)

Now we can compute the inverse of G for the range of interest.


s0 = 1

s1 = 3

s2 = 5

s3 = 6

s4 = 6

s5 = 7

s6 = 7

s7 = 7
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Since we are working with integer number, because of rounding


the computed transformation does not give an exact histogram
match.

However, there will be a tendency to move the intensity values to


have a similar match.
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Pollen image Pollen image


(reference img)
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Pollen image Pollen image


(reference img)
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Pollen image Pollen image


(reference img)
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

How to do the histogram matching in Matlab?

J=histeq(I) : histogram equalization of the image I

J=histeq(I, HIST) : creates J from I by intensity


transformation so that the histogram of J and HIST match.
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

The histogram transformations that we have seen up to this point are


global, in the sense that they act on the whole image.

In some applications, one might wish to apply different


transformations to different regions of the image

Local histogram processing


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Further image statistics (normalized quantities):

Definitions:
Average intensity:
𝐿 −1
m =  ∑ 𝑟 𝑖 𝑝(𝑟 𝑖 ) (also called mean intensity)
𝑖=1
nth moment:
𝐿 −1
𝜇𝑛 =  ∑ (𝑟 𝑖 −𝑚) 𝑝(𝑟 𝑖 )
𝑛

𝑖=1

Second moment:
(this is the intensity
𝐿 −1
𝜇 2 =  ∑ (𝑟 𝑖 − 𝑚)2 𝑝(𝑟 𝑖 ) variance, and equals
𝑖=1
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

The mean m (average) and the variance can be computed directly from the
image data,
𝑀 −1 𝑁 −1
1
m= ∑
𝑀𝑁 𝑥=0
∑ 𝑓 (𝑥 , 𝑦)
𝑦 =0

𝑀 −1 𝑁 −1
1
𝜎=2
𝑀𝑁
∑ ∑ ( 𝑓 ( 𝑥 , 𝑦 ) − 𝑚) 2

𝑥=0 𝑦 =0

It is an easy exercise to see that the values computed in this manner


are the same obtained with the probability density function.
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

The previous definitions of mean, average, moments,


can easily be adapted to a subregion of an image (instead of the full
one).
𝑀 −1
1 1
𝑚𝑠 𝑥𝑦 =
𝑀𝑁
∑ 𝑟 𝑖 𝑝𝑠 𝑥𝑦 ( 𝑟 𝑖 ) = ∑
|𝑆 𝑥𝑦| (𝑠,𝑡)∈𝑆
𝑓 (𝑠,𝑡)
𝑥=0 𝑥𝑦

𝐿−1
1
𝜎 = ∑ (𝑟 𝑖 −𝑚𝑠 𝑥𝑦 ) 𝑝𝑠 𝑥𝑦 ( 𝑟 𝑖 )= ∑
2 2 2
( 𝑓 ( 𝑠,𝑡 ) −𝑚𝑠 𝑥𝑦 )
𝑖=1 |𝑆 𝑥𝑦|(𝑠 ,𝑡)∈𝑆 𝑥𝑦
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

Example: we have an image, we are satisfied from parts of it


(foregreound), want to enhance other parts (background)
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

© 1992–2008 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering
Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

Fundaments of spatial filtering

Filtering = sieving, removing unwanted components

Image filtering adapts and expands


techniques already used in signal
processing.

Filtering is accomplished directly


on the image using masks (kernels,
templates, windows)

© 1992–2008 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

We have already seen an example of spatial filtering

(blurring by 9 pixels averaging (D8))

This is a special example


of linear filter
as the result (response) is
obtained as a linear
combination of the input
image pixel values.

© 1992–2008 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

We have already seen an example of spatial filtering

(blurring by 9 pixels averaging (D8))

This is a special example


of linear filter
as the result (response) is
obtained as a linear Example of a blurred
image by a 3x3
combination of the input mask
image pixel values.

© 1992–2008 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial Filtering

A generic D 8 linear filter will have the following expression


g(x, y) = w(−1, −1)f (x − 1,y − 1) + w(−1, 0)f (x − 1, 0) +
·· ·
+ w(0, 0)f (x, y) + ·· · + w(1, 1)f (x + 1,y +
or, for a generic mask using
1) mn pixels,
𝑎 𝑏
g( x ,y) = ∑ ∑ 𝑤(𝑠,𝑡) 𝑓 (𝑥+𝑠 , 𝑦+1)
𝑠=− 𝑎 𝑡=− 𝑏
where
m = 2a + n = 2b + 1
1,

© 1992–2008 R. C. Gonzalez & R. E. Woods


Digital Image Processing, 3rd ed.
Gonzalez & Woods
www.ImageProcessingPlace.com

Chapter 3
Intensity Transformations & Spatial
Filtering

2 important filter concepts: Spatial correlation and convolution

Correlation: is the process of moving a filter mask over an image and


computing the sum of the (pointwise) product.

𝑎 𝑏
g( x ,y) = ∑ ∑ 𝑤(𝑠,𝑡) 𝑓 (𝑥+𝑠 , 𝑦+1)
𝑠=− 𝑎 𝑡=− 𝑏

Convolution: mechanically the same (except


for the mask, rotated by 180 degrees).
𝑎 𝑏
g( x ,y) = ∑ ∑ 𝑤(𝑠,𝑡) 𝑓 (𝑥 − 𝑠, 𝑦 −1)
𝑠=− 𝑎 𝑡=− 𝑏

© 1992–2008 R. C. Gonzalez & R. E. W oods

You might also like