You are on page 1of 37

Digital Image Processing

Dr Mai Kamal DIP 2022 1

By

Lecturer, Computer Science Department,


Faculty of Computers and Artificial Intelligence,
Benha University

Dr Mai Kamal DIP 2022 2


Digital Iamge Processing Course

Dr Mai Kamal DIP 2022 3

Digital Iamge Processing


Course

Teams Link
https://teams.microsoft.com/l/team/19%3az6X9_t1JI66-
A0ZBSXtqyEeY4MaCpjrEIIm2ouxKVuU1%40thread.tacv2/conversations
?groupId=5376f567-69ce-4117-92c0-
21871798fba1&tenantId=ff4a48d6-4b5e-4fd3-8266-7eafc3e6e23e

Dr Mai Kamal DIP 2022 4


Grading Formula

Weights of the Assessments


Final Term Examination 50
Final Oral Exam 10
Midterm Exam 15
Practical Exam 15
Semester Work 10

Dr Mai Kamal DIP 2022 5

Text Book

R. Gonzalez, R. Woods.
Digital Ιmage Processing,
Prentice Hall, 2008.
Third Edition .

Dr Mai Kamal DIP 2022 6


Image Enhancement in the Spatial Domain

Dr Mai Kamal DIP 2022 7

Histogram Processing
There are two Methods of Enhancing
Contrast:
1. The first one is called Histogram
Stretching that increase contrast.
2. The second one is called Histogram
Equalization that improve contrast.
3. The third one is called Histogram
(Specification) Matching that
improve contrast.

Dr Mai Kamal DIP 2022 10


Histogram Processing
 an image's histogram is transformed
according to a desired function.
 Transforming the intensity values so that
the histogram of the output image
aapproximately matches a specified
histogram.

Dr Mai Kamal DIP 2022 11

Histogram Processing
 Histogram
h(rk )  nk
– where rk is the kth gray level and n k is the
number of pixels in the image having gray
level rk
– Normalized histogram
p(rk )  nk / n
Histogram Processing
 Histogram equalization
s  T (r ), 0  r  1
r  T 1 ( s ), 0  s  1

Histogram Processing
Histogram Specification (HS)
 Histogram Matching (Histogram Specification)
is the method used to generate a processed
image that has a specified histogram.
 let 𝑝𝑟 (𝑟) and 𝑝𝑧 (𝑧) denote their corresponding
continuous probability density functions.
 In this notation, 𝑟 and 𝑧 denote the gray levels
of the input and output images, respectively.

Dr Mai Kamal DIP 2022 15

Histogram Matching
 Histogram matching (specification)
r
s  T (r )  ( L  1)  pr ( w)dw
0

z
G ( z )  ( L  1)  p z (t )dt  s
0

z  G 1 (s)  G 1[T (r )]

pz (z) is the desired PDF


Histogram Matching

k k nj
sk  T (rk )  ( L  1) pr (rj )  ( L  1) , k  0,1,2,..., L  1
j 0 j 0 n
k
vk  G ( zk )  ( L  1) p z ( zi ) sk , k  0,1,2,..., L  1
i 0

z k  G 1[T (rk )], k  0,1,2,..., L  1

Histogram Matching

 Histogram Matching Procedure Steps:


– Obtain the histogram of the given image, T(r)
– Precompute a mapped level s k for each level rk
– Obtain the transformation function G from
the given pz (z )
– Precompute z k for each value of s k
– Map rk to its corresponding level s k ; then
map level s k into the final level z k
Histogram specification
histogram1 histogram2

S-1*T

T S

Example
 Suppose that a 3-bit image (𝑳=8) of size 64 × 64
pixels (𝑴𝑵 = 4096) has the intensity distribution
shown in following table, where the intensity
levels are integers in the range [0,𝐿 − 1] = [0, 7].

1. Apply Histogram Matching

Dr Mai Kamal DIP 2022 24


Example
 Sol:
– In the next step, we compute all the values of the
transformation function, 𝐺

