Chapter#3
Intensity Transformation &
Spatial Filtering
Muhammad Awais Hafeez
Assistant Professor
UET-FSD
University of Engineering and Technology, Lahore
Faisalabad Campus
Image Enhancement
• Improving the interpretability or perception of information in images
for human viewers
• Providing ‘better’ input for other automated image processing
techniques
Spatial Domain Methods Transform Domain Methods
Direct manipulation of pixels in Spatial domain first transformed
an image into transfer domain
Two categories Perform processing in transfer
• Intensity Transformations domain
• Spatial Filtering Inverse transform to bring back
results in spatial domain
Spatial Domain Techniques
• Point Processing/Intensity Transformation/Gray level
Transformations
– Image Negatives
– Log Transformation
– Power Law Transformations
– Piecewise Linear Transformation Functions
• Contrast Stretching
• Thresholding
• Gray level Slicing
• Bit Plane Slicing
• Histogram
– Histogram Equalization
– Histogram Specification
• Spatial Filtering
Intensity Transformation
• 1-pixel neighborhood in which we operate directly on the
intensity of the pixel
s = T (r )
• This is called point processing (also called gray-level or
mapping) as opposed to neighborhood processing
• Typical operations it perform
– Contrast Stretching
– Thresholding
Contrast Stretching
• Darkening the intensity level below m
• Bighting the intensity level above m
• Medium intensity areas are “stretched”
Thresholding
• Choose a threshold parameter k
• Intensities below this parameter are set to 0
• Intensities above this parameter are set to 1
• T(r) produces a two level binary image
Spatial Domain
• Spatial domain techniques operate directly on the pixels of an image
as opposed to the frequency domain
𝑔 𝑥, 𝑦 = 𝑇[𝑓 𝑥, 𝑦 ]
Is a function of the old
New intensity at (x,y)
intensity values at (x,y)
Operator on f defined over a neighborhood of
point (x,y)
Neighborhood along with a predefined operator
is called spatial filter (also referred to as spatial
mask, kernel, template or window)
Spatial Filtering
• Type of operation determines the nature of filtering process
• Consider a 3x3 mask and run it over the whole image to
generate the new image (can also use conv2)
Basic Intensity Transformation
Image Negatives
• Reversing the intensity level of an image produces the
equivalent of photographic negative
s = L −1− r
• Useful when one is interested in gray/white area and the black
area are dominating
Log Transformations
• Maps low intensities to a wider range so
• While high intensities are mapped to
smaller range
s = c log(1 + r ) so
ro
• Opposite is true for inverse
log transformation
Log Transformations
• Maps low intensities to a wider range so
• While high intensities are mapped to
smaller range
s = c log(1 + r ) so
ro
• Opposite is true for inverse
log transformation
Log Transformations
import cv2
import numpy as np
# Open the image.
img = cv2.imread('sample.jpg')
# Apply log transform.
c = 255/(np.log(1 + np.max(img)))
log_transformed = c * np.log(1 + img)
# Specify the data type.
log_transformed = np.array(log_transformed, dtype =
np.uint8)
# Save the output.
cv2.imwrite('log_transformed.jpg', log_transformed)
Power Law Transformations
s = cr
• Where c and γ are positive constant
• It also sometime written as
s = c ( r + e)
• Maps narrow range of dark i/p values to wider range of o/p values
and vice-versa
• Family of possible curves by varying gamma values
Gamma correction
• The process to correct power law
response phenomena is called
gamma correction
• It aims to improve the correctness
of an image when display on a
screen
• Controls the overall brightness
of an image
• Incorrect images can look either
bleached out or too dark
Gamma correction
import cv2
import numpy as np
# Open the image.
img = cv2.imread('images/lena.jpg',0)
# Trying 4 gamma values.
for gamma in [0.1, 0.5, 1.2, 2.2]:
# Apply gamma correction.
gamma_corrected = np.array(255*(img / 255) **
gamma, dtype = 'uint8')
# Save edited images.
cv2.imwrite('images/gamma_transformed'+str(ga
mma)+'.jpg', gamma_corrected)
Gamma correction
Piecewise Linear Transformation
• Contrast Stretching: Expands the range of intensity levels so that it
spans the full intensity range
If r1=s1 & r2=s2 Linear function
If r1=r2 , s1=0 & s2=L-1
Thresholding function
Generally, r1 ≤r2 & s1 ≤ s2
(r1,s1)=(rmin,0) &
(r2,s2)=(rmax,L-1)
Piecewise Linear Transformation
import cv2 # Define parameters.
import numpy as np r1 = 70
s1 = 0
# Function to map each intensity level r2 = 140
to output intensity level. s2 = 255
def pixelVal(pix, r1, s1, r2, s2):
if (0 <= pix and pix <= r1): # Vectorize the function to apply it to
return (s1 / r1)*pix each value in the Numpy array.
elif (r1 < pix and pix <= r2): pixelVal_vec = np.vectorize(pixelVal)
return ((s2 - s1)/(r2 -
r1)) * (pix - r1) + s1 # Apply contrast stretching.
else: contrast_stretched = pixelVal_vec(img,
return ((255 - s2)/(255 r1, s1, r2, s2)
- r2)) * (pix - r2) + s2
# Save edited image.
# Open the image. cv2.imwrite('images/contrast_stretch.jp
img = cv2.imread('images/lena.jpg',0) g', contrast_stretched)
Intensity Level Slicing
To highlight a specific range of intensities
• Display the desired range of intensities in white and suppress all
other intensities to black or vice versa. This results in a binary
image.
• The transformation function for both the cases is shown below.
Suppose in an image, your region of
interest always take value between
say 80 to 150.
So, intensity level slicing highlights
this range and now instead of looking
at the whole image, one can now
focus on the highlighted ROI.
Intensity Level Slicing
Two approaches
• Display white in one range and black in rest
• Display white in one range and rest remains unchanged
Bit-plane Slicing
• Pixels are digital number composed of bits
• It highlights the contribution made to the total image
appearance by specific bit
Histogram Processing
• Histogram of a digital image is a discrete function that counts
how many pixels have a given intensity value
h( rk ) = nk
K-th intensity value Number of pixels in the image with intensity rk
h(rk)
Normalized Histogram
• Scale nk by MN, the number of pixels
• Estimate the probability of occurrence of intensity level rk in an
image
n k = MN p (rk ) =
nk
MN
p(r ) = 1
k
k
k
Probability that a random pixel has intensity rk
Covers wide range of intensity scale
and distribution is uniform
Result will be of high contrast
and exhibits large variety of gray tones
Net effect will be an image that shows
a great deal of gray-level detail
and has high dynamic range
Histogram Stretching
• Suppose an image with histogram, associated with the table
Grey 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Level i
ni 15 0 0 0 0 70 110 45 70 35 0 0 0 0 0 15
I 5 6 7 8 9
j 2 5 8 11 14
Histogram Stretching
• Suppose an image with histogram, associated with the table
Grey 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Level i
ni 15 0 0 0 0 70 110 45 70 35 0 0 0 0 0 15
I 5 6 7 8 9
j 2 5 8 11 14
14 − 2
j= (i − 5) + 2
9−5
Histogram Stretching
• Piece-wise linear stretching function
bi +1 − bi
y= ( x − ai ) + bi
ai +1 − ai
• >> pel=find(f >= a(i) & f< (a(i+1) )
• >> out(pel)=(f(pel) - a(i)) * (b(i+1)- b(i)) / (a(i+1)- a(i)) + b(i);
M-File (Function)
1. function out=histpwl(im,a,b)
2. classChanged =0;
3. if ~isa(im, 'double')
4. classChanged=1;
5. im=im2double(im);
6. end
7. if length(a)~=length(b)
8. error('Vectors A and B must be of equal size');
9. end
10. N=length(a);
11. out=zeros(size(im));
12. for i=1:N-1
13. pix=find(im>=a(i) & im < a(i+1));
14. out(pix)=(im(pix)-a(i))*(b(i+1)-b(i))/(a(i+1)-a(i)) + b(i);
15. end
16. pix=find(im==a(N));
17. out(pix)=b(N);
18. if classChanged==1
19. out=uint8(255*out);
20. end
Use of imadjust Function
• For Histogram stretching in Matlab
imadjust( im, [low_in, high_in] , [low_out, high_out], gamma)
x−a
y= (d − c) + c
b−a
Histogram Equalization
• Increases local contrast by spreading out the intensity
histogram
• Produces artifacts
ni
h( ri r ri +1 ) =
n
Histogram Equalization
s = T (r )
Output intensity Input intensity
P(r)
P(s)
Uniform distribution Uniform Histogram Narrow Histogram Narrow distribution
of Intensities of Intensities of Intensities of Intensities
Histogram Equalization
s = T (r )
Output Intensity Input Intensity
Monotone Increasing Function
r pr ( w)
s = T (r ) = dw
− ps ( w) 1
Histogram Equalization
s = T (r )
Output Intensity Input Intensity
Monotone Increasing Function
r
s = T (r ) = pr ( w)dw
−
Histogram Equalization
s = T (r )
Output Intensity Input Intensity
Monotone Increasing Function
s = T (r ) = p (i)
i = −
r
Histogram Equalization
s = T (r ) 0 r L −1
• T is monotonically increasing i.e. T ( r1 ) T ( r2 ) for r1 r2
• and 0 T ( r ) L − 1 for 0 r L − 1
• For inverse formulation, T is monotonically increasing
−1
r = T (s) 0 s L −1
Problem
• A 4-bit image
• Intensity levels [0,L-1]=[0,15]
Grey 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Level i
ni 15 0 0 0 0 70 110 45 80 40 0 0 0 0 0 0
Problem
Grey 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Level i
ni 15 0 0 0 0 0 0 0 0 70 110 45 80 0 0 0
Σni 15 15 15 15 15 15 15 15 15 85 195 240 320 360 360 360
1/24( 0. 0. 0.6 0. 0. 0. 0. 0. 0. 3. 8.1 10 13. 15 15 15
Σni) 63 63 3 63 63 63 63 63 63 65 3 33
Rounded
value
1 1 1 1 1 1 1 1 1 4 8 10 13 15 15 15
CDF
20
10
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Histogram Equalization
• If pr(r) and T(r) are known, then the PDF of transformed
variable s can be obtained as
dr
p s ( s ) = pr ( r )
ds
• Transformation function has generally the form
r
s = T ( r ) = ( L − 1) pr ( w) dw
0
Histogram Equalization
• If pr(r) and T(r) are known, then the PDF of transformed
variable s can be obtained as
p s ( s ) = pr ( r )
dr
ds
pY ( y)dT ( x) = i
p X ( xi )dx
• Transformation function has generally the form
r
s = T (r ) = pr ( w)dw
r
s = T (r ) = ( L − 1) pr ( w)dw
0 −
Histogram Equalization
• Derivative of a definite integral with respect to its upper limit in
the integrand evaluated at the limit
ds dT ( r )
=
dr dr
d r
= ( L − 1)
dr 0
pr ( w) dw
= ( L − 1) pr ( r )
• Substituting the result
dr
p s ( s ) = pr ( r )
ds
1
= pr ( r )
( L − 1) pr ( r )
1
= 0 s L −1
L −1
Example 3.4
2r
0 r L −1
pr ( r ) = ( L − 1) 2
0
otherwise
Find out Ps(s)
r
s = T (r ) = ( L − 1) pr ( w)dw
0
dr
p s ( s ) = pr ( r )
ds
Histogram Equalization
nk
p ( rk ) = k = 0,1,2 , L − 1
MN
• Discrete form of transformation
k
sk = T (rk ) = ( L − 1) pr (r j )
j =0
( L − 1) k
=
MN j =0
nj k = 0,1,2, , L − 1
Example 3.5
• A 3-bit image of size 64X64 (MN 4096)
• Intensity levels [0,L-1]=[0,7]
rk nk Pr(rk)=nk/MN s0 1.33 → 1
r0 790 0.19 s1 3.08 → 3
r1 1023 0.25 s2 4.55 → 5
r2 850 0.21
s3 5.67 → 6
r3 656 0.16
s4 6.23 → 6
r4 329 0.08
s5 6.65 → 7
r5 245 0.06
s6 6.86 → 7
r6 122 0.03
r7 81 0.02 s7 7.00 → 7
Example 3.5
• A 3-bit image of size 64X64 (MN 4096)
• Intensity levels [0,L-1]=[0,7]