0% found this document useful (0 votes)
22 views

CV_Assignment2_SC22B109

The document presents a computer vision assignment involving the computation of 2D Discrete Fourier Transform (DFT) for a grayscale image, convolution and correlation operations with a kernel, and gradient magnitude calculation using the Sobel operator. It includes detailed mathematical derivations, explanations of the convolution theorem, and the application of ideal low-pass filtering. Additionally, it discusses the separability of filters and provides Python code for gradient computation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

CV_Assignment2_SC22B109

The document presents a computer vision assignment involving the computation of 2D Discrete Fourier Transform (DFT) for a grayscale image, convolution and correlation operations with a kernel, and gradient magnitude calculation using the Sobel operator. It includes detailed mathematical derivations, explanations of the convolution theorem, and the application of ideal low-pass filtering. Additionally, it discusses the separability of filters and provides Python code for gradient computation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

AVD641 - Computer Vision

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

1.1 (a) Compute the 2D Discrete Fourier Transform (DFT)


The 2D Discrete Fourier Transform (DFT) is given by:

F = W4 · I · W4T
where W4 is the DFT basis matrix, defined as:

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 :

1360 −80 + 80j −80 −80 − 80j


−320 + 320j 0 0 0
F=
−320 0 0 0
−320 − 320j 0 0 0

Thus, the final frequency domain representation is:

1360 −80 + 80j −80 −80 − 80j


−320 + 320j 0 0 0
F=
−320 0 0 0
−320 − 320j 0 0 0

1.2 (b) Explain how the convolution theorem helps in frequency domain
filtering
The Convolution Theorem states that:

Convolution in the spatial domain ↔ Multiplication in the frequency domain

That is, if f (x, y) is an image and h(x, y) is a filter, their convolution is:

g(x, y) = f (x, y) ∗ h(x, y)

which in the frequency domain is:

G(u, v) = F (u, v) · H(u, v)

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

2.1 Compute the convolution of I with K using zero-padding.


To perform convolution, we first apply zero-padding to the image I. This extends I into a×
55
matrix:

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

Computing for all positions, the output convolved image is:

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.

2.2 Compute the correlation of I with K.


Correlation is computed by sliding K over I without flipping K.
Using zero-padding, the padded image is:

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

2.3 Explain the difference between convolution and correlation.


Convolution and correlation are similar operations, but differ in the handling of the kernel:

• Convolution: The kernel is flipped, both horizontally and vertically, before applying it to the
image and computing the sum of element-wise multiplication.

• Correlation: The kernel is applied directly without flipping the kernel.

• When the kernel is symmetric, both operations give identical results.

Convolution is commonly used in filtering, while correlation is useful in finding similarities or


template matching.

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

is separable, we need to determine whether it can be written as an outer product of two 1D


vectors.
For K to be separable, its rank must be 1, meaning that there exist two 1D vectors v and w such
that:

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.

2.5 Approximate Separable Decomposition of the given kernel K.


2.5.1 Step 1: Expressing K as a Sum of Two Rank-1 Matrices
We aim to decompose K into the sum of two separable filters:

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

Summing the two matrices:


1 2 1 1 0 −1 0 1 0
0 0 0 + 2 0 −2 = 1 −4 1
−1 −2 −1 1 0 −1 0 1 0
Since the original kernel K is reconstructed, we confirm that K can be decomposed into these
two separable filters.

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

3.1 Compute the gradient magnitudes using the Sobel operator.


The Sobel operator uses two kernels:
−1 0 1 −1 −2 −1
Gx = −2 0 2 , Gy = 0 0 0 (1)
−1 0 1 1 2 1

Applying these kernels to the image, we compute the gradient magnitudes as:
q
G = G2x + G2y (2)

The following Python code is used to compute the gradients:


import numpy as np
import m a tp lo t l ib . pyplot as p l t

# De fin e th e image matrix


I = np . a rra y ( [
[ 1 0 , 20 , 30 , 40 , 5 0 ] ,
[ 6 0 , 70 , 80 , 90 , 1 0 0 ] ,
[ 1 1 0 , 120 , 130 , 140 , 1 5 0 ] ,
[ 1 6 0 , 170 , 180 , 190 , 2 0 0 ] ,
[ 2 1 0 , 220 , 230 , 240 , 250 ]
])

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

# Compute the g r a d i e n t s in the x and y d i re c t i o n s


g ra d i e n t x = s o b e l o p e r a t o r ( I , G x)
g ra d i e n t y = s o b e l o p e r a t o r ( I , G y)

