CV_Assignment2_SC22B109
CV_Assignment2_SC22B109
Assignment 2
Rahul Kumar Sinha
SC22B109
1 Problem 1
Consider a 4 × 4 grayscale image given by:
10 20 30 40
50 60 70 80
I =
90 100 110 120
130 140 150 160
F = W4 · I · W4T
where W4 is the DFT basis matrix, defined as:
2π
W (k, n) = e−j kn
4
4
for N = 4.
The DFT basis matrix for N = 4 is:
1 1 1 1
1 −j −1 j
W4 =
1 −1 1 −1
1 j −1 −j
The transpose of W4 is:
1 1 1 1
T 1 j −1 −j
W4 =
1 −1 1 −1
1 −j −1 j
Now, performing matrix multiplication:
1. Compute W4 · I:
1 1 1 1 10 20 30 40
W · I = 1 −j −1 j 50 60 70 80
4
1 −1 1 −1 90 100 110 120
1 j −1 −j 130 140 150 160
After computation, we get:
280 320 360 400
−80 + 80j −80 + 80j −80 + 80j −80 + 80j
W ·I =
4 −80 −80 −80 −80
−80 − 80j −80 − 80j −80 − 80j −80 − 80j
1
2. Compute (W4 · I) · W 4T :
1.2 (b) Explain how the convolution theorem helps in frequency domain
filtering
The Convolution Theorem states that:
That is, if f (x, y) is an image and h(x, y) is a filter, their convolution is:
This allows efficient filtering by simple multiplication rather than costly spatial convolution.
1.3 (c) Apply an ideal low-pass filter that retains only the DC compo-
nent
To apply an ideal low-pass filter that retains only the DC component, we set all frequency components
to zero except F (0, 0):
1360 0 0 0
0 0 0 0
Ffiltered =
0 0 0 0
0 0 0 0
The inverse DFT gives a uniform image:
1360
Ioutput = = 85
16
Thus, the output image is:
85 85 85 85
85 85 85 85
Ioutput =
85 85 85 85
85 85 85 85
This confirms that retaining only the DC component removes all variations, making the image
completely smooth.
2
2 Problem 2
Consider a 3 × 3 image patch:
1 2 3
I = 4 5 6
7 8 9
and the filter (kernel):
0 1 0
K = 1 −4 1
0 1 0
0 0 0 0 0
0 1 2 3 0
Ip = 0 4 5 6 0
0 7 8 9 0
0 0 0 0 0
To perform the convolution, we need to flip our kernel both vertically and horizontally. Since the
given kernel is symmetric so the flipped kernel will be the same, i.e.
0 1 0
Kflipped = 1 −4 1
0 1 0
Now, we slide the horizantally and vertically flipped kernel over the padded image and compute
the sum of element-wise multiplications.
For the center pixel at (1, 1):
(0 × 0) + (1 × 0) + (0 × 0) + (1 × 0) + (−4 × 1) + (1 × 2) + (0 × 0) + (1 × 4) + (0 × 5) = −4 + 2 + 4 = 2
2 1 −4
Iconv = −3 0 −7
−16 −11 −22
Thus, the convolution of I with K using zero-padding results in the above filtered image.
0 0 0 0 0
0 1 2 3 0
Ipadded = 0 4 5 6 0
0 7 8 9 0
0 0 0 0 0
3
Following the same element-wise multiplication procedure as convolution but without flipping the
kernel, we obtain:
2 1 −4
O= −3 0 −7
−16 −11 −22
• Convolution: The kernel is flipped, both horizontally and vertically, before applying it to the
image and computing the sum of element-wise multiplication.
2.4 Show how the kernel K can be decomposed into separable filters
A filter is separable if it can be expressed as the outer product of two 1D vectors, meaning:
K = v · wT
To check whether the given kernel:
0 1 0
Kflipped = 1 −4 1
0 1 0
K = v · wT
However, computing the rank of K shows that it is rank-2, meaning that it cannot be decomposed
into a single separable filter. In other words, this kernel is not separable.
1 1
K= 0 · 1 2 1 + 2 · 1 0 −1
−1 1
Let’s verify this decomposition.
4
First separable filter:
1 1 2 1
0 · 1 2 1 = 0 0 0
−1 −1 −2 −1
Second separable filter:
1 1 0 −1
2 · 1 0 −1 = 2 0 −2
1 1 0 −1
3 Problem 3
Given the following 5 × 5 grayscale image:
10 20 30 40 50
60 70 80 90 100
I = 110 120 130 140 150
160 170 180 190 200
210 220 230 240 250
Applying these kernels to the image, we compute the gradient magnitudes as:
q
G = G2x + G2y (2)
5
# De fine the Sob e l k e rn e l s
G x = np . a rr a y ([ [ − 1 , 0 , 1 ] ,
[ −2 , 0 , 2 ] ,
[ −1 , 0 , 1 ] ] )
G y = np . ar ra y ([ [ − 1 , −2, −1] ,
[0 , 0 , 0] ,
[1 , 2 , 1]])
# Fu nctio n to a p p ly the So b e l o p e r a to r
def s o b e l o p e r a t o r ( image , ke r n e l ) :
height , width = image . shape
k e r n e l s i z e = ke rn e l . shape [ 0 ]
p a d s i z e = k e r n e l s i z e // 2
padded image = np . pad ( image , pa d s ize , mode=’ c o n sta n t ’ )
output = np . z e r o s l i k e ( image , dtype=float )
for i in range ( he i g ht ) :
for j in range ( width ) :
output [ i , j ] = np . sum( padded image [ i : i+k e r n e l s i z e , j : j+k e r n e l s i z e
return output
# Di s p la y n u m e rica l o u tp u ts
print ( ” Or ig in a l - Image - ( I ) : ” )
print ( I )
# P lo t the r e s u l t s
p l t . f i g u r e ( f i g s i z e =(15 , 10 ))
# Plot O r i g i n a l Image
p l t . s u b pl o t ( 2 , 2 , 1 )
p l t . imshow ( I , cmap=’ gray ’ , vmin=0 , vmax=255)
p l t . t i t l e ( ’ Or ig in a l - Image - ( I ) ’ )
p l t . c o lo r b a r ()
6
# Pl o t Grad ie n t in th e x−d i re c t i o n ( G x )
p l t . s u b pl o t ( 2 , 2 , 2 )
p l t . imshow ( g r a d i e n t x , cmap=’ gray ’ , vmin=−500, vmax=500)
p l t . t i t l e ( ’ Gra die nt - in - the - x−d i r e c t i o n - ( G x) ’ )
p l t . c o lo r b a r ()
# Pl o t Gra di e nt in th e y−d i re c t i o n ( G y)
p l t . s u b pl o t ( 2 , 2 , 3 )
p l t . imshow ( gr a d ie n t y , cmap=’ gray ’ , vmin=−500, vmax=500)
p l t . t i t l e ( ’ Gra die nt - in - the - y−d i r e c t i o n - ( G y) ’ )
p l t . c o lo r b a r ()
p l t . t i g h t l a y o u t ()
p l t . show ()
The output of the above code is as follows:
Original Image (I):
10 20 30 40
60 70 80 90 100 50
110 120 130 140
150
160 170 180 190 200
210 220 230 240 250
Gradient in the x-direction (Gx):
110 60 60 60 −170
280 80 80 80 −360
480 80 80 80 −560
680 80 80 80 −760
610 60 60 60 −670
7
Figure 1: Plot of the outputs.
G y = np . ar ra y ([ [ − 1 , −2, −1] ,
[0 , 0 , 0] ,
[1 , 2 , 1]])
# Fu nctio n to a p p ly the So b e l o p e r a to r
def s o b e l o p e r a t o r ( image , ke r n e l ) :
height , width = image . shape
8
k e r n e l s i z e = ke rn e l . shape [ 0 ]
p a d s i z e = k e r n e l s i z e // 2
padded image = np . pad ( image , pa d s ize , mode=’ c o n sta n t ’ )
output = np . z e r o s l i k e ( image , dtype=float )
for i in range ( he i g ht ) :
for j in range ( width ) :
output [ i , j ] = np . sum( padded image [ i : i+k e r n e l s i z e , j : j+k e r n e l s i z e
return output
# Conve rt edge d i re c t i o n s to d e g re e s
e d g e d i r e c t i o n s d e g = np . de gre e s ( e d g e d i r e c t i o n s )
# Di s p l a y n u me ri ca l o u t p u t s
print ( ” Gr a die nt - in - the - x−d i r e c t i o n - ( G x ) : ” )
print ( g ra d i e n t x )
# P lo t the r e s u l t s
p l t . f i g u r e ( f i g s i z e =(15 , 10 ))
# Plot O r i g i n a l Image
p l t . s u b pl o t ( 2 , 2 , 1 )
p l t . imshow ( I , cmap=’ gray ’ , vmin=0 , vmax=255)
p l t . t i t l e ( ’ Or ig in a l - Image - ( I ) ’ )
p l t . c o lo r b a r ()
# Pl o t Grad ie n t in th e x−d i re c t i o n ( G x )
p l t . s u b pl o t ( 2 , 2 , 2 )
p l t . imshow ( g r a d i e n t x , cmap=’ gray ’ , vmin=−500, vmax=500)
p l t . t i t l e ( ’ Gra die nt - in - the - x−d i r e c t i o n - ( G x) ’ )
p l t . c o lo r b a r ()
# Pl o t Gra di e nt in th e y−d i re c t i o n ( G y)
p l t . s u b pl o t ( 2 , 2 , 3 )
p l t . imshow ( gr a d ie n t y , cmap=’ gray ’ , vmin=−500, vmax=500)
9
p l t . t i t l e ( ’ Gradie nt - in - the - y−d i r e c t i o n - ( G y) ’ )
p l t . c o lo r b a r ()
# P lot Edge D i r ec ti o n s
p l t . s u b pl o t ( 2 , 2 , 4 )
p l t . imshow ( e d g e d i r e c t i o n s d e g , cmap=’ hsv ’ , vmin=−180, vmax=180)
p l t . t i t l e ( ’ Edge - D i r e c t i o n s - ( in - de gre e s ) ’ )
p l t . c o lo r b a r ()
p l t . t i g h t l a y o u t ()
p l t . show ()
The numerical output of the code is as follows:
Gradient in the x-direction (Gx):
10
Figure 2: Plot of the outputs.
11
3.3 Explain how non-maximum suppression in the Canny edge detector
refines edges.
Non-maximum suppression (NMS) is a step in the Canny edge detector that makes edges thinner
and more precise. It works by keeping only the strongest edges and removing weaker ones. Here’s
how it works:
2. Working:
• For each pixel, check its gradient direction to determine the edge orientation (e.g., horizontal,
vertical, or diagonal).
• Compare the pixel’s gradient magnitude with its neighbors along the edge direction.
• If the pixel’s gradient magnitude is not the largest among its neighbors, remove it (set its value
to zero).
• Keep the pixel only if it has the highest gradient magnitude in its edge direction.
Summary
Non-maximum suppression refines edges by keeping only the strongest pixels along the edge direction
and removing the rest. This makes edges thin, precise, and free from noise.
article amsmath
4 Problem 4
A 1D signal is given as:
S = [3, 7, 15, 20, 23, 18, 12, 5, 2]
A Gaussian kernel with σ = 1 is given as:
1
G= [1, 4, 6, 4, 1]
16
4.1 (a) Apply Gaussian filtering to smooth the signal using discrete
convolution.
To apply Gaussian filtering, we perform discrete convolution of the signal S with the Gaussian kernel
G. The steps are as follows:
12
Step 2: Perform Convolution
The convolution of Spadded with G is computed as:
Σ
2
Ssmoothed[i] = Spadded[i + k] · G[k]
k=−2
Here, G = 1
[1, 4, 6, 4, 1],
16 and k ranges from −2 to 2.
Ssmoothed = [3.8125, 8.375, 14, 18.5625, 19.8125, 17.0625, 11.8125, 6.5, 2.75]
13
• Weighted Smoothing: The Gaussian weights decrease with distance from the center, so
nearby pixels have a stronger influence, while distant pixels have less impact. This ensures
that noise is smoothed out without distorting the signal too much.
• Frequency Domain: In the frequency domain, the Gaussian kernel acts as a low-pass filter,
attenuating high-frequency noise while retaining low-frequency components of the signal.
5 Problem 5
Consider a 2D Gaussian function:
1 x 2 + y2
G(x, y) = exp − ,
2πσ2 2σ2
where σ = 1.
∂G x
= G(x, y) · − ,
∂x σ2
∂G y
= G(x, y) · − .
∂y σ2
The second partial derivatives are:
∂2G x2 1
= G(x, y) · − ,
∂x2 σ4 σ2
∂2G y2 1
= G(x, y) · 4 − 2 .
∂y2 σ σ
∇2G = G(x, y) · x2 + y2 − 2 .
14
5.2 (b) Sketch the zero-crossings of the Laplacian of Gaussian function
and explain how they help in blob detection.
Zero-Crossings of the Laplacian of Gaussian (LoG):
The zero-crossings of the LoG occur where ∇2 G = 0. From part (a), this happens when:
x2 + y2 − 2 = 0 ⇒ x2 + y2 = 2.
√
This is a circle of radius 2 centered at the origin.
• By identifying zero-crossings, we can locate the centers and sizes of blobs in an image.
15
• The DoG is often used in practical implementations (e.g., in the SIFT algorithm) because it
provides a good approximation of the LoG.
Comparison:
• Accuracy: LoG is more accurate, but DoG is a good approximation.
• Practical Use: DoG is preferred in real-time applications due to its computational efficiency.
import numpy as np
import m a tp lo t l ib . pyplot as p l t
from s c ip y . s i g n a l import c onv olve 2 d
# Ro ta te c o o r d i n a t e s
x pri me = x ∗ np . c os ( t he t a ) + y ∗ np . s in ( t he t a )
y pri me = −x ∗ np . s in ( t he t a ) + y ∗ np . cos ( t he t a )
return gabor
16
kernel size = 5 # Size o f th e Gabor f i l t e r k e rn e l
# Di s p l a y n u me ri ca l o u t p u t s
print ( ” Or ig in a l - Image - Patch : ” )
print ( ima ge pa tc h )
# P lo t the o r i g i n a l image
p l t . s u b pl o t ( 1 , 3 , 1 )
p l t . imshow ( ima ge patch , cmap=’ gray ’ , vmin=0 , vmax=255)
p l t . t i t l e ( ” Or ig in a l - Image” )
p l t . c o lo r b a r ()
# P lo t the f i l t e r e d image
p l t . s u b pl o t ( 1 , 3 , 3 )
p l t . imshow ( f i l t e r e d i m a g e , cmap=’ gray ’ )
p l t . t i t l e ( ” Fi l t e r e d - Image - ( C onvolut ion ) ” )
p l t . c o lo r b a r ()
p l t . t i g h t l a y o u t ()
p l t . show ()
Gabor Filtering Output:
17
Figure 4: Gabor Filtering numerical Output.
How Gabor Filters are Useful for Edge Detection and Texture
Analysis
Gabor filters are widely used in image processing for edge detection and texture analysis due to their
ability to capture both spatial and frequency information. They are particularly effective because
they combine the properties of a Gaussian envelope and a sinusoidal wave, making them sensitive to
specific orientations and scales in an image.
1. Edge Detection
Gabor filters are highly effective for edge detection because they can detect variations in intensity at
specific orientations. Here’s how they work for edge detection:
• Orientation Sensitivity: By tuning the orientation parameter (θ), Gabor filters can highlight
edges in different directions. For example, a Gabor filter with θ = 0 will detect vertical edges,
while θ = π2 will detect horizontal edges.
18
• Scale Sensitivity: The wavelength parameter (λ) controls the scale of the edges detected.
Smaller values of λ detect fine edges, while larger values detect coarse edges.
• Localized Response: The Gaussian envelope ensures that the filter responds strongly to
edges in a localized region, making it robust to noise.
Mathematically, the Gabor filter for edge detection can be expressed as:
x′2 + γ2y′2 x′
G(x, y; λ, θ, ψ, σ, γ) = exp — cos 2π +ψ ,
2σ2 λ
where:
• x′ = x cos θ + y sin θ,
• y′ = −x sin θ + y cos θ,
• λ is the wavelength of the sinusoidal factor,
Applications
Gabor filters are used in a variety of applications, including:
• Face Recognition: Gabor filters extract facial features by capturing texture and edge infor-
mation.
• Medical Imaging: Gabor filters analyze textures in medical images, such as detecting tumors
in MRI scans.
19