Dr Mai Kamal DIP 2022 27

Example

Dr Mai Kamal DIP 2022 28


Example
 Sol:
– Find the corresponding value of 𝑧𝑞 For every value
of 𝑠𝑘 ,

Dr Mai Kamal DIP 2022 29

Example

𝒔𝒌 𝑷𝒌 (𝒔𝒌 )
1 0.19
3 0.25
5 0.21
6 0.16+0.08
=0.24
7 0.06+0.03
+0.02
=0.11

Dr Mai Kamal DIP 2022 30


Histogram Matching

Neighbourhood Processing

Dr Mai Kamal DIP 2022 32


Neighbourhood Processing
 Image can be modified by applying a particular
function to each pixel value.
 Neighbourhood processing may be considered
as an extension of this, where a function is
applied to a neighbourhood of each pixel.
 The idea applied by moving a “mask”: a
rectangle (usually with sides of odd length) or
other shape over the given image.
 The combination of mask and function is
called a filter.
Dr Mai Kamal DIP 2022 33

Spatial Mask on Image

Dr Mai Kamal DIP 2022 34


Smoothing Special Filter

Dr Mai Kamal DIP 2022 36

Spatial Filter
 Spatial filtering is also called mask processing.
 Using a filter kernel ( which is a sub-image,
 w(x, y)) to operate on the image f(x,y).

 Spatial filtering
1. Linear spatial filtering.
2. Nonlinear spatial filtering.

Dr Mai Kamal DIP 2022 37


Smoothing Spatial Filters
 Smoothing Linear Filters
– Noise reduction
– Smoothing of false contours
– Reduction of irrelevant detail

Smoothing Spatial Filters


 Smoothing filters are used for
- blurring
- noise reduction.
 Blurring is used in preprocessing steps, such as removal
of small details from an image.

 There are two ways of smoothing spatial filters


- Smoothing linear filters - operations performed on
image pixel – i.e. Lowpass filtering
- Smoothing nonlinear (Order-Statistic) filters - based on
ranking (ordering) the pixels – i.e. Median filter, Max
filter, Min filter

Dr Mai Kamal DIP 2022 39


Smoothing Linear Filters (cont.)
 Smoothing linear filter
Example

Linear Filter
 If the function by which the new grey value is
calculated is a linear function of all the grey
values in the mask, then the filter is called a
linear filter.
 A linear filter can be implemented by
multiplying all elements in the mask by
corresponding elements in the neighbourhood,
and adding up all these products.

Dr Mai Kamal DIP 2022 43


Spatial Mask on Image
The mask is 3x5

 Suppose the mask is 3x5.


 The corresponding pixel
values are :

corresponding pixels

Dr Mai Kamal DIP 2022 47

Fundamentals of Spatial
Filtering
 The Mechanics of Spatial Filtering
 The process consists simply of moving the filter
mask from point to point in an image.
 At each point (x,y) the response of the filter at
that point is calculated using
a b
g ( x, y )    w( s, t ) f ( x  s, y  t )
s   at   b
a=(m-1)/2 & b=(n-1)/2
 The response R
R = w(-1, -1)f(x-1, y-1) +w(-
1,0) f(x-1, y)+….+w(0, 0)f(x,
y)+….+w(1,0)f(x+1, y)+
w(1,1)f(x+1, y+1)
Dr Mai Kamal DIP 2022 48
Fundamentals of Spatial
Filtering
 Edges of the image
• Ignore the edges( The output image which is
smaller than the original).
• “Pad” with zeros( The output image of the same
size as the original).

Dr Mai Kamal DIP 2022 49

Fundamentals of Spatial
Filtering
 One important linear filter is to use a 3x3 mask and take
the average of all nine values within the mask. This
operation may be described as follows:

Dr Mai Kamal DIP 2022 52


Smoothing Linear Filters (cont.)
 These filters sometimes are called Averaging filters
or Lowpass filters.
 Smoothing linear spatial filter is simply the average
