0% found this document useful (0 votes)
6 views101 pages

Spatial Domain Filtering Techniques

Uploaded by

satish
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views101 pages

Spatial Domain Filtering Techniques

Uploaded by

satish
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Spatial

Domain
Filtering

(CSE4007)

Module 2

1
Intensity transformations

 The term spatial domain refers to the image plane itself, and
image processing methods in this category are based on direct
manipulation of pixels in an image.
 Two principal categories of spatial processing are
 Intensity transformations and
 Spatial filtering.

 Intensity transformations operate on single pixels of an image,


principally for the purpose of contrast manipulation and image
thresholding etc.

 Spatial filtering involves performing operations like sharpening


and blurring by working with the neighborhood of each pixel in
an image.
Dr. [Link] Babu Intensity transformations 2
Intensity transformations
•Transformations applied pixel by pixel.
•Operate independently on each pixel value.
•No neighborhood is considered (Point Processing).
•Used for:
 Brightness adjustment
 Contrast enhancement
 Image sharpening r = input pixel intensity
s = output pixel intensity
s=T(r)
T = transformation function

Types of Intensity Transformations


a) Image Negatives Transformation
b) Log Transformation
c) Power-Law(Gamma) Transformation
d) Piecewise-Linear Transformation
i. Contrast Stretching
ii. Intensity-Level Slicing
iii. Bit Plane Slicing

Dr. [Link] Babu Intensity transformations 3


Intensity transformations
a) Image Negative
• Reversing the intensity levels of an image in this manner
produces the equivalent of a photographic negative.

• S : Output pixel intensity (after transformation)


• r : Input pixel intensity (before transformation)
• L: Total number of possible intensity levels in the image

• This type of
processing is
particularly suited
for enhancing white
or gray detail
embedded in dark
regions of an image,
especially when the
black areas are
dominant in size.

Dr. [Link] Babu Intensity transformations 4


Intensity transformations

Problem 1 - Digital Negative

Dr. [Link] Babu Intensity transformations 5


Intensity transformations
Problem 2 - Digital Negative

Dr. [Link] Babu Intensity transformations 6


Intensity transformations
b) Log Transformation
• This transformation expands dark pixels (pixels with values near
0 i.e black ), compresses bright ones(pixels with values near 255
i.e. white ).
• The general form of the log transformation is given as

r: Current Input pixel intensity (0 to


255)
s: Output intensity after
transformation
10c: A constant scaling factor
• For
log:low values
Naturaloflogarithm
r (dark pixels), the10)
(or base log function increases quickly.
• For high values of r (bright pixels), the log function flattens out.
 Assume c=25 and r=10 then log(1+10) ≈ 2.4  61
 Assume c=25 and r=200 then log(1+200) ≈ 5.3  135
 Assume c=25 and r=255 then log(1+255) ≈ 5.5  140

• Dark pixel (r = 10) gets boosted from 10 → 61


• Bright pixel (r = 255) increases only slightly → 140

Dr. [Link] Babu Intensity transformations 7


Intensity transformations
b) Log Transformation

Result of applying the log transformation


with c=1

Dr. [Link] Babu Intensity transformations 8


Intensity transformations
Problem 1 on Log Transformation

Dr. [Link] Babu Intensity transformations 9


Intensity transformations
Problem 1 on Log Transformation

Dr. [Link] Babu Intensity transformations 10


Intensity transformations
Problem2 on Log Transformation

Dr. [Link] Babu Intensity transformations 11


Intensity transformations
Problem2 on Log Transformation

Dr. [Link] Babu Intensity transformations 12


Intensity transformations
c) Power-Law (Gamma) Transformation
• Gamma (γ) controls how pixel values are mapped — it changes the
brightness and contrast of the image non-linearly.

s: Output pixel value (after transformation)


r: Input pixel value (normalized, between 0 and 1)
c: Constant (usually 1)
γ(gamma): Gamma value (the exponent)

γ < 1: More emphasis on dark values → image becomes darker


γ > 1: More emphasis on bright values → image becomes brighter
Gamma
correction is
important if
displaying an
image
accurately on a
computer
γ=1 γ = 0.33 screen is of
γ=2
concern.
Dr. [Link] Babu Intensity transformations 13
Intensity transformations
Problems on Power-Law (Gamma) Transformation

Dr. [Link] Babu Intensity transformations 14


Intensity transformations
d) Piecewise Linear Transformations
• It is a way to change the brightness and contrast of an image.
• We break the range of brightness into parts. For each part, we
apply a simple rule or function
• That’s why it’s called “piecewise linear transformation”