# Compute the g r a d i e n t magnitude


gr a d ie nt m a gn it u d e = np . s q r t ( g ra d i e n t x ∗∗2 + g ra d i e n t y ∗∗ 2 )

# 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 )

print ( ” \n Gradie nt - in - the - x−d i r e c t i o n - ( G x ) : ” )


print ( g ra d i e n t x )

print ( ” \n Gra dient - in - the - y−d i r e c t i o n - ( G y ) : ” )


print ( g ra d i e n t y )

print ( ” \n Gra die nt - magnitude - (G) : ” )


print ( g ra die nt mag nit ude )

# 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 lo t Gra d ie nt magnitude (G)


p l t . s u b pl o t ( 2 , 2 , 4 )
p l t . imshow ( gr a die nt ma gn it ude , cmap=’ gray ’ , vmin=0 , vmax=600)
p l t . t i t l e ( ’ Gr a die nt - magnitude - (G) ’ )
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

Gradient in the y-direction (Gy):

190 280 320 360 290


300 400 400 400 300
300 400 400 400 300
300 400 400 400 300
−490 −680 −720 −760 −590

Gradient magnitude (G):

219.544984 286.35642127 325.57641192 364.96575182 336.15472628


410.36569057 407.92156109 407.92156109 407.92156109 468.61498055
407.92156109 407.92156109 407.92156109 635.29520697
566.03886792
743.23616704 407.92156109 407.92156109 407.92156109 817.06792863
782.43210568 682.64192664 722.49567473 762.3647421 892.74856483

The plot of outputs are as follows:

7
Figure 1: Plot of the outputs.

3.2 (b) Identify the edge directions.


The python code below computes and display the edge directions:
import numpy as np
import m a tp lo t l ib . pyplot as p l t

# De fin e th e image matrix


I = np . a rra y ( [
[ 1 0 , 20 , 30 , 40 , 5 0 ] ,
[ 6 0 , 70 , 80 , 90 , 1 0 0 ] ,
[ 1 1 0 , 120 , 130 , 140 , 1 5 0 ] ,
[ 1 6 0 , 170 , 180 , 190 , 2 0 0 ] ,
[ 2 1 0 , 220 , 230 , 240 , 250 ]
])

# 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

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

# Compute the g r a d i e n t s in the x and y d i re c t i o n s


g ra d i e n t x = s o b e l o p e r a t o r ( I , G x)
g ra d i e n t y = s o b e l o p e r a t o r ( I , G y)

# Compute the g r a d i e n t magnitude


gr a d ie nt m a gn it u d e = np . s q r t ( g ra d i e n t x ∗∗2 + g ra d i e n t y ∗∗ 2 )

# Compute the edge d i re c t i o n s ( in r a d ia n s )


e d g e d i r e c t i o n s = np . a rcta n 2 ( gr a d ie n t y , g ra d i e n t x )

# 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 )

print ( ” \n Gra dient - in - the - y−d i r e c t i o n - ( G y ) : ” )


print ( g ra d i e n t y )

print ( ” \nEdge - D i r e c t i o n s - ( in - de gre e s ) : ” )


print ( e d g e d i r e c t i o n s d e g )

# 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):

110. 60. 60. 60. −170.


280. 80. 80. 80. −360.
Gx = 480. 80. 80. 80. −560.
680. 80. 80. 80. −760.
610. 60. 60. 60. −670.

Gradient in the y-direction (Gy):

190. 280. 320. 360. 290.


300. 400. 400. 400. 300.
Gy = 300. 400. 400. 400. 300.
300. 400. 400. 400. 300.
−490. −680. −720. −760. −590.

Edge Directions (in degrees):


Edge Directions =
59.93141718 77.90524292 79.38034472 80.53767779 120.37912601
46.97493401 78.69006753 78.69006753 78.69006753 140.19442891
32.00538321 78.69006753 78.69006753 78.69006753 151.82140989
23.80594352 78.69006753 78.69006753 78.69006753 158.45902408
−38.77417094 −84.95754893 −85.23635831 −85.48601154 −138.63295074
The graphical output is as follows:

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:

1. What Does NMS Do?


After computing the gradient magnitude (edge strength) and gradient direction (edge orientation),
edges are often thick or blurry. NMS fixes this by keeping only the pixels with the highest gradient
magnitude along the edge direction.

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:

Step 1: Pad the Signal


To handle edge effects, we pad the signal S with zeros on both sides. Since the kernel has length 5,
we add 2 zeros on the left and 2 zeros on the right:

Spadded = [0, 0, 3, 7, 15, 20, 23, 18, 12, 5, 2, 0, 0]

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.

Step 3: Compute Each Output Value


The smoothed signal is computed as follows:
1 1 61
Ssmoothed[1] = (0 · 1 + 0 · 4 + 3 · 6 + 7 · 4 + 15 · 1) = (0 + 0 + 18 + 28 + 15) =
= 3.8125
16 16 16
1 1 134
Ssmoothed[2] = (0 · 1 + 3 · 4 + 7 · 6 + 15 · 4 + 20 · 1) = (0 + 12 + 42 + 60 + 20) = = 8.375
16 16 16
1 1 224
Ssmoothed[3] = (3 · 1 + 7 · 4 + 15 · 6 + 20 · 4 + 23 · 1) = (3 + 28 + 90 + 80 + 23) = = 14
16 16 16
1 1 297
Ssmoothed[4] = (7 · 1 + 15 · 4 + 20 · 6 + 23 · 4 + 18 · 1) = (7 + 60 + 120 + 92 + 18) = = 18.5625
16 16 16
1 1 317
Ssmoothed[5] = (15 · 1 + 20 · 4 + 23 · 6 + 18 · 4 + 12 · 1) = (15 + 80 + 138 + 72 + 12) = = 19.8125
16 16 16
1 1 273
Ssmoothed[6] = (20 · 1 + 23 · 4 + 18 · 6 + 12 · 4 + 5 · 1) = (20 + 92 + 108 + 48 + 5) = = 17.0625
16 16 16
1 1 189
Ssmoothed[7] = (23 · 1 + 18 · 4 + 12 · 6 + 5 · 4 + 2 · 1) = (23 + 72 + 72 + 20 + 2) = = 11.8125
16 16 16
1 1 104
Ssmoothed[8] = (18 · 1 + 12 · 4 + 5 · 6 + 2 · 4 + 0 · 1) = (18 + 48 + 30 + 8 + 0) = = 6.5
16 16 16
1 1 44
Ssmoothed[9] = (12 · 1 + 5 · 4 + 2 · 6 + 0 · 4 + 0 · 1) = (12 + 20 + 12 + 0 + 0) = = 2.75
16 16 16

Final Smoothed Signal


The smoothed signal after Gaussian filtering is:

Ssmoothed = [3.8125, 8.375, 14, 18.5625, 19.8125, 17.0625, 11.8125, 6.5, 2.75]

4.2 (b) Explain the effect of increasing σ on the output.


Increasing σ in the Gaussian kernel has the following effects:
• Wider Kernel: A larger σ results in a wider Gaussian kernel, which means more neighboring
pixels are included in the smoothing process.
• Stronger Smoothing: The signal becomes smoother as more pixels are averaged together,
reducing high-frequency variations.
• Blurrier Edges: Fine details and sharp edges in the signal are blurred, as the kernel spreads
out the influence of each pixel over a larger area.

4.3 (c) Why does Gaussian filtering help in reducing noise?


Gaussian filtering helps reduce noise because:
• Averaging Effect: The Gaussian kernel averages pixel values with their neighbors, which
reduces random variations (noise) while preserving the overall structure of the signal.

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.

5.1 (a) Compute the Laplacian of this Gaussian function.


The Laplacian of a 2D function G(x, y) is given by:
∂2G ∂2G
∇ G = ∂x2
2
+ .
∂y2

Step 1: Compute the second partial derivatives of G(x, y).


The Gaussian function is:
1 x 2 + y2
G(x, y) = exp — .
2πσ2 2σ2
The first partial derivatives are:

∂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 σ σ

Step 2: Add the second partial derivatives to get the Laplacian.


2 ∂2G ∂2G x 2 + y2 2
∇G= + = G(x, y) · − .
∂x2 ∂y2 σ4 σ2
For σ = 1, this simplifies to:

∇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.

Figure 3: Zero-crossings of the Laplacian of Gaussian Function.

How Zero-Crossings Help in Blob Detection:


• The zero-crossings of the LoG correspond to the boundaries of blobs in an image.
• Blobs are regions of uniform intensity, and the LoG highlights these regions by detecting changes
in intensity (edges).

• By identifying zero-crossings, we can locate the centers and sizes of blobs in an image.

5.3 (c) Describe how Laplacian of Gaussian compares to Difference of


Gaussians in practical implementations.
Laplacian of Gaussian (LoG):
• The LoG is a single operator that combines Gaussian smoothing and Laplacian edge detection.