of the pixels contained in the neighborhood of the
filter mask.
 The idea is replacing the value of every pixel in an
image by the average of the gray levels in the
neighborhood defined by the filter mask.
 This process results in an image with reduced
“sharp” transitions in gray levels.

Dr Mai Kamal DIP 2022 53

Smoothing Linear Filters (cont.)


 Two Filter mask :
1. Averaging filter (all coefficients are equal ).
2. Weighted averaging filter (all coefficients are
different ).

Dr Mai Kamal DIP 2022 56


Averaging Filter

 A major use of averaging filters is in the reduction of


“irrelevant” detail in an image.
 An 𝑚 × 𝑛 mask would have a normalizing constant
1
equal to .
𝑚𝑛
 Its also known as Lowpass filter.
 Spatial averaging filter in which all coefficients are
equal is sometimes called a box filter.

Averaging Filter

1 9
R   zi
9 i 1
Weighted Averaging Filter

a b

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

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

where x=0, 1, 2, p , 𝑀-1 and y=0, 1, 2, p , 𝑁-1.

Fundamentals of Spatial
Filtering
 Example : Apply average filter on the following
image.

Dr Mai Kamal DIP 2022 60


Fundamentals of Spatial
Filtering
 Example : Apply average filter on the following
image.

Dr Mai Kamal DIP 2022 61

Weighted Average Filter


Example
1 2 1

2 4 2

1 2 1

150.187 150.125

149.687 149.687

Dr Mai Kamal DIP 2022 62


Spatial Correlation and
Convolution
 Correlation is the process of moving a filter mask over the
image and computing the sum of products at each
location, exactly as explained.
 The correlation of a filter w(x,y) of size m×n with an
image f(x,y), denoted as,
a b
w( x, y ) f ( x, y )    w(s, t ) f ( x  s, y  t )
s  a t  b

Dr Mai Kamal DIP 2022 63

Spatial Correlation and


Convolution
 spatial filtering is spatial convolution.
 The method for performing a convolution is the same as
that for filtering, except that the filter must be rotated by
180° before multiplying and adding.
 Convolution is the same, except that the filter is first
rotated by 180°.
 The convolution of a filter w(x,y) of size m×n with an
image f(x,y), denoted as,
a b
w( x, y ) f ( x, y )    w(s, t ) f ( x  s, y  t )
s  a t  b

Dr Mai Kamal DIP 2022 65


Spatial Correlation and Convolution
 Example:

 1-D correlation and convolution of a filter with a


discrete unit impulse

Dr Mai Kamal DIP 2022 66

Spatial Correlation and Convolution


 Example:

 1-D correlation and convolution of a filter with a


discrete unit impulse
 by rotating the mask by 180°.

Dr Mai Kamal DIP 2022 69


Spatial Correlation and Convolution
 Example1: 1-D correlation and convolution of a
filter with a discrete unit impulse

Dr Mai Kamal DIP 2022 71

Spatial Correlation and


Convolution
 Example: 2D correlation and convolution
0 0 0 0 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 2 3
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 4 5 6
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 7 8 9
0 0 1 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0

Dr Mai Kamal DIP 2022 73


Spatial Correlation and
Convolution
 Example: 2D correlation and convolution
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 1 2 3 0 0 0
0 0 0 4 5 6 0 0 0
0 0 0 7 8 9 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0

Dr Mai Kamal DIP 2022 75

Spatial Correlation and


Convolution
 Example: 2D correlation and convolution
0 0 0 0 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 9 8 7
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 6 5 4
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 3 2 1
0 0 1 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0

Dr Mai Kamal DIP 2022 76


Spatial Correlation and
Convolution
 Example: 2D correlation and convolution
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 9 8 7 0 0 0
0 0 0 6 5 4 0 0 0
0 0 0 3 2 1 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0

Dr Mai Kamal DIP 2022 77

Spatial Correlation and Convolution


 2-D correlation and convolution

