Lec 02 (BIP) Image Enhancement - Spatial Domain
Lec 02 (BIP) Image Enhancement - Spatial Domain
Engineering
UET Lahore, New Campus
Engr.Mohd Abdullah
Brightness and Contrast
Brightness
+50 +50
So we need enhancement
Image Enhancement
e.g.) A method that is quite useful for enhancing x-ray images may
not necessarily be the best approach for enhancing pictures of
Mars transmitted by a space probe.
3 Combination Methods
Spatial Domain Methods
𝑔 𝑥, 𝑦 = 𝑻 𝑓(𝑥, 𝑦)
Operator/Transformation
Spatial Enhancement
1 2 3 4
Advantages of negative:
✓ Produces an equivalent of a photographic negative.
✓ Enhances white or gray detail embedded in dark regions.
Basic Gray Level Transformations
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
b) Log Transformations:
𝑠 = 𝑐 log(1 + 𝑟)
𝑠 = 𝑐𝑟 𝛾
𝛾=0.6
𝛾=0.4 𝛾=0.3
𝛾=4
𝛾=5
Piecewise Linear Transformations
Piecewise Linear Transformation
(r1, s1)
(r1, s1)
(r1, s1)
This is called 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.
Piecewise Linear Transformation
Example: Contrast stretching
Image (r) Image (s) after applying T (contrast
stretching)
T= If r >150; s =1
If r <150; s =0
Approach 1 Approach 2
Display in one value (e.g. white) all the Brightens or darkens the desired range
values in the range of interest , and in of intensities but leaves all other
another (e.g. black) all other intensities intensity levels in the image unchanged
Piecewise Linear Transformation
Approach 1 Approach 2
𝑳−𝟏 𝒇𝒐𝒓 𝑨 ≤ 𝒙 ≤ 𝑩
𝑳−𝟏 𝒇𝒐𝒓 𝑨 ≤ 𝒙 ≤ 𝑩 𝒈 𝒙 =ቊ
𝒈 𝒙 =ቊ 𝒙 𝑬𝒍𝒔𝒆𝒘𝒉𝒆𝒓𝒆
𝟎 𝑬𝒍𝒔𝒆𝒘𝒉𝒆𝒓𝒆
Preserves background
Example: Intensity level slicing
If the pixel intensity is between (100 → 150)
Approach-1 convert it in the new image into 255 (white).
Otherwise convert it to 0 (black).
Example: Intensity level slicing
If the pixel intensity is between (100 → 150)
convert it in the new image into 255 (white).
Otherwise leave it the same
Example: Intensity level slicing
01100100
MSB LSB
Image of bit1: Image of bit2: Image of bit3:
00000000 00000000 00000100 Image of bit4:
00000000
0 0 1 0
Advantage/ Disadvantage?
Histogram Introduction
Histogram
8
7
6
5
4
3
2
1
0
Java C++ Python C#
Example
Result
CMS # Grade Result
001 A
002 D 4
003 B 3
004 A
2
005 C
006 B 1
007 B
0
008 C A B C D
009 B
Histogram of an image
• Histogram of an image, like other histograms also shows frequency.
But an image histogram, shows frequency of pixels intensity values.
Given an image 0 0 0 0
0 1 2 3
0 2 4 6
Draw Histogram
Why Histogram?
3.5
2.5
1.5
0.5
a) Histogram Sliding:
In histogram sliding , we just simply shift a complete histogram
rightwards or leftwards. Due to shifting or sliding of histogram towards
right or left, a clear change can be seen in the brightness.
𝑓𝑚𝑖𝑛 𝑓𝑚𝑎𝑥
Example: Histogram Stretching
4 0 1 2 6
𝑓𝑚𝑎𝑥 = 4
0 3 2 1 4
𝑓𝑚𝑖𝑛 = 0
2 0 1 2 2
Contrast = 4
3 4 0 2 0
0 1 2 3 4 5 6 7
3-bit image
0
0 1 2 3 4 5 6 7
Example: Histogram Stretching
5
x 10 Histogram of original image
4
histogram stretched image
3
𝑓𝑚𝑎𝑥 = 143
𝑓𝑚𝑖𝑛 = 84
Contrast = 59
2
0
0 50 100 150 200 250
5
x 10 Histogram of output
4
0
0 50 100 150 200 250
Example: Histogram Stretching
image
Histogram
10000
𝑓𝑚𝑎𝑥 = 178
𝑓𝑚𝑖𝑛 = 112 5000
Contrast = 66
0
0 50 100 150 200 250
Histogram
10000
• Applied Histogram
stretching
5000
0 Histogram
0 50 100 150 10000 200 250
Histogram
𝑓𝑚𝑎𝑥 = 255 10000
5000
𝑓𝑚𝑖𝑛 = 0
Contrast = 255
5000 0
0 50 100 150 200 250
Example: Histogram Stretching
image
4
x 10 Histogram
3
0
0 50 100 150 200 250
4
x 10 Histogram
Histogram 3
0
0 50 100 150 200 250
Failing of Histogram Stretching
(𝐿 − 1)
𝑔 𝑥, 𝑦 = 𝑓 𝑥, 𝑦 − 𝑓𝑚𝑖𝑛 ×
𝑓𝑚𝑎𝑥 − 𝑓𝑚𝑖𝑛
(𝐿 − 1)
𝑔 𝑥, 𝑦 = 𝑓 𝑥, 𝑦 − 0 ×
255 − 0
255
𝑔 𝑥, 𝑦 = 𝑓 𝑥, 𝑦 ×
255
𝑔 𝑥, 𝑦 = 𝑓 𝑥, 𝑦
Failing of Histogram Stretching
original image
4
x 10 Histogram of original image
6
0
0 50 100 150 200 250
0
0 50 100 150 200 250
Histogram Equalization
c) Histogram Equalization
1
𝑝𝑠 𝑠 =
𝐿−1
𝑝𝑠 𝑠 ds = 𝑝𝑟 𝑟 𝑑𝑟 : 𝑃𝑟𝑜𝑏𝑎𝑏𝑖𝑙𝑖𝑡𝑦 𝑡ℎ𝑒𝑜𝑟𝑦
𝑑𝑠
= 𝐿 − 1 𝑝𝑟 (𝑟)
𝑑𝑟
𝑟
𝑠 = 𝑇 𝑟 = 𝐿 − 1 න 𝑝𝑟 𝑤 𝑑𝑤 → Cumulative distribution
0 function(CDF)
For discrete values
𝑟
𝑠 = 𝑇 𝑟 = 𝐿 − 1 𝑝𝑟 𝑗 →Histogram Equalization
𝑗=0
Histogram Equalization
Probability density function (pdf) of the image
𝑐𝑑𝑓 𝑛 = 𝑝𝑛
𝑛=0
𝑔𝑖,𝑗 = 𝑟𝑜𝑢𝑛𝑑 𝐿 − 1 𝑝𝑛
𝑛=0
Histogram Equalization
Histogram Equalization
L X pdf(X) CDF CDF×(L-1) g(x)
0 1 0.031 0.031 0.219 0
1 3 0.094 0.125 0.875 1
2 5 0.156 0.281 1.969 2
3 6 0.188 0.469 3.281 3
4 6 0.188 0.656 4.594 5
5 6 0.188 0.844 5.906 6
6 2 0.063 0.906 6.344 6
7 3 0.094 1 7 7
Histogram Equalization
0 0 0 4
Histogram Equalization
1 1 1 5
L X pdf(X) CDF CDF×(L-1) g(x)
1 2 2 7 0 3 0.1875 0.1875 1.3125 1
2 2 2 7 1 4 0.25 0.4375 3.0625 3
2 5 0.3125 0.75 5.25 5
3 0 0 0.75 5.25 5
1 1 1 6 4 1 0.0625 0.8125 5.6875 6
1 1 1 5 3
1 2 2 7 2
1
2 2 2 7
0
0 1 2 3 4 5 6 7
5
1 1 1 6
4
3 3 3 6 3
3 5 5 7 2
5 5 5 7 1
0
0 1 2 3 4 5 6 7
Example: Histogram Equalization
4
x 10 Histogram
3
2
Histogram equalized
1
0
0 50 100 150 200 250
Histogram equalized
4
x 10 Histogram
Histogram 3
0
0 50 100 150 200 250
Example: Histogram Equalization
4
x 10 Histogram
2
1.5
Histogram equalized
1
0.5
0
0 50 100 150 200 250
4
x 10 Histogram
2
1.5
0.5
0
0 50 100 150 200 250
Example: Histogram Equalization
4
x 10 Histogram
5
4 Histogram equalized
3
0
0 50 100 150 200 250
Histogram equalized
Histogram x 10
4
Histogram
6
0
0 50 100 150 200 250
Question
Histogram equalized
4
Histogram x 10 Histogram
15
Adoptive HE • Global histogram equalization
10
• Comparing the results with the
figure above we can see that
5 the distribution was shifted
towards higher values
0
0 50 100 150 200 250
Adoptive HE
100 150 200
4 250 Histogram
Histogram x 10
10
• Adaptive histogram
equalization shows better
5
contrast over different parts of
the image.
0
0 50 100 150 200 250
Adaptive Histogram Equalization
4
x 10 Histogram
5 Histogram equalized
3 • X-ray Image
2
0
0 50 100 150 200 250
Histogram equalized
4
x 10 Histogram
Histogram 6 Adoptive HE
4
• Histogram
2
equalization
0
0 50 100 150 200 250
Histogram
100 Histogram 150 15000 200 250
10000
• Adaptive
5000
histogram
equalization
0
0 50 100 150 200 250
Adaptive Histogram Equalization
4
x 10 Histogram
Histogram equalized
3
2
• Image
1
0
0 50 100 150 200 250
Histogram equalized
4 Adoptive HE
Histogram
x 10
Histogram 3
2
• HE
1
0
0 50 100 150 200 250
Histogram
50 100 Histogram150 15000
200 250
10000 • AHE
5000
0
0 50 100 150 200 250
Enhancement Using Arithmetic/Logic Op.
Enhancement Using Arithmetic/Logic Op.
Arithmetic/logic operations involving images are performed on a pixel-
by-pixel basis between two or more images.
Arithmetic Operations
Addition, Subtraction, Averaging
Logic Operations
AND, OR, NOT
Addition
+ =
Addition
+ =
𝑔 𝑥, 𝑦 = 𝑓1 𝑥, 𝑦 + 0.2 × 𝑓2(𝑥, 𝑦)
Image Averaging
Idea: Average multiple pictures of the same scene to reduce noise
If 𝑀 images are averaged
• Image averaging works on the assumption that the noise in your
image is truly random. This way, random fluctuations above and
below actual image data will gradually even out as one averages
more and more images.
g ( x, y ) = f ( x, y ) + ( x, y )
Averaging M different noisy images
M
1
g ( x, y ) =
M
g ( x, y )
i =1
i
This technique was used to take the photo of Mount Kenya shown below from a
distance of 20 kilometers on a hazy day using a low-cost mega-zoom camera:
❖ Images taken from Geoff Dougherty, DIP for medical applications
Example: Image Averaging
Subtraction
What’s changed?
Example: Subtraction
Image A Image B
A and B A or B A xor B
OR
OR =