You are on page 1of 76

Computer Vision and Image Processing:

CoSc4113
Chapter 3

Spatial Domain Image Processing

Wolaita Sodo University


Department of Computer Science
Mr. Tsega Asresa(2023)
3.1 Background
3.1.1 Spatial Domain vs Transform Domain
► Spatial domain (p127)
This method directly processes the intensity values or pixel values of
the image plane and its results can immediately be interpreted by
humans.

► Transform domain
This method transforms the input images into a transform domain,
processes some operation in the domain, and applies the inverse
transform to return to the spatial domain as shown in belows
diagram:Fourier Inverse Fourier
Transform Transform

2
3.1.1 Spatial Domain vs Transform Domain

g ( x, y )  T [ f ( x, y )]) (3.1-1)
f ( x, y ) : input image Spatial – Transform
g ( x, y ) : output image domain domain

T : an operator on f defined over


a neighborhood of point ( x, y )
- The operator T can be a Fourier
Transformation in this book. The output
of T is much different from input image
and the shape in the input image
completely changed.

3
Spatial Domain Process

4
Spatial Domain Process
- Contrast stretching, Thresholding

Intensity transformation function


s  T (r )

Contrast stretching Thresholding


function function
5
3.2 Basic Intensity Transformation Functions

Intensity transformation
function
6
3.2.1 Image Negatives (p130)

Image negatives
intensity level s ( 0 ~ 255)

s  L 1 r
Output image –

r= input intensity level


s= output intensity level
L-1 = maximum intensity level
= 256 -1 = 255

r=0  s= (256-1)-0 =255


r=128  s=(256-1)-128 =127
r=255  s=(256-1)-255 = 0

Input image –
intensity level r ( 0 ~ 255)
7
Image Negatives

Image negatives
s  L 1 r

8
Example: Image Negatives
Ex:
w w=686

h
Ex:
h=790

Small
lesion

9
3.2.2 Log Transformations (p131)

Log Transformations
s  c log(1  r )

10
Example: Log Transformations

11
3.2.3 Power-Law (Gamma) Transformations


s  cr

12
Power-Law (Gamma) Transformations
void DibGammaCorrection(CDib& dib, float gamma)
{ register int i, j;
float invgamma = 1.f / gamma;

int w = dib.GetWidth(); int h = dib.GetHeight();

BYTE** ptr = dib.GetPtr();


for( j = 0 ; j < h ; j++ )
for( i = 0 ; i < w ; i++ )
{ ptr[j][i] = (BYTE)limit(pow((ptr[j][i]/255.f), invgamma)*255 + 0.5f); }
}


s  cr
13
Example: Gamma Corrections - p134

s=r2.5

s=r0.5

s=r2.5

14
Example: Gamma Transformations
Cathode ray tube
(CRT) devices have an
s=r2.5 intensity-to-voltage
response that is a
power function, with
exponents varying
from approximately
s  r1/2.5 1.8 to 2.5

s=r0.4
s=r2.5

15
Example: Gamma Transformations
s=r0.6

(a) original

16

s=r0.4 s=r0.3
Example: Gamma Transformations

s=r3.0

s=r4.0

s=r5.0
17
Piecewise-Linear Transformations
► Contrast Stretching
— Expands the range of intensity levels in an image so that it spans
the full intensity range of the recording medium or display device.

► Intensity-level Slicing
— Highlighting a specific range of intensities in an image often is of
interest.

18
pollen 꽃가루 , x700
19
Highlight the major
blood vessels and
study the shape of the
flow of the contrast
medium (to detect
blockages, etc.)

Measuring the actual


flow of the contrast
medium as a function
of time in a series of
images 20
Bit-plane Slicing

21
Bit-plane Slicing

22
Bit-plane Slicing
Bit 8, 7 Bit 8, 7, 6 Bit 8, t, 6, 5

23
► Experiment 3.1

► Select a image and implement:

 image negative

 Log and gamma transformation

 Contrast stretching

 Bit plane slicing

24
3.3 Histogram Processing (p142)
► 3.3.1 Histogram Equalization