• It is computationally expensive because it requires computing second derivatives.

Difference of Gaussians (DoG):


• The DoG approximates the LoG by subtracting two Gaussian-smoothed images with different
σ values.
• It is computationally cheaper than the LoG because it avoids second derivatives.

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.

• Computation: DoG is faster and more efficient.

• Practical Use: DoG is preferred in real-time applications due to its computational efficiency.

6 Problem 6 (Additional Reading)


6.1 Compute the response of the image patch in Problem 3 to a Gabor
filter with parameters λ = 4, θ = 0, ϕ = 0, σ = 1, andγ = 0.5.

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

def g a b o r f i l t e r ( lambda , the ta , psi , sigma , gamma, s i z e ) :


”””
Generate a Gabor f i l t e r k e r n e l .
: param lambda : Wavelength o f the s in u s o i d a l f a c t o r .
: param t h e ta : Or ie n ta tio n o f the f i l t e r .
: param p s i : Phase o f f s e t .
: param sigma : Standard d e v i a t i o n o f the Gaussian e n ve lo p e .
: param gamma: Sp a t ia l aspec t ra t i o .
: param s i z e : Size o f the k e r n e l ( s i z e x s i z e ) .
: re tu rn : Gabor f i l t e r ke rn e l .
”””
# Crea te a g r id o f x and y c o o r d i n a t e s
x = np . ara nge( − s i z e // 2 + 1 , s i z e // 2 + 1 )
y = np . ara nge(− s i z e // 2 + 1 , s i z e // 2 + 1 )
x , y = np . me s hgri d ( x , y)

# 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 )

# Compute the Gabor f i l t e r k e rn e l


gabor = np . exp(−( x prime ∗∗2 + gamma∗∗2 ∗ y prime ∗∗ 2 ) / (2 ∗ sigma ∗∗ 2 )) ∗ \
np . c os (2 ∗ np . pi ∗ x pr ime / lambda + p s i )

return gabor

# De fine the p arame te rs


lambda = 4 # Wavelength
the ta = 0 # Ori en ta ti o n
psi = 0 # Phase o f f s e t
sigma = 1 # Stand ard d e v i a t i o n o f the Gaussian e n ve lo p e
gamma = 0 . 5 # Sp a t i a l a s p e ct ra t i o

16
kernel size = 5 # Size o f th e Gabor f i l t e r k e rn e l

# De fine the image p atch from Problem 3


ima ge pa tc h = np . a r ra y ( [
[ 1 0 , 20 , 30 , 40 , 5 0 ] ,
[ 6 0 , 70 , 80 , 90 , 1 0 0 ] ,
[ 1 1 0 , 120 , 130 , 140 , 1 5 0 ] ,
[ 1 6 0 , 170 , 180 , 190 , 2 0 0 ] ,
[ 2 1 0 , 220 , 230 , 240 , 250 ]
])

# Gene rate the Gabor f i l t e r k e rn e l


g a b o r ke rn e l = g a b o r f i l t e r ( lambda , the ta , psi , sigma , gamma, k e r n e l s i z e )

# Perform 2D c o n v o l u t i o n between the image patch and the Gabor f i l t e r k e r n e l


f i l t e r e d i m a g e = convolve 2 d ( image patch , ga bor k e rn e l , mode=’ same ’ , boundary=’s

# 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 )

print ( ” \nGabor - F i l t e r - Ke rnel : ” )


print ( g a b o r ke rn e l )

print ( ” \n F ilt e r e d - Image - ( Af te r - Convolution ) : ” )


print ( f i l t e r e d i m a g e )

# P lo t the o r i g i n a l image , Gabor filter ke rn e l , and f i l t e r e d image


p l t . f i g u r e ( f i g s i z e =(12 , 5 ))

# 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 lot the Gabor f i l t e r k e rn e l


p l t . s u b pl o t ( 1 , 3 , 2 )
p l t . imshow ( ga b or k e r ne l , cmap=’ gray ’ )
p l t . t i t l e ( ”Gabor - F i l t e r - Ker ne l ” )
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.

Figure 5: Gabor Filtering Output Visualization.

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,

• θ is the orientation of the filter,

• ψ is the phase offset,

• σ is the standard deviation of the Gaussian envelope,

• γ is the spatial aspect ratio.

Applications
Gabor filters are used in a variety of applications, including:

• Fingerprint Recognition: Gabor filters enhance fingerprint images by highlighting ridges


and valleys.

• 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

You might also like