Computer Vision hw2
Computer Vision hw2
Homework 2:
Linear Filtering
Pesented by:
MSc stg 1
HW1: White Balance
1. Perform the following operation, and show your work both graphically
Row1:
0.-1+0.-2+4.2+5.1= 13
0.-1+0.-2+0.3+4.1+5.2+6.1= 20
0.-2+0.3+5.2+6.1= 17
Row2:
1.-2 +2.-1+0+0+2.7+1.8= 18
(1⋅-1)+(2⋅-2)+(3⋅-1)+(4⋅0)+(5⋅0)+(6⋅0)+(7⋅1)+(8⋅2)+(9⋅1)= 24
1.-2+3.-2+0+0+8.1+9.2=18
Row3:
0.1+0.2+4.-2+5.-1= -13
0.1+0.2+0.3+4.-1+5.-2+6.-1= -20
0.2+0.3+5.-2+6.-1= -17
Final Result:
H = v̂ ∙ ûT
Filter each of the attached images using the ûT 1D filter first, and then filter the resulting images
using the v̂ 1D filter. Show the result of applying each filter?
Sol//
image = imread('C:\Users\ahmed\Pictures\computer
vision\Bike.png');
image = rgb2gray(image);
u = [1 0 -1];
v = [1; 2; 1];
subplot(1, 3, 1);
imshow(image, []);
title('Original Image');
subplot(1, 3, 2);
imshow(intermediateResult, []);
title('After Horizontal Filter');
subplot(1, 3, 3);
imshow(finalResult, []);
title('After Vertical Filter');
imwrite(uint8(intermediateResult),
'intermediate_result.png');
imwrite(uint8(finalResult), 'final_result.png');
Where:
I=
1⋅1+2⋅2+3.1=8
2⋅1+3⋅2+0⋅1=8
3⋅1+0⋅2+0⋅1=3
Row 2:
1⋅4+2⋅5+1⋅6=20
1⋅5+2⋅6+1⋅0=17
1⋅6+2⋅0+1⋅0=6
Row 3:
1⋅7+2⋅8+1⋅9=7+16+9=32
1⋅8+2⋅9+1⋅0=26
1⋅9+2⋅0+1⋅0=9
Result after applying u^ T:
I middle =[8 8 3 ; 20 17 6 ; 32 26 9]
The filter v^=[−1,0,1] T is applied vertically to each column of the intermediate result.
8⋅ -1+20⋅0+32⋅1= 24
Column 2:
8⋅ -1+17⋅0+26⋅1= 18
26⋅ -1+0⋅0+0⋅1=-26
Column 3:
3⋅ -1+6⋅0+9⋅1 = 6
6⋅ -1+9⋅0+0⋅1 = -6
9⋅ -1+0⋅0+0⋅1 = -9
sol//
In Matlab:
image = imread('C:\Users\ahmed\Pictures\computer
vision\Bike.png');
image = rgb2gray(image);
u = [1 0 -1];
v = [1; 2; 1];
subplot(1, 3, 1);
imshow(image, []);
title('Original Image');
subplot(1, 3, 2);
imshow(intermediateResult, []);
title('After Vertical Filter');
subplot(1, 3, 3);
imshow(finalResult, []);
title('After Horizontal Filter');
imwrite(uint8(intermediateResult),
'intermediate_result_reversed.png');
imwrite(uint8(finalResult), 'final_result_reversed.png');
1. Did the middle results change?
- Yes, the middle results changed. Applying the vertical filter first highlights vertical edges.
Applying the horizontal filter first highlights horizontal edges.
- Therefore, the intermediate results differ based on the order of filter application.
H=v^⋅u^T=[−1 0 1]T⋅[1 2 1]
I=
Column 1:
−1⋅1+0⋅4+1⋅7 = 6
−1⋅4+0⋅7+1⋅0= -4
−1⋅7+0⋅0+1⋅0= -7
Column 2:
−1⋅2+0⋅5+1⋅8 = 6
−1⋅5+0⋅8+1⋅0= -5
−1⋅8+0⋅0+1⋅0= -8
Column 3:
−1⋅3+0⋅6+1⋅9=−3+0+9=6
−1⋅6+0⋅9+1⋅0= -6
−1⋅9+0⋅0+1⋅0= -9
Imid2=[6 6 6; -4 -5 -6 ; -7 -8 -9 ]
The filter u^T is applied horizontally to each row of the intermediate result.
Row 1:
6⋅1+6⋅2+6⋅1 =24
6⋅1+6⋅2+(0)⋅1 =18
6⋅1+(0)⋅2+(0)⋅1= 6
Row 2:
−4⋅1+(−5)⋅2+(−6)⋅1=−20
−5⋅1+(−6)⋅2+(0)⋅1= -17
−6⋅1+(0)⋅2+(0)⋅1=−6
Row 3:
−7⋅1+(−8)⋅2+(−9)⋅1=−32
−8⋅1+(−9)⋅2+0⋅1=−26
−9⋅1+0⋅2+0⋅1=−9+0+0=−9
Final Result:
Ifinal=[24 18 6 ; -20 -17 -6 ; -32 -26 -9 ]
Results:
1. Intermediate Results:
o Imid2=[6 6 6; -4 -5 -6 ; -7 -8 -9 ]
o This result is completely different from the intermediate result when
applying u^T first.
2. Final Results:
o The final result (Ifinal)is the same in both cases:
Ifinal=[24 18 6 ; -20 -17 -6 ; -32 -26 -9 ]
Reason:
The separable filter (H=v^⋅u^T) satisfies the commutative property of outer product
multiplication, meaning the final result does not change regardless of the order of applying the
filters. However, the intermediate results are different because each filter processes a different
direction (horizontal vs. vertical).
4. Derive the formula of the directed 4th derivative of Gaussian filter.
5.Implement the formulas of the directed 1st, 2nd, and 4th derivative of Gaussian filters as MATLAB
functions. Each function should take the value of the standard deviation σ of the Gaussian and the
direction angle θ and give back the coefficients of the 2D filter with the appropriate size. Use the
following parameters: σ = 1, θ = 45° and show the corresponding coefficients of each filter.
Sol// in Matlab
function directed_gaussian_filters()
% Parameters
sigma = 1; % Standard deviation
theta = 45; % Direction angle in degrees
% Gaussian function
G = (1 / (2 * pi * sigma^2)) * exp(-(x.^2 + y.^2) / (2 * sigma^2));
% Gaussian function
G = (1 / (2 * pi * sigma^2)) * exp(-(x.^2 + y.^2) / (2 * sigma^2));
% Gaussian function
G = (1 / (2 * pi * sigma^2)) * exp(-(x.^2 + y.^2) / (2 * sigma^2));