► 3.3.2 Histogram Matching

► 3.3.3 Local Histogram Processing

► 3.3.4 Using Histogram Statistics for Image Enhancement

25
Histogram Processing

Histogram h( rk )  nk n0 n1 n2
0 1 2 3 ..... 254 255

rk is the k th intensity value


nk is the number of pixels in the image with intensity rk

nk
Normalized histogram p( rk ) 
MN
nk : the number of pixels in the image of
size M  N with intensity rk

26
Histogram Processing

0 1 2 3 ..... 254 255

29
30
3.3.1 Histogram Equalization
The intensity levels are assumed in the interval [0, L-1].  ex. [0, 255]
r : input variable – intensity level of input image, s: the output variable for output image
Relation between r and s is transformation function T. Then ..
s = T (r) 0<= r <= L -1 (3.3-1)
that produce an output intensity level s for every pixel in input image having intensity r. We
assume that:
(a) T( r) is a “monotonically increasing function” in 0 <= r <=L-1; and
(b) 0 <= T( r) <= L-1 for 0 <= r <=L-1
Then, We use the inverse
r = T-1 ( s) 0<= s <=L-1 (3.2-2)
In which case we change the condition (a) to (a’) :
(a’) T( r) is a “strictly monotonically increasing function” in 0 <= r <=L-1;
Condition (a’) guarantees that the mapping from s back to r will be one-to-on, preventing ambiguities.
(page 144)
T(r)
T(r)

r r
31
Histogram Equalization
3.3.1 histogram equalization
• Probability theory: (page 145)
If pr(r) and T(r) are known, and T(r) is continuous and differentiable
over the range of values of interest,
then the PDF of the transformed (mapped) variable s can be obtained
using: dr
p s ( s )  pr ( r ) (3.3  3)
ds
• The PDF of the output intensity variable s is determined by the PDF of input r and
the transformation function [ recall that r and s are related by T( r)].
r
s  T (r )  ( L  1)  pr ( w)dw (3.3  4)
0
The right side of this equation is CDF (Cumulative distribution function)
of random variable r.
1) Transformation function of e.q. 3.3-4 satisfies condtion (a)
because the area under the function cannot decrease as r increases
2) When the upper limit of this eq. is r=(L-1), the integral evaluates to 1,
this satisfies condition (b)

32
Histogram Equalization (page 146)

By the Leibniz’s rule in calulus,


• From e.q. 3.3.-1 and 3.3-4:
S= T(r) 0<= r <= L – 1 (3.3-1)
ds dT ( r ) dr
 p s ( s )  pr ( r ) (3.3  3)
dr dr ds
d  r
 ( L  1)  pr ( w) dw r
s  T (r )  ( L  1)  pr ( w)dw (3.3  4)
dr  0  0

 ( L  1) pr ( r ) (3.3  5)

•From e.q. 3.3.-3:


dr
ps ( s )  pr ( r )
ds
1
 pr ( r ) (3.3  6)
( L  1) pr (r )
1
 0  s  L 1
L 1

We see that as (eq. 3.3-6) shows, the resulting ps(s) always is uniform,
33
independently of the form of pr(r). Fig 3.18 ilustrates these concepts:
Histogram Equalization
Exercise 3.4: Illustration of eqs. (3.3-4) and (3.3-6)
• Suppose that the intensity values in an image have the PDF
 2r

pr ( r )   ( L  1) 2 for 0  r  L 1

 0 otherwise r
• From eq. (3.3-4) 2
r
s  T (r )  ( L  1)  pr ( w)dw 
2 r 2 w r
2

L  1 0
w dw  
L 1 L 1
2
0

• From eq. (3.3-6) ps ( s )  pr ( r )


dr 0

ds
1


2r  dr 
( L  1) 2 
 ds 

1

2r  d r  2

( L  1) 2  dr L 1
 
2r L 1

( L  1) 2 2r
1

L 1 34

• As expected, the result ps (s) is a uniform PDF and independent of the form of p r(r)
Histogram Equalization
Exercise 3.4: Illustration of eqs. (3.3-4) and (3.3-6)
• Suppose that the intensity values in an image have the PDF
 2r