• Think of brightness levels like numbers from 0 to 255


• Instead of using the same rule for all brightness levels, you do
different things for different ranges. For example:

Brightness
Part Rule
Range
Piece1 0–100 (dark) Make them a little brighter
Piece2 101–200 (mid) Make them a lot brighter
201–255
Piece3 Keep the same or make dimmer
(bright)
Dr. [Link] Babu Intensity transformations 15
Intensity transformations
• This graph is from a contrast
stretching piecewise linear
transformation used to enhance
an image.
• (r₁, s₁): At this input brightness r1​
, we want the output to be
s1​.This is the first turning point
in the curve.
• (r₂, s₂): At this input brightness r2​
, we want the output to be
s2​.This is the second turning
point.

Dr. [Link] Babu Intensity transformations 16


Intensity transformations

•In above contrast stretching operation we have


 Low brightness (left side): Brightness is increased slightly.
 Mid-range (middle segment): Brightness is increased a lot (steeper line).
 High brightness (right side): Brightness is only slightly adjusted again.

Dr. [Link] Babu Intensity transformations 17


Intensity transformations

Zone 1 (below r1 = 70): map [0, 70] → 0


Zone 2 (between r1 and r2): map [70, 140] → [0, 255] (stretching contrast)
Zone 3 (above r2 = 140): map [140, 255] → 255 (flat in this case)

Dr. [Link] Babu Intensity transformations 18


Intensity transformations

ii. Intensity-Level Slicing

• Intensity Slicing highlights specific intensity ranges.


• It is useful when features span a specific intensity band.
• Two Approaches:
• Suppress all but one range, highlighting features in that
band.
• Highlight ranges by assigning them specific intensities

Dr. [Link] Babu Intensity transformations 19


Intensity transformations

Dr. [Link] Babu Intensity transformations 20


Intensity transformations

• This transformation highlights pixels in the range [A, B] with


intensity and preserves all other intensity levels.

Dr. [Link] Babu Intensity transformations 21


Intensity transformations
ii. Bit plane Slicing
• A grayscale image, where each pixel is represented by an 8-bit
value, can be divided into 8 bit planes.
• Each bit plane represents a specific bit position (Most Significant
Bit (MSB) to Least Significant Bit (LSB)) in the pixel's binary
representation.
• Bit plane slicing helps us to understand how image intensity is
distributed across the bits.
• It will help us to identify which bits contribute most to visual
quality.
• Useful for compression, filtering, watermarking, etc.
Most
Significant Bit
(leftmost bit)
LSB

Least
Significant Bit
(rightmost bit)

Dr. [Link] Babu Intensity transformations 22


• Taken a small area form the grayscale image and illustrated its
pixel values
• This 8-bit grayscale image is composed of eight Bit-planes.

• High-order bit-planes (Planes 5 to 8) contain most of the


significant visual information. They are usually retained for
image reconstruction or compression.
• Low-order bit-planes (Planes 1 to 4) mostly contain less image
details. They may be ignored due to minimal visual contribution.
Dr. [Link] Babu Intensity transformations 23
Dr. [Link] Babu Intensity transformations 24
N
I (i, j )  2n  1 I n (i, j )
Reconstruction is obtained by

n 1
•In​(i,j): The n-th bit-plane image (a binary image: 0 or 1 at each
pixel).
•N: Number of bits used (typically 8 for 8-bit images).
•2n−1 :The weight of the n-th bit.

Dr. [Link] Babu Intensity transformations 25


MSB
Intensity transformations
ii. Bit plane Slicing

An 8-bit fractal image

LSB

• We can observe that the three higher order planes, especially


the 8th bit plane, contain a significant amount of the visually
significant data.

Dr. [Link] Babu Intensity transformations 26


Intensity transformations
ii. Bit plane Slicing

• Clearly
from the
above
figure, the
last 4 bit
planes do
not seem to
have much
information
in them.

• Now, if we
combine
the 8,7,6,5
bit planes,
we will get
approximat
ely the
original
image as Dr. [Link] Babu Intensity transformations 27
Intensity transformations

ii. Bit plane Slicing

Dr. [Link] Babu Intensity transformations 28


Histogram Processing
Histogram Processing
• Histogram: is the discrete function h(rk)= nk , where rk is the kth gray
level in the range of [0, L-1] and nk is the number of pixels having
gray level rk.

• Histograms are the basis for numerous spatial domain processing


