Chapter 3 - Intensity Transformation and Spatial Filtering (Updated)
Chapter 3 - Intensity Transformation and Spatial Filtering (Updated)
types of neighborhood:
Advantages of negative :
Produces an equivalent of a photographic negative.
Enhances white or gray detail embedded in dark regions especially when the black
areas are dominant in size..
Negative Transformation Function
The negative of an image with intensity levels in the range [0,L-1]
is obtained by using the negative transformation :
s= L-1-r
Image (r)
Example
100 110 90 95
the following matrix represents the pixels values of
an 8-bit image (r) , apply negative transform and 98 140 145 135
find the resulting image pixel values. 89 90 88 85
Input range
This example proves that the log transform can be effective in editing pictures
that have been captured against the light source by digging out contrast
information from the darker regions of an image at the cost of the brighter
segments
Power-Law Transformation
Power-law transformations have the basic form
S=cry
19/07/2020 28
Example: Power-Law Transformation
Piecewise Linear
Transformation Functions
Contrast Stretching and
Thresholding
Contrast Stretching
Contrast can be simply explained as the difference between maximum
and minimum pixel intensity in an image.
The following example shows three 8-bit images with different contrast
level
100 100 100 100 100 120 150 180 115 117 112 118
100 100 100 100 200 220 240 255 130 140 145 150
19/07/2020 32
Contrast Stretching
Contrast stretching is also known as normalization. The
quality of image is enhanced by stretching the range of
intensity values.
Is a process that
expands the range of
intensity levels in an
image so that it spans
the full intensity range
of the display device.
Contrast Stretching
Contrast stretching, which means that the bright
pixels in the image will become brighter and the dark
pixels will become darker, this means : higher
contrast image.
Contrast Stretching
Example:
Input Image: 8-bit image have range [80 – 150]
After Contrast Stretching: the new range becomes [0 – 255]
using the following equation:
(r1, s1)
(r1, s1)
(r1, s1)
(r1, s1)
If f(x,y)>=150; g(x,y)=1
T= If f(x,y)<150; g(x,y)=0
Or simply…
This is called
thresholding,
0 255
T= If r >=150; s =1 and it produces
If r <150; s =0 a binary image!
Thresholding function
(r1,s1)=(m,0) , (r2,s2)=(m,L-1)
m : mean intensity level in the image
Exercise on Contrast Stretching and Thresholding
Exercise:
90 91 99 100
1 1 0 1
0 0 1 1 82 96 85 90
0 0 1 1
0 1 0 0
Piecewise Linear
Transformation Functions
Intensity-Level Slicing and
Bit-Plane Slicing
Intensity-Level Slicing (gray level slicing)
Highlighting a specific range of intensities in an image.
In other words, we segment certain gray level regions from the
rest of the image.
Approach 1 Approach 2
28 29 25 27 28 29 25 27 0 0 0 0
01100100
end
end
figure, imshow(x);
figure, imshow(y);
ENHANCEMENT USING
ARITHMETIC/LOGIC
OPERATIONS
ENHANCEMENT USING
ARITHMETIC/LOGIC OPERATIONS
And/Or Masks
Logical Operations for Masking
Consider the following is a pixel in the image:
10101011 10101011
AND AND Anding with 0’s
Anding with 1’s will 11111111 00000000 will give all zero ,
give same pixel ----------- ----------- so it will hide the
pixel
10101011 00000000
10101011 10101011
OR Oring with 1’s
OR
Oring with 0’s will will give all ones,
00000000 11111111
give same pixel so it will hide the
----------- ----------- pixel
10101011 11111111
Image Subtraction: Example
Image Subtraction: Motion Tracking
HISTOGRAM EQUALIZATION
Histogram
Ch3, lesson 4: histogram
Bright
image
Low
contract
image
High
contrast
image
Histogram
Histogram of a digital image
h(rk) = nk
Where:
rk : kth gray level
nk : # of pixels with having gray level rk
histogram
h(rk) = nk gray levels ھو ﺗﻣﺛﯾل ﻟﻌدد اﻟﺑﻛﺳل ﻓﻲ ﻛل ﻗﯾﻣﺔ ﻟوﻧﯾﺔ ﻣن درﺟﺎت
Where:
rk : kth gray level
nk : # of pixels with having gray level rk
Histogram of the image:
For example: we have 700 pixels having
the intensity value ≈ 160
Histogram Equalization
Histogram Equalization is an image processing technique
used to improve contrast in images .
It accomplishes this by effectively spreading out the most
frequent intensity values, i.e. stretching out the intensity
range of the image.
Histogram equalization
We have this image in matlab called
pout.tif, when we plot its histogram it
is showed like this:
Histogram equalization
histogram equalization : is the process for adjusting image
intensities to enhance contrast.
Im matlab : we use histeq function
Histogram
produces
pixels having
values that are
distributed
throughout
the range
Example: Histogram equalization
Histogram equalization of the image:
Notice that histogram
equalization does not
always produce a
good result
Equalization (mathematically)
g(x) = [(T(X)/n) * L] -1
g(x) = (L/n). T(X) -1
Where,
G(X) : the new image after equalization
L: No of gray levels 2K
n: No of pixels
T(x): cumulative sum of each gray level
Example: Histogram Equalization
Apply histogram equalization to the following
3-bit image: 0 1 1 2 2 3 4 4
n=4*8
L= 23=8 =32 0 1 1 2 2 3 4 4
L/n = 8/32 0 1 1 2 2 3 4 4
= 0.25
0 1 1 2 2 3 4 4
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
8 ﻋدد ال
ﻋدد اﻟﺑﻛﺳﻼت اﻟﻛﻠﻲNo of pixels
graylevel
Example
Spatial filters
Remember that types of neighborhood:
10
19/07/2020 4
Spatial filters
1)Smoothing filters - low pass
2)Sharpening filters – high pass
Spatial filters : Smoothing ( low pass)
Use for:
1)blurring
2) noise reduction.
10
19/07/2020 8
Spatial filters : Smoothing
Standard and weighted Average- example
1
1/9 1/9 1/9
× 1 1 1
≡ 1/9 1/9 1/9
13 12 10 11 12 9
1/9 1/9 1/9
1 1 1 1/9 1/9 1/9
10 11 12 10 11
1/9 1/9 1/9
11 13 200 15 14
10 12 13 13 14
5
10 11 12 13 11
13 12 10 11 12 5 8
1/9 1/9 1/9
10 11 12 10 11
1/9 1/9 1/9
11 13 200 15 14
10 12 13 13 14
10 11 12 13 11
OR
13 12 10 11 12 5 8 7
1/9 1/9 1/9
10 11 12 10 11
1/9 1/9 1/9
11 13 200 15 14
10 12 13 13 14
10 11 12 13 11
12*1/9+ 10*1/9+11*1/9+11*1/9+12*1/9+10*1/9= 7
OR
13 12 10 11 12 5 8 7 7
1/9 1/9 1/9
10 11 12 10 11
1/9 1/9 1/9
11 13 200 15 14
10 12 13 13 14
10 11 12 13 11
13 12 10 11 12 5 8 7 7 5
1/9 1/9 1/9
10 11 12 10 11
1/9 1/9 1/9
11 13 200 15 14
10 12 13 13 14
10 11 12 13 11
(11*1+12*1+10*1+11*1)/9 = 5
Example on the standard average filter
with zero padding
13 12 10 11 12 5 8 7 7 5
1/9 1/9 1/9
10 11 12 10 11 8
1/9 1/9 1/9
11 13 200 15 14
1/9 1/9 1/9
10 12 13 13 14
10 11 12 13 11
(13*1+12*1+10*1+11*1+11*1+11*1+13*1)/9 = 8
Example on the standard average filter
with zero padding
13 12 10 11 12 5 8 7 7 5
1/9 1/9 1/9
10 11 12 10 11 8 32
1/9 1/9 1/9
11 13 200 15 14
1/9 1/9 1/9
10 12 13 13 14
10 11 12 13 11
(13*1+12*1+10*1+10*1+11*1+12*1+11*1+13*1/9+200*1)/9 = 32
Example on the standard average filter
with zero padding
13 12 10 11 12 5 8 7 7 5
1/9 1/9 1/9
10 11 12 10 11 8 32 33
1/9 1/9 1/9
11 13 200 15 14
1/9 1/9 1/9
10 12 13 13 14
10 11 12 13 11
(12*1+10*1+11*1+11*1+12*1+10*1+13*1+200*1/9+15*1)/9 = 33
Example on the standard average filter
with zero padding
13 12 10 11 12 5 8 7 7 5
1/9 1/9 1/9
10 11 12 10 11 8 32 33 33
1/9 1/9 1/9
11 13 200 15 14
1/9 1/9 1/9
10 12 13 13 14
10 11 12 13 11
(10*1+11*1+12*1+12*1+10*1+11*1+200*1+15*1/9+14*1)/9 = 33
Example on the standard average filter
with zero padding
13 12 10 11 12 5 8 7 7 5
1/9 1/9 1/9
10 11 12 10 11 8 32 33 33 8
1/9 1/9 1/9
11 13 200 15 14
1/9 1/9 1/9
10 12 13 13 14
10 11 12 13 11
(11*1+12*1+10*1+11*1+15*1+14*1)/9 = 8
Example on the standard average filter
with zero padding
You have to apply same process till to the end of the image
13 12 10 11 12
5 8 7 7 5
10 11 12 10 11
8 32 33 33 8
11 13 200 15 14
7 32 33 34 9
10 12 13 13 14
1/9 1/9 1/9
7 32 34 34 9
10 11 12 13 11
1/9 1/9 1/9
5 8 8 8 6
13 12 10 11 12
12 11 11 11 11
10 11 12 10 11
12 32 33 33 12
11 13 200 15 14
11 32 33 34 13
10 12 13 13 14
11 32 34 34 13
10 11 12 13 11
10 11 12 12 12
5 x 5 averaging 9 x 9 averaging
15 x 15 averaging 35 x 35 averaging
Spatial filters : Smoothing
order statistics: Median filter
the median filter is based on
ordering (ranking) the pixels ,
110 120 90 130 then replacing the value of a pixel
becomes
by the median of the gray levels in
91 94 98 200 95 the neighborhood of that pixel.
90 95 99 100
Median filters are quite popular
82 96 85 90 because, for certain types of
random noise, they provide
excellent noise reduction +
Steps: less blurring than linear
smoothing filters of similar size.
1. Sort the pixels in ascending order:
90,90, 91, 94, 95, 98, 99, 110, 120 Median filters are particularly
effective in the presence of
2. replace the original pixel value by the median : impulse noise, also called salt-
and-pepper noise because of its
95 appearance as white and black
dots superimposed on an image.
Spatial filters : Smoothing
order statistics: Median filter
use : blurring + reduce salt and pepper noise
Salt noise
151
150 151 155 150 151
151 152
150 151 155 150 151
150 151 155 150 151 151 152 151 151 151
150 151 155 150 151 151 152 151 151 151
150 151 155 150 151 151 152 151 151 151
150 151 155 150 151 151 152 151 151 151
150 151 155 150 151 151 152 151 151 151
150 151 155 150 151 151 152 151 151 151
152 255 153 150 152 152 153 153 152 151
152 255 153 150 152 After applying 152 153 153 152 151
3×3 Median filter
154 155 155 153 153
153 154 155 0 153
14
19/07/2020 1
Spatial filters : Sharpening ( high pass)
Example on a Sharpening:
= +
50 60 90 100 -10 -20 20 10 40 40 110 110
0 10 30 10 0 0
10 20 -20 -10 0
19/07/2020
3.6.2 Using the Second Derivative for Image Sharpening–The
Laplacian
The Laplacian, for a function (image) f(x, y) of two variables, is
defined and given below:
14
19/07/2020 4
Ex: apply the second derivative
20 20 20 20 on the shaded pixel
20 10 10 10
= 20 + 20 + 10 + 10 − 4 ∗ 10
20 10 10 10
= 20
20 10 10 10
= 10 + 10 + 10 + 10 − 4 ∗ 10
=0
Spatial filters : Sharpening ( high pass)
1.LAPLACE
2.Unsharp
3.High boost
4.sobel
2nd Derivative for Image Sharpening–The
Laplacian
2nd Derivative for Image Sharpening–The
Laplacian
Spatial filters : Sharpening
1) LAPLACE (2nd derivative)
The Laplacian (Better Implementation)
Composite Laplacian Filter
( , )= , − + , − − , − , + − ( , − )
, = , + ( ,
, = , + , − + , − − , − , + − ( , − )
, = , − + , − − , − , + − ( , − )
The Laplacian
(more practical
masks)
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge
= +
50 60 90 100 Laplacian -10 -20 20 10 40 40 110 110
Composite
50 60 90 100 Laplacian 40 40 110 110
0 -1 0
= +
50 60 90 100 -10 40
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100
50 60 90 100
= 4 ∗ 50 − 50 − 50 − 60 − 50 , = 50 − 10
= 200 − 210
, = 40
= −10
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge
0 -1 0
= +
50 60 90 100 -10 -20 40 40
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100
50 60 90 100
= 4 ∗ 60 − 50 − 60 − 60 − 90 , = 60 − 20
= 240 − 260
, = 40
= −20
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge
0 -1 0
= +
50 60 90 100 -10 -20 20 40 40 110
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100
50 60 90 100
= 4 ∗ 90 − 90 − 90 − 60 − 100 , = 90 + 20
= 360 − 340
, = 110
= 20
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge
0 -1 0
= +
50 60 90 100 -10 -20 20 10 40 40 110 110
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100
50 60 90 100
= +
50 60 90 100 -10 -20 20 10 40 40 110 110
0 -1 0
50 60 90 100 -10 40
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100
= 4 ∗ 50 − 50 − 50 − 50 − 60 , = 50 − 10
= 200 − 210
, = 40
= −10
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge
= +
50 60 90 100 -10 -20 20 10 40 40 110 110
0 -1 0
50 60 90 100 -10 -20 40 40
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100
= 4 ∗ 60 − 50 − 60 − 60 − 90 , = 60 − 20
= 240 − 260
, = 40
= −20
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge
= +
50 60 90 100 -10 -20 20 10 40 40 110 110
0 -1 0
50 60 90 100 -10 -20 20 40 40 110
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100
= 4 ∗ 90 − 90 − 90 − 60 − 100 , = 90 + 20
= 360 − 340
, = 110
= 20
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge
= +
50 60 90 100 -10 -20 20 10 40 40 110 110
0 -1 0
50 60 90 100 -10 -20 20 10 40 40 110 110
-1 4 -1
50 60 90 100
0 -1 0
50 60 90 100
= +
50 60 90 100 -10 -20 20 10 40 40 110 110
0 -1 0
0 -1 0
50 60 90 100 40
-1 5 -1
50 60 90 100
0 -1 0
50 60 90 100
50 60 90 100
( , ) = 5 ∗ 50 − 50 − 50 − 60 − 50
= 250 − 210
= 40
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge
0 -1 0
50 60 90 100 40 40
-1 5 -1
50 60 90 100
0 -1 0
50 60 90 100
50 60 90 100
( , ) = 5 ∗ 60 − 50 − 60 − 60 − 90
= 3000 − 260
= 40
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge
0 -1 0
50 60 90 100 40 40 110
-1 5 -1
50 60 90 100
0 -1 0
50 60 90 100
50 60 90 100
( , ) = 5 ∗ 90 − 90 − 90 − 60 − 100
= 450 − 340
= 110
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge
0 -1 0
Ramp
edge
Step
edge
Ramp
edge 5 5 5 6 7 8 9 9 9
1st 0 0 1 1 1 1 0 0
derivative
2nd
derivative
0 0 1 0 0 0 -1 0
Spatial filters : Sharpening
1st VS 2nd derivative sharpening
1st derivative sharpening produces thicker edges in an image
1st derivative sharpening has stronger response to gray level
change
17
19/07/2020 5
17
19/07/2020 6
Example
Example: Apply Unsharp filter on the following blurred edge
= −
50 60 90 100 Std. avg 53 67 83 97 -3 -7 7 3
Z1 Z2 Z3 Z1 Z2 Z3
Z4 Z5 Z6 Z4 Z5 Z6
-1 0 0 -1
Z7 Z8 Z9 Z7 Z8 Z9
0 1 1 0
Gx = Z9 -Z5 Gy = Z8 –Z6
Z1 Z2 Z3 Z1 Z2 Z3
-1 -2 -1 -1 0 1
Z4 Z5 Z6 Z4 Z5 Z6
0 0 0 -2 0 2
Z7 Z8 Z9 Z7 Z8 Z9
1 2 1 -1 0 1
Gx = -Z1-2Z2-Z3+Z7+2Z8+Z9 Gx = -Z1-2Z4-Z7+Z3+2Z6+Z9
Gradient=|(Z7+2Z8+Z9)–( Z1+2Z2+Z3)|+|(Z3+2Z6+Z9)–(Z1+2Z4+Z7)|
Sobel Operators
Gx(P2) Gy(P2)
Gx(p2) = -10-20-200+0+0+0+10+20+200 = 0
Gx(P3) Gy(P3)
3 = +
3 = 0 + 360 = 360
The Gx here is non-zero because P3 is located on a horizontal edge
Example: Find the gradient of the following shaded pixels:
Gx(P4) Gy(P4)
Gx(P4) = -10-20-200+0+0+0+100+200+20 = 90
Gy(P4) = -10+0+200-20+0+400-100+0+20 = 490
4 = +
4 = 90 + 490 = 580
The Gx and Gy here are non-zero because P4 is located on a
horizontal and vertical edges
1st Derivatives of Enhancement – The
Gradient
Combining spatial Enhancement
Methods
Combining spatial Enhancement
Methods
Combining spatial Enhancement
Methods
Our objective is to enhance the image by sharpening it and by
bringing out more of the skeletal detail.
The narrow dynamic range of the gray levels and high noise
content make this image difficult to enhance.
The strategy we will follow is:
to utilize the Laplacian to highlight fine detail, and
the gradient to enhance prominent edges.
a smoothed version of the gradient image will be used to
mask the Laplacian image
Finally, we will attempt to increase the dynamic range of
the gray levels by using a gray-level transformation.
19
4
Thank
you
Conclusions
19
6
MATLAB (average +sobel+laplace)
Fspecial : for choosing the filter:
X=fspecial(‘average’,[3 3])
p=fspecial(‘laplacian’, 0)
v=fspecial(‘sobel’) horizontal sobel
Y=v’ vertical sobel
Revise slide 57 + 58
For highboost , only multiply the mask any any constant in the
last step: y=x+3*mask for example
MATLAB – cont. (border padding)
Ex.
X=imread(‘cameraman.tif‘)
p=fspecial(‘laplacian’, 0)
Xp=imfilter(x,p, ‘replicate‘)