pr ( r )   ( L  1) 2 for 0  r  L 1

 0 otherwise r
• From eq. (3.3-4) 2
r
s  T (r )  ( L  1)  pr ( w)dw 
2 r 2 w r
2

L  1 0
w dw  
L 1 L 1
2
0

• From eq. (3.3-6) ps ( s )  pr ( r )


dr 0

ds
1


2r  dr 
( L  1) 2 
 ds 

1

2r  d r  2

( L  1) 2  dr L 1
 
2r L 1

( L  1) 2 2r
1

L 1 35

• As expected, the result ps (s) is a uniform PDF and independent of the form of p r(r)
Histogram Equalization (page 148)

nk
Normalized histogram p ( rk ) 
MN
nk : the number of pixels in the image of
size M  N with intensity rk
Continuous case:
r
s  T ( r )  ( L  1)  pr ( w) dw Eq. (3.3-4)
0
The right side of the eq. is recognized as the cumulative distribution function
(CDF) of random variable r.

Discrete values:
k
sk  T (rk )  ( L  1) pr ( rj ) Eq. (3.3-8)
j 0
k nj L 1 k
 ( L  1)   nj k=0,1,..., L-1
j  0 MN MN j  0 36
Example 3.5 : Histogram Equalization ★★★
(page 148)

Suppose that a 3-bit image (L=8) of size 64 × 64 pixels (MN = 4096) has
the intensity distribution shown in following table.
Get the histogram equalization transformation function and give the ps(sk)
for each sk.

37
Example: Histogram Equalization
• Use eq. 3.3-8 for sk :
Discrete values:
k
sk  T (rk )  ( L  1) pr (rj ) Eq. (3.3-8)
j 0

Solution:
0
s0  T (r0 )  7 pr (rj )  7  0.19  1.33 1
j 0

3
1
s1  T ( r1 )  7  pr ( rj )  7  (0.19  0.25)  3.08
j 0

s2  4.55  5 s3  5.67  6
s4  6.23  6 s5  6.65  7
s6  6.86  7 s7  7.00  7
38
Example: Histogram Equalization

39
Histogram Equalization Ex3.5

rk nk pr(rk) sk=T(rk) sk sk ps(sk) rk

r0=0 790 0.19 s0=1.33 → 1 0 ← 0


r1=1 1023 0.25 s1=3.08 → 3 1 ← 0.19 0
r2=2 850 0.21 s2=4.55 → 5 2 ← 0
r3=3 650 0.16 s3=5.67 → 6 3 ← 0.25 1
r4=4 329 0.08 s4=6.23 → 6 4 ← 0
r5=5 245 0.06 s5=6.55 → 7 5 ← 0.21 2
r6=6 122 0.03 s6=6.86 → 7 6 ← 0.24 3,4
r7=7 81 0.02 s7=7 → 7 7 ← 0.11 5,6,7
40
41
Histogram Equalization
 Input image vs Histogram equalized image

“lenna.bmp” image
and histogram

“lenna.bmp” image
after histogram
equalization

and its histogram

43
Histogram Equalization
 Input image vs Histogram equalized image

11.08
“camera man” image
and histogram

“camera man” image


after histogram
equalization

and its histogram

44
Histogram Equalization
 Histogram equalization does not give us always better images.

“child” image and


histogram

“child” image after


histogram
equalization

and its histogram

45
46
Question
Is histogram equalization always good?

No

47
Histogram Matching (Specification)
Histogram matching (histogram specification)
— generate a processed image that has a specified histogram

Let pr ( r ) and pz ( z ) denote the continous probability


density functions of the variables r and z. pz ( z ) is the
specified probability density function.
Let s be the random variable with the probability
r
s  T ( r )  ( L  1)  pr ( w) dw
0

Define a random variable z with the probability


z
G ( z )  ( L  1)  pz (t ) dt  s
0
48
Histogram Matching (Specification)

Equations needed for Histogram Matching :