Dr Mai Kamal DIP 2022 78


Representing the Special Filter
 Vector Representation of Linear Filtering

Representing the Special Filter


 Vector Representation of Linear Filtering

R  w1 z1  w2 z2  ...  w9 z9
9
  wi zi
i 1
The mask 3x3

z z z
z z z Pixels behind the mask
3x3
z z z
Separable filters
 Some filters can be implemented by the successive
application of two simpler filters.
 The 3x3 averaging filter can be implemented by
first applying a 3x1averaging filter, and then
applying a 1x3 averaging filter to the result.
 The 3x3 averaging filter is thus separable into two
smaller filters.
 Separability can result in great time savings.
Suppose an nxn filter is separable into two filters
of size nx1 and 1xn.

Dr Mai Kamal DIP 2022 81

Separable filters

 Example

Dr Mai Kamal DIP 2022 82


Smoothing Linear Filters (cont.)
 Smoothing linear filter Example

Smoothing Nonlinear Filters


(Order-Statistic Filters)

 Order-statistic filters are smoothing nonlinear


spatial filters.
 It is based on ordering (ranking) the pixels
contained in the image .
 It replacing the value of the center pixel with the
value determined by the ordering result
 The filter selects a sample from the window, does
not average.
 Best suited for “salt and pepper” noise.

Dr Mai Kamal DIP 2022 85


Smoothing Nonlinear Filters
(Order-Statistic Filters)

 Very effective for removing “salt and pepper”


noise (i.e., random occurrences of black and white
pixels).

Dr Mai Kamal DIP 2022 86

Smoothing Nonlinear Filters


(Order-Statistic Filters)

 Different types of order-statics filters


are:
 Minimum filters (suited for salt noise)
 Maximum filters (suited for pepper noise)
 Median filters (suited for salt and pepper
noise)

Dr Mai Kamal DIP 2022 87


Smoothing Nonlinear Filters
(Order-Statistic Filters)

 Median Filter:
fˆ ( x, y )  median{g ( s, t )}
( s ,t )S xy

 Max Filter:
fˆ ( x, y )  max {g ( s, t )}
( s ,t )S xy

 Min Filter:
fˆ ( x, y )  min {g ( s, t )}
( s ,t )S xy

Dr Mai Kamal DIP 2022 88

Non-linear Filter
 Linear filters are easy to describe, and can be
applied very quickly and efficiently by Matlab.
 A non-linear filter is obtained by a non-linear
function of the greyscale values in the mask.
 rank-order filters.
1. maximum filter
2. minimum filter

Dr Mai Kamal DIP 2022 89


Non-linear Filter
 In such a filter, the elements under the mask are
ordered, and a particular value returned as output.
 So if the values are given in increasing order, the
minimum filter is a rank-order filter for which the first
element is returned, and
 the maximum filter is a rank-order filter for which the
last element is
returned.

Dr Mai Kamal DIP 2022 90

Median Filter
 If the objective is to achieve noise reduction instead of
blurring, this method should be used.
 This method is particularly effective when the noise
pattern consists of strong, spike-like components and
the characteristic to be preserved is edge sharpness.
 It is a nonlinear operation.
 Three steps to be followed to run a median filter:
1. Consider each pixel in the image.
2. Sort the neighboring pixels into order based upon
their intensities.
3. Replace the original value of the pixel with the median
value from the list.

Dr Mai Kamal DIP 2022 91


Median Filter

Dr Mai Kamal DIP 2022 92

Median Filter

Dr Mai Kamal DIP 2022 93


Median Filter Example

Dr Mai Kamal DIP 2022 96

Median Filter Example

Dr Mai Kamal DIP 2022 97


Comparison between Median
Filter and Averaging Filter

Dr Mai Kamal DIP 2022 98

Next Lecture
LEC.6: SPECIAL FILTER :
SHARPING

Dr Mai Kamal DIP 2022 100


Dr Mai Kamal DIP 2022 101

You might also like