techniques.
• Histogram manipulation can be used for image enhancement
• An image histogram is a plot of the gray-level frequencies.

Dr. [Link] Babu Histogram Processing 29


Histogram Processing

Histogram Processing
• Figure below shown in four basic intensity characteristics: dark,
light, low contrast, and high contrast.
• The right side of the figure shows the histograms corresponding
to these images.
• The horizontal axis of each histogram plot corresponds to
intensity values,
• The vertical axis corresponds to values of intensity frequencies

• We note in the dark image that the components of the histogram


are concentrated on the low (dark) side of the intensity scale.
Dr. [Link] Babu Histogram Processing 30
Histogram Processing

Similarly, the
components of
the histogram
of the light
image are
biased toward
the high side
of the scale.

An image with
low contrast
has a narrow
histogram
located
typically
toward the
middle of the
intensity scale.

Dr. [Link] Babu Histogram Processing 31


Histogram Processing

• Finally, we see that the components of the histogram in the high-


contrast image cover a wide range of the intensity

Dr. [Link] Babu Histogram Processing 32


Histogram Processing
Histogram Equalization
• Histogram equalization is an image processing technique that
enhances contrast by redistributing pixel intensities, aiming for
a more uniform distribution of values across the entire intensity
range

(a) The input image (b) its gray level


histogram
Dr. [Link] Babu Histogram Processing 33
Histogram Processing
Histogram Equalization

(c) Output Image after


(d) Modified
Histogram equalization
Histogram

Dr. [Link] Babu Histogram Processing 34


Histogram Processing
Histogram Equalization Process
 Find the running sum of the histogram values.
 Normalize the values from Step (1) by dividing by the total
number of pixels.
 Multiply the values from Step (2) by the maximum gray-level
value and round.
 Map the gray level values to the results from Step (3) using a
one-to-one correspondence.
 Perform the histogram equalization of the image

 There are eight possible gray levels from 0 to 7. The histogram


can be seen as above

Dr. [Link] Babu Histogram Processing 35


Histogram Processing
Histogram Equalization Process

Dr. [Link] Babu Histogram Processing 36


Histogram Processing
Histogram Equalization Process

Dr. [Link] Babu Histogram Processing 37


Histogram Processing
Histogram Equalization Process

Problem2: Perform the histogram equalization of the image

Dr. [Link] Babu Histogram Processing 38


Histogram Processing
Histogram Equalization Process

Gray level 0 1 2 3 4 5 6 7
Number of pixels 4 1 2 4 3 1 1 0
Running Sum 4 5 7 11 14 15 16 16
Running Sum / Total 4/16 5/16 7/16 11/16 14/16 15/16 16/16 16/16
pixels
Multiply result by max 2 2 3 5 6 7 7 7
gray level (7)

Dr. [Link] Babu Histogram Processing 39


Histogram Processing

• To represent a pixel with a maximum intensity of 14, we need the


minimum number of bits (b).
• We find the smallest b such that 2 b−1≥14.
• 23−1=7 (not enough)
• 24−1=15 (enough)
• Therefore, 4 bits are required. This means the image uses L=2 4=16
gray levels, ranging from 0 to 15.
Gray level (rk​) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Number of
pixels (nk​) 0 1 0 1 2 3 5 0 3 2 0 0 6 0 1 0
Running Sum 0 1 1 2 4 7 12 12 15 17 17 17 23 23 24 24

Running
Sum/Total
number of pixels 0/24 1/24 1/24 2/24 4/24 7/24 12/24 12/24 15/24 17/24 17/24 17/24 23/24 23/24 24/24 24/24

Multiply the
above result by
maximum gray
level 0 1 1 1 3 4 8 8 9 11 11 11 14 14 15 15

Dr. [Link] Babu Histogram Processing 40


Histogram Processing

Dr. [Link] Babu Histogram Processing 41


Histogram Processing

Dr. [Link] Babu Histogram Processing 42


Histogram Processing

Dr. [Link] Babu Histogram Processing 43


Spatial Filtering
• Spatial filtering enhance or modify an image by manipulating the
pixel values based on their spatial location and the values of
neighboring pixels.
• It applies a filter (or kernel) over an image to process a local group of
pixels at a time.
• This can be used to sharpen, blur, detect edges, or remove noise from
an image.
Origin x
• A spatial filter consists of (a) a neighborhood, and (b) a predefined
operation
a b c r s t
d
g
e
h
f
i
* u
x
v
y
w
z
Original Image Filter
Simple 3*3 Pixels
e 3*3 Filter
Neighbourhood
eprocessed = v*e +
r*a + s*b + t*c +
u*d + w*f +
y Image f (x, y) x*g + y*h + z*i
Dr. [Link] Babu Spatial Filtering 44
Spatial Filtering

 The above is repeated for