r
s  T (r )  ( L  1)  pr ( w)dw (3.3-10)
=(3.3-4)
0
z
G ( z )  ( L  1)  pz (t )dt  s (3.3-11)
0

1
z  G (s)  G 1
T (r ) (3.3-11)

49
Histogram Matching: Procedure
1) Obtain pr(r) from the input image and then obtain the values of s
r
s  ( L  1)  pr ( w)dw (3.3-13)
0

2) Use the specified PDF and obtain the transformation function G(z)
z
G ( z )  ( L  1)  pz (t )dt  s (3.3-14)
0

3) Mapping from s to z ----


(3.3-15)

4) inverse transformation process : Obtain the output image by first equalizing the
input image using eq. (3.3-10); the pixel values in this image are the s values.
Perform the inverse mapping z = G-1(s) to obtain the corresponding pixel in
output image. The PDF of the output image will be the specified PDF.

z  G 1 ( s ) (3.3-16) 50
Histogram Matching: Example 3.7 (page 153)

Assuming continuous intensity values, suppose that an image has


the intensity PDF
 2r
 , for 0  r  L -1
pr (r )   ( L  1) 2

 0, otherwise

Find the transformation function that will produce an image
whose intensity PDF is
 3z 2
 , for 0  z  ( L -1)
pz ( z )   ( L  1) 3

 0, otherwise

51
Histogram Matching: Example
1) Find the histogram equalization transformation for the input image

r
s  T (r )  ( L  1)  pr ( w)dw  ( L  1) 
r 2w r2
dw 
0 0 ( L  1) 2
L 1
2) Find the histogram equalization transformation for the specified histogram

z z 3t 2 z3
G ( z )  ( L  1)  pz (t )dt  ( L  1)  dt  s
0 0 ( L  1) 3
( L  1) 2

3) The transformation function


1/3
1/3  2 r
2
 2 1/3
z  ( L  1) s 
2
 ( L  1)   ( L  1)r 
 L  1
4) From 3), get the image z, where the specified PDF can be obtained.

52
Example: Histogram Matching

53
Example: Histogram Matching

Original satellite image:


“ Mars moon” photo
54
Example: Histogram Matching

Original satellite image: Histogram equalized Histogram matching


“ Mars moon” photo image image

55
Local Histogram Processing

11/15
 Define a neighborhood and move its center from pixel to
pixel

 At each location, the histogram of the points in the


neighborhood is computed. Either histogram equalization or
histogram specification transformation function is obtained

 Map the intensity of the pixel centered in the


neighborhood

 Move to the next location and repeat the procedure

56
Local Histogram Processing: Example
(P 161)

57
3.3.4 Using Histogram Statistics
for Image Enhancement

Average Intensity L 1 M 1 N 1
1
m   ri p (ri ) 
MN
  f ( x, y )
x 0 y 0
i 0
L 1
un (r )   (ri  m) n p (ri )
i 0

Variance L 1 M 1 N 1
1
  u2 (r )   (ri  m) p (ri )    f ( x, y )  m 
2 2 2

i 0
MN x 0 y 0

58
Using Histogram Statistics
for Image Enhancement
The nth moment of r about its mean: (from section 2.6.8)
L 1
un (r )   (ri  m) n p (ri )
i 0
Average Intensity m:
L 1 M 1 N 1
1
m   ri p (ri ) 
MN
  f ( x, y )
x 0 y 0
i 0
Variance = the second moment:
L 1 M 1 N 1
1
  u2 (r )   (ri  m) p(ri )    f ( x, y )  m 
2 2 2

MN x 0 y 0
i 0

59
Spatial Filtering
A spatial filter consists of
(a) a neighborhood, and
(b) a predefined operation

- “Filtering” refers to accepting (passing) or rejecting certain frequency


components. “Low pass filter” passes low frequencies.
 Main role is to enhance image quality such as reducing
noises.
- spatial filter: is called also mask, kernel, template, and window.
- There is one-to-one correspondence between linear filters
and filters in frequency domain.
- Linear spatial filtering of an image of size MxN with a filter of
a b
size mxn is given by the expression
g ( x, y )    w(s, t ) f ( x  s, y  t )
s  a t  b
60
Spatial Filtering

Spatial filter
mask

Image section
under filter 61
Spatial Correlation

The correlation of a filter w( x, y ) of size m  n


with an image f ( x, y ), denoted as w( x, y) f ( x, y)
a b
w( x, y ) f ( x, y )    w(s, t ) f ( x  s, y  t )
s  a t  b

a=(m-1)/2, b=(m-1)/2
Ex) an image of size MxN with a filter of size mxn
m=3, n=3  a=(3-1)/2= 2/2=1, b=1

62
Spatial Convolution

The convolution of a filter w( x, y) of size m  n


with an image f ( x, y ), denoted as w( x, y) f ( x, y)

a b
w( x, y ) f ( x, y )    w( s, t ) f ( x  s, y  t )
s  a t  b

s= -1, t= -1,  w(-1,-1) f(x+1, y+1)


-1, 0,  w(-1, 0) f(x+1, y)
-1, 1,  w(-1, 1) f(x+1, y-1)
0, -1,  w( 0,-1) f(x, y+1)
0, 0,  w( 0, 0) f(x, y)
0, 1,  w( 0, 1) f(x, y-1)
1, -1,  w( 1,-1) f(x-1, y+1) 63

1, 0,  w( 1, 0) f(x-1, y)
Smoothing Spatial Filters

 Smoothing filters are used for blurring and for noise


reduction

 Blurring is used in removal of small details and bridging


of small gaps in lines or curves

 Smoothing spatial filters include linear filters and


nonlinear filters.

64
Spatial Smoothing Linear Filters

The general implementation for filtering an M  N image


with a weighted averaging filter of size m  n is given
a b

  w(s, t ) f ( x  s, y  t )
g ( x, y )  s  a t  b
a b

  w(s, t )
s  a t  b

where m  2a  1, n  2b  1.

65
Two Smoothing Averaging Filter Masks

66
Example: Gross Representation of Objects

67
Order-statistic (Nonlinear) Filters

— Nonlinear

— Based on ordering (ranking) the pixels contained in the


filter mask

— Replacing the value of the center pixel with the value


determined by the ranking result

E.g., median filter, max filter, min filter

- median filter: 1) sorting 2) select the value in the


middle
- max filter: find the largest value in the filter section
68
Example: Use of Median Filtering for Noise Reduction

69
Sharpening Spatial Filters

► Foundation

► Laplacian Operator

► Unsharp Masking and Highboost Filtering

► Using First-Order Derivatives for Nonlinear Image


Sharpening — The Gradient

70
Sharpening Spatial Filters: Foundation

► The first-order derivative of a one-dimensional function f(x)


is the difference

f
 f ( x  1)  f ( x)
x

► The second-order derivative of f(x) as the difference

2 f
 f ( x  1)  f ( x  1)  2 f ( x)
x 2

71
72
Sharpening Spatial Filters: Laplace Operator

Image sharpening in the way of using the Laplacian:

g ( x, y )  f ( x, y )  c  2 f ( x, y ) 
where,
f ( x, y ) is input image,
g ( x, y ) is sharpenend images,
c  -1 if  2 f ( x, y ) corresponding to Fig. 3.37(a) or (b)
and c  1 if either of the other two filters is used.

73
74
Unsharp Masking and Highboost Filtering

► Un sharp masking
 Sharpen images consists of subtracting an unsharp (smoothed)
version of an image from the original image
e.g., printing and publishing industry

► Steps
1. Blur the original image

2. Subtract the blurred image from the original

3. Add the mask to the original

75
Unsharp Masking: Demo

76
Example:

Combining
Spatial
Enhancement
Methods

Goal:

Enhance the
image by
sharpening it
and by bringing
out more of the
skeletal detail

77
Example:

Combining
Spatial
Enhancement
Methods

Goal:

Enhance the
image by
sharpening it
and by bringing
out more of the
skeletal detail

78
Thank You

79

You might also like