every pixel in the original
image to generate the filtered
image.
 Filtering can be given in
equation form as shown
below

 where x and y are varied so


that each pixel in w visits
every pixel in f

Dr. [Link] Babu Spatial Filtering 45


Spatial Correlation and convolution
• There are two closely related concepts that must be
understood clearly when performing spatial filtering.
 1) Correlation and
 2) Convolution.

1)Correlation
• Correlation is the process of moving a filter mask over the
image and computing the sum of products at each location,
exactly as explained in the previous section.

2)Convolution
• The mechanics of convolution are the same, except that
the filter is first rotated by 180°.

• The best way to explain the differences between the two


concepts is by example. We begin with a 1-D illustration.

Dr. [Link] Babu Correlation and convolution 46


Spatial Correlation and convolution

(0,0) (2×4)+(4×2)+(5×4)+(3×2)+(5×1)+(6×2)+(8×4)+(4×2)+(2×4)= 107

(4×4)+(5×2)+(6×4)+(5×2)+(6×1)+(8×2)+(4×4)+(2×2)+(6×4)= 126
(0,1)

(5×4)+(6×2)+(7×4)+(6×2)+(8×1)+(9×2)+(2×4)+(6×2)+(7×4) = 146
(0,2)

Dr. [Link] Babu Correlation and convolution 47


Spatial Correlation and convolution

(6×4)+(7×2)+(3×4)+(8×2)+(9×1)+(2×2)+(6×4)+(7×2)+(1×4)= 121
(0,3)

(3×4)+(5×2)+(6×4)+(8×2)+(4×1)+(2×2)+(7×4)+(2×2)+(5×4)=122
(1,0)

(5×4)+(6×2)+(8×4)+(4×2)+(2×1)+(6×2)+(2×4)+(5×2)+(9×4)=140
(1,1)

(1,2) (6×4)+(8×2)+(9×4)+(2×2)+(6×1)+(7×2)+(5×4)+(9×2)+(3×4)= 150

(8×4)+(9×2)+(2×4)+(6×2)+(7×1)+(1×2)+(9×4)+(3×2)+(7×4)= 149
(1,3)

Dr. [Link] Babu Correlation and convolution 48


Spatial Correlation and convolution

(2,0) (8×4)+(4×2)+(2×4)+(7×2)+(2×1)+(5×2)+(3×4)+(4×2)+(2×4)= 102

(2,1) (4×4)+(2×2)+(6×4)+(2×2)+(5×1)+(9×2)+(4×4)+(2×2)+(4×4)= 107

(2×4)+(6×2)+(7×4)+(5×2)+(9×1)+(3×2)+(2×4)+(4×2)+(6×4)= 113
(2,2)

(2,3) (6×4)+(7×2)+(1×4)+(9×2)+(3×1)+(7×2)+(4×4)+(6×2)+(2×4)= 113

Dr. [Link] Babu Correlation and convolution 49


Spatial Correlation and convolution
This entire process is
[107 126 146 121 Valid
Final output Matrix: 122 140 150 149 Correlation/Convoluti
102 107 113 113] on-2D Without
Padding

Dr. [Link] Babu Correlation and convolution 50


Spatial Correlation and convolution
• If you need output image as same size as input image, then
apply padding of 1 row/col
• Rule for Padding (for SAME output size):
 For a filter of size m×n (Consider below example)
 Pad with ⌊m/2⌋ rows on top and bottom
 Pad with ⌊n/2⌋ columns on left and right

Dr. [Link] Babu Correlation and convolution 51


Spatial Correlation and convolution

• Flip the Kernel

• Now, we slide the 3×3 kernel over the image, compute


element-wise product and sum them. We can compute 4
positions (2×2 output)
• Position (0,0):

(0*3 + 5*3 + 0*3) + (1*2 + 6*2 +


2*2) + (2*1 + 7*1 + 8*1) = 50

Dr. [Link] Babu Correlation and convolution 52


Spatial Correlation and convolution

• Position (0,1):

(5*3 + 0*3 + 2*3) + (6*2 + 2*2 + 4*2) + (7*1 +


8*1 + 6*1) = 66

• Position (1,0):

(1*3 + 6*3 + 2*3) + (2*2 + 7*2 + 8*2) + (3*1 + 2*1


+ 0*1) = 66

• Position (1,1):

(6*3 + 2*3 + 4*3) + (7*2 + 8*2 + 6*2) + (2*1 + 0*1


+ 1*1) = 81

Dr. [Link] Babu Correlation and convolution 53


Spatial Correlation and convolution
Final Answer with 180° Flipped Kernel

Dr. [Link] Babu Correlation and convolution 54


Smoothing Filters
 In the context of digital image processing, smoothing filters
are used to reduce noise, blurring image and minor
variations in images.
 Smoothing filters can be broadly categorized into:
 Linear Filters: These filters compute the output pixel
value as a weighted sum of neighboring pixel values. e.g
Averaging Filter
 Non-linear Filters: These filters use non-linear operations
Linear Filter
to determine the output pixel value. e.g Median Filter
 Linear filters compute output pixel values as weighted sums of
neighboring pixels.
 The weights are defined in a small matrix called a kernel or
mask.
 This Kernel or mask is applied to given image via convolution:
the kernel slides over the image.
 These filters sometimes are called averaging filters or low
pass filters or Mean Filter
 Averaging Filter is the simplest form of linear filter. It replaces
each pixel with the average of its neighbors.

Dr. [Link] Babu Smoothing Filters 55


Smoothing Filters x
104 100 108 1
/9 1
/9 1
/9

* /9 /9 /9
1 1 1
99 106 98

95 90 85 1
/9 1
/9 1
/9
1
/9 100
104 1
/9 108
1
/9
Original Image Filter
Simple 3*3 /9 106
1
99 1
/9 198
/9
3*3 Smoothing Pixels
Neighbourhood /9 190
1
95 /9 185
/9 Filter
e= 1
/9*106 + 1/9*104 + 1/9*100 +
1
/9*108 + 1/9*99 + 1/9*98 +
1
/9*95 + 1/9*90 + 1/9*85
y Image f (x, y) = 98.3333
 The above convolution is repeated for every pixel in the
original image to generate the smoothed image.
 During convolution, align the center cell of the filter directly
over the current pixel in the image that you're processing.
 A spatial averaging filter in which all coefficients are equal
sometimes is called a box filter.
Dr. [Link] Babu Smoothing Filters 56
Smoothing Filters

Box filter Weighted Average filter

 A weighted average filter gives different weights to pixels in


the neighborhood
 Usually it gives more weight to the center and less to the
outer pixels.

Dr. [Link] Babu Smoothing Filters 57


Smoothing Filters
 The image at the top left is
an original image of size
500*500 pixels

 The subsequent images


show the image after
filtering with an averaging
filter of increasing sizes 3, 5,
9, 15 and 35

 Notice how detail begins to


disappear

Dr. [Link] Babu Smoothing Filters 58


Smoothing Filters
Non Linear Filter: Median Filter
 The median filter replaces each pixel’s value with the median
of the intensity values in its neighborhood.(Median = the
middle value)
 It works based on ordering (ranking) the pixels contained in
the filter mask
 Replace the value of the center pixel with the value
determined by the ranking result
 E.g., median filter, max filter, min filter

Dr. [Link] Babu Smoothing Filters 59


Smoothing Filters
Non Linear Filter: Median Filter
– It works well for impulse noise (e.g. salt and pepper).
– It requires sorting of the image values.
– It preserves the edges better than an average filter in the
case of impulse noise.

– Filtering is often used to remove noise from images.


Sometimes a median filter works better than an averaging
filter

Dr. [Link] Babu Smoothing Filters 60


Smoothing Filters
Non Linear Filter: Median Filter
 Min Filter: replaces center pixel with the minimum value in
the neighborhood. Useful for removing bright noise

Minimum value = 60
Replace the center pixel
(150) with 60

 Max Filter: replaces center pixel with the maximum value in


the neighborhood. This operation is great for removing dark
(black) noise.

Maximum value = 255


Replace the center pixel
(150) with 255

Dr. [Link] Babu Smoothing Filters 61


Smoothing Filters
Non Linear Filter: Median Filter

 Padding:
 Padding is the process of adding extra rows and columns
around the border of an image to allow filtering operations
(like convolution) to be applied at the edges without losing
data.
 At the edges of an image we are missing pixels to form a
neighbourhood

Dr. [Link] Babu Smoothing Filters 62


Smoothing Filters
Non Linear Filter: Median Filter

 Padding:
There are a few approaches to dealing with missing edge
pixels:
 Omit missing pixels
• Only works with some filters
• Can add extra code and slow down processing
 Pad the image
• Typically with either all white or all black pixels
 Replicate border pixels
 Allow pixels wrap around the image
• When a filter needs pixels outside the image, it
takes them from the opposite side of the image

Dr. [Link] Babu Smoothing Filters 63


Smoothing Filters

Pad the image Mean filter kernel


with zeros (1
layer around)

Dr. [Link] Babu Smoothing Filters 64


Smoothing Filters

• At position (0,0) on original image

Sum = 9  Mean = 9/9 = 1

• In the same manner apply filter for each 3×3 block of input
image to fill a 4×4 output.
• Final Output for (a):

Dr. [Link] Babu Smoothing Filters 65


Smoothing Filters

 Instead of 0s, we "wrap" the values from opposite sides.


 For example:
 Rows:
 Row 0 (above row 1): copy from last row (row 4) →
2647
 Row 5 (below row 4): copy from row 1 → 1 2 3 2
 Columns:
 Column 0 (left of col 1): wrap from last column (col
4)
 Column 5 (right of col 4): wrap from first column
(col 1)

Dr. [Link] Babu Smoothing Filters 66


Smoothing Filters

We can use the following weighted mean filter

• Apply the same sliding 3×3 filter with this wrapped padded
image and compute each output.
• You’ll need to multiply each element by its kernel weight,
sum, then divide by 16.

(1/16)×(1×7+2×2+1×6+2×2
+4×1+2×2+1×1+2×4+1×2)
=2.5

(rounded to nearest
integer): 2

Dr. [Link] Babu Smoothing Filters 67


Smoothing Filters

Output for (b) (rounded to nearest integer):

• Rule for Padding (for SAME output size):


 For a filter of size m×n (Consider
below example)
 Pad with ⌊m/2⌋ rows on top and
bottom
 Pad with ⌊n/2⌋ columns on left and
Filter = right
5×5, so: ⌊5/2⌋ =2
So, you need:
2 rows of padding at top and
bottom
2 columns of padding at left and
right Dr. [Link] Babu Smoothing Filters 68
Smoothing Filters

After
Padding

Apply 5x5 Median


filter
Flatten the matrix and sort

[1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 5, 6]

From these 25 values  median = 13th value = 2

Repeat the same process to fill 4*4 output matri

Dr. [Link] Babu Smoothing Filters 69


Smoothing Filters

Gaussian filter

Dr. [Link] Babu Smoothing Filters 70


Smoothing Filters
Gaussian filter

Dr. [Link] Babu Smoothing Filters 71


Smoothing Filters
Gaussian filter Example Problem

Dr. [Link] Babu Smoothing Filters 72


Sharpening Filters

• Previously we have looked at smoothing filters which remove


fine detail
• Sharpening spatial filters seek to highlight fine detail
• Remove blurring from images
• Highlight edges
• One of the ways to do this is by using spatial differentiation,
which involves calculating the rate of change in pixel intensity
values.
• Differentiation: It is the process of finding a derivative.
• Spatial differentiation highlights intensity transitions
 It’s the action of calculating how a function changes.
 Here Differentiation is the operation; derivative is the
result.

• Derivative: It is the result of differentiation .


 It represents the rate of change or slope of a function at a
given point.
 Derivative is what you get after you differentiate.

Dr. [Link] Babu Sharpening Filters 73


Sharpening Filters
• In a digital image, brightness values change across space.
• When we differentiate these values .we get the derivative.
• The derivative tells us how fast the brightness changes which
is critical for:
• Detecting edges
• Sharpening
• Feature extraction

Dr. [Link] Babu Sharpening Filters 74


• The derivatives of a digital function are defined in terms of
differences.
• There are various ways to define these differences.
• Definition we use for a first derivative
1. must be zero in areas of constant intensity;
2. must be nonzero at the onset (start) of an intensity step or
ramp; and
3. must be nonzero along ramps.
• A basic definition of the first-order derivative of a one-
dimensional function f(x)is the difference

• Definition of a second derivative


1. must be zero in constant areas;
2. must be nonzero at the onset and end of an intensity step
or ramp; and
3. must be zero along ramps of constant slope.
• We define the second-order derivative f(x) of as the difference

Dr. [Link] Babu Sharpening Filters 75


• To illustrate this, and to examine the similarities and
differences between first- and second-order derivatives of a
digital function, consider the example

• As the figure shows, the scan line contains


• an intensity ramp,
• three sections of constant intensity,
• and an intensity step.
• The circles indicate the onset or end of intensity transitions

Dr. [Link] Babu Sharpening Filters 76


• The first- and second-order derivatives computed using the
two preceding definitions are included below the scan line
and plotted
• When computing the first derivative at a location x, we
subtract the value of the function at that location from the
next point.

f(x+1)
Dr. [Link] – f(x) = 5-6Sharpening
Babu = -1 Filters 77
• Similarly, to compute the second derivative at x, we use the
previous and the next points in the computation. For
example. If f(x)=5
f(x+1) + f(x-1) – 2.f(x)= 6+4 – 2(5) = -1
Comparison: First, we encounter an area of constant intensity
both derivatives are zero there, so condition (1) is satisfied
for both.
Dr. [Link] Babu Sharpening Filters 78
• Next, we encounter an intensity ramp followed by a step, and
we note that
• The first-order derivative is nonzero at the onset of the
ramp and the step;
• Similarly, the second derivative is nonzero at the onset and
end of both the ramp and the step; therefore, property (2)
is satisfied
• we see for both
that property (3)derivatives
is satisfied also for both
derivatives
Dr. [Link] Babu Sharpening Filters 79
Second Derivative for Image Sharpening—The
Laplacian
• We consider the implementation of 2-D, second-order
derivatives and their use for image sharpening.

• In discrete terms (digital images), a common approximation of


the Laplacian is:

Laplacian Filter Kernel


(Mask) are given as

Option 1: 4-connected Option 2: 8-


neighbors connected
neighbors

Dr. [Link] Babu Sharpening Filters 80


Second Derivative for Image Sharpening—The
Laplacian
• Laplacian filters are used to find edges (second-order
derivatives), and there are two commonly used Laplacian
kernels:

1)

• Here, center is positive, so you add the Laplacian calculated for


every pixel to the original pixel

2)

Here, center is negative , so you subtract the Laplacian


calculated for every pixel to the original pixel

• It computes the difference between a pixel and its neighbors.


• If all neighbors are similar → output ≈ 0.
• If there's a sharp change → output is large (positive or
negative).
Dr. [Link] Babu Sharpening Filters 81
• Use a standard second-order Laplacian kernel:

• Perform border Replication Padding

• We apply the kernel to each pixel in the original image:

Dr. [Link] Babu Sharpening Filters 82


• Find Laplacian Output Matrix

Pixel (0, 0):


g=

=(0×7)+(1×7)+(0×1)+(1×7)+
g(0,0) (−4×7)+(1×1)+(0×7)+(1×7)+(0×1) = -6
= 7−(−6)=13

Pixel (0, 1):


g=

=(0×7)+(1×1)+(1×0)+(7×1)−(1×4)+(1×1)+(7×0)+(1×1)+(1×0)=
g(0,1)=1−6=−5

milarly find g(0,2), g(1,0), g(1,1), g(1,2), g(2,0), g(2,1), g(2,2)

Dr. [Link] Babu Sharpening Filters 83


The Final Sharpened Image:

Dr. [Link] Babu Sharpening Filters 84


import cv2
import numpy as np
import [Link] as plt
image = [Link]('inputs/[Link]', cv2.IMREAD_GRAYSCALE)
filter = [Link]([
[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]
], dtype=np.float32)
# Laplacian filter (detect edges)
filtered = cv2.filter2D(image, ddepth=cv2.CV_64F, kernel=filter)
edges = [Link](filtered)
sharpened = [Link](image, edges)

# Display all results


[Link](figsize=(15, 5))

[Link](1, 3, 1)
[Link]('Original Image')
[Link](image, cmap='gray')
[Link]('off')

Dr. [Link] Babu Sharpening Filters 85


[Link](1, 3, 2)
[Link]('Laplacian Edges')
[Link](edges, cmap='gray')
[Link]('off')

[Link](1, 3, 3)
[Link]('Sharpened Image')
[Link](sharpened, cmap='gray')
[Link]('off')

plt.tight_layout()
[Link]()

Dr. [Link] Babu Sharpening Filters 86


Dr. [Link] Babu Sharpening Filters 87
First Derivative for Image Sharpening—The Gradient
• The first derivative measures how fast pixel values are
changing ( detects edges),
• The second derivative measures how fast the change itself is
changing

• On 1D vector first derivative tells how brightness changes


from left to right i.e

• In 2D images, we have two directions:

• The gradient combines both partial first derivatives into a


vector:

: detects horizontal intensity changes.


gy : detects vertical intensity
changes.
Dr. [Link] Babu Sharpening Filters 88
• To find the strength of the change (i.e greatest rate of
change) we calculate the magnitude(M) of the gradient.

• Following are the kernels (masks) used to estimate the first


derivative
Filter to get Filter to get

• These filter’s are called Sobel operators. All the mask


coefficients sum to zero.

• Apply both filters to the image to get gx and gy, then


calculate gradient magnitude M(x,y)

• Sharpened image: g(x,y)=f(x,y)+M(x,y)

Dr. [Link] Babu Sharpening Filters 89


• To find the strength of the change (i.e greatest rate of
change) we calculate the magnitude(M) of the gradient.

• Following are the kernels (masks) used to estimate the first


derivative
Filter to get Filter to get

Dr. [Link] Babu Sharpening Filters 90


import cv2
import numpy as np
import [Link] as plt

image = [Link]('inputs/[Link]',
cv2.IMREAD_GRAYSCALE)
# for better precision
image = np.float32(image)

# Sobel kernels
sobel_x = [Link]([
[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]
], dtype=np.float32)

sobel_y = [Link]([
[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]
], dtype=np.float32)

Dr. [Link] Babu Sharpening Filters 91


g_x = cv2.filter2D(image, cv2.CV_32F, sobel_x)
g_y = cv2.filter2D(image, cv2.CV_32F, sobel_y)

# M(x,y) = sqrt(gx^2 + gy^2)


gradient_magnitude = [Link](g_x, g_y)

# Normalize magnitude for visualization (0–255)


gradient_magnitude_norm = [Link](gradient_magnitude,
None, 0, 255, cv2.NORM_MINMAX)
gradient_magnitude_uint8 = np.uint8(gradient_magnitude_norm)

# Sharpened image: g(x,y) = f(x,y) + M(x,y)


sharpened = [Link](image, gradient_magnitude)

# Clip values and convert to uint8 for display


sharpened_clipped = [Link](sharpened, 0, 255).astype(np.uint8)

# Display results
[Link](figsize=(15, 5))

Dr. [Link] Babu Sharpening Filters 92


[Link](1, 3, 1)
[Link]('Original Image')
[Link](image, cmap='gray')
[Link]('off')

[Link](1, 3, 2)
[Link]('Gradient Magnitude')
[Link](gradient_magnitude, cmap='gray')
[Link]('off')

[Link](1, 3, 3)
[Link]('Sharpened Image')
[Link](sharpened_clipped, cmap='gray')
[Link]('off')

plt.tight_layout()
[Link]()

Dr. [Link] Babu Sharpening Filters 93


Dr. [Link] Babu Sharpening Filters 94
Unsharp Masking & High Boost Filtering
Unsharp masking is a simple and effective image sharpening technique that
subtracts a blurred version of the image from the original image to detect edges,
then adds this mask back to the original to enhance edge contrast

•First, we blur the image. We know by smoothing an image we suppress most of


the high-frequency components.
•Then, we subtract this smoothed image from the original image(the resulting
difference is known as a mask m(x,y).

•where, f(x,y) is the original image and fb(x,y) is the blurred version of the original
image.
•Adding this mask m(x,y) back to the original will enhance the high-frequency
components.

•When k= 1 this is known as Unsharp masking.


•We can also write the above two equations into one as the
weighted average of the original and the blurred image.

•For k>1 we call this as high-boost filtering because we are


boosting the high-frequency components by giving more weight to
the masked (edge) image.
•We can also apply the following filters

If k = 1, it becomes it
becomes “standard”
Laplacian sharpening
98
REFERENCES
Rafael C Gonzalez and Richard E Woods, “Digital Image Processing”,
Pearson Education, Third Edition, 2016.

Dr. [Link] Babu Reference Text Book 99


Module1 Theory: Sampling & Quantization, Phases in Digital Image Processing,
Light, Brightness adaptation and discrimination, Pixels, Coordinate conventions,
Imaging Geometry transformation techniques, Perspective Projection
Module1 Problems: Problems on Geometric transformations (Nearest neighbor,
Bilinear Interpolation and other), calculation of distances (Euclidean, D4, D8,
etc.)

Module2 Theory: All Intensity Transformations, Smoothing filters, Smoothing


filters, Sharpening Filters, Unsharp Masking & High Boost Filtering
Module2 Problems: Correlation and Convolution, Histogram equalization,
Smoothing filters(Averaging, Median), Sharpening Filters(Laplacian)

100
101

You might also like