0% found this document useful (0 votes)
5 views13 pages

Computer Vision hw2

The document outlines a homework assignment on linear filtering in computer vision, specifically focusing on white balance and the implementation of Sobel's edge detection using separable filters. It details the steps for applying horizontal and vertical filters to an image, including calculations for intermediate and final results, and discusses the effects of filter order on results. Additionally, it includes the derivation and implementation of directed Gaussian derivative filters in MATLAB.

Uploaded by

zahraaalfaeq
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)
5 views13 pages

Computer Vision hw2

The document outlines a homework assignment on linear filtering in computer vision, specifically focusing on white balance and the implementation of Sobel's edge detection using separable filters. It details the steps for applying horizontal and vertical filters to an image, including calculations for intermediate and final results, and discusses the effects of filter order on results. Additionally, it includes the derivation and implementation of directed Gaussian derivative filters in MATLAB.

Uploaded by

zahraaalfaeq
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/ 13

Computer Vision

Homework 2:
Linear Filtering

Pesented by:

Zahraa muhaned mohammed faeq

MSc stg 1
HW1: White Balance

1. Perform the following operation, and show your work both graphically

(as in the lecture notes) and numerically (in terms of the

corresponding multiply-add operations).

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:

After applying Correlation to all pixels, the final result is:

I filtered=[13 20 17 ;18 24 18 ; -13 -20 -17]


2. Implement Sobel’s horizontal edge detector as a separable filter

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?

What did each 1D filter do?

Sol//

image = imread('C:\Users\ahmed\Pictures\computer
vision\Bike.png');
image = rgb2gray(image);

u = [1 0 -1];
v = [1; 2; 1];

intermediateResult = conv2(double(image), u, 'same');


finalResult = conv2(intermediateResult, v, 'same');

figure('Position', [100, 100, 1200, 400]);

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:

 u^T=[1,2,1] (horizontal smoothing).


 v^=[−1,0,1]T (vertical derivative).

Applying the 1D Filters Separately

We will apply the filters to the following example image:

I=

1.Apply u^T (Horizontal Smoothing)

The filter u^T=[1,2,1] is applied horizontally to each row of the image.

Calculation for the row1 :

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]

 This performs a weighted average (smoothing) in the horizontal direction.


 This reduces noise and emphasizes horizontal structures in the image.

2 Apply v^ (Vertical Derivative)

The filter v^=[−1,0,1] T is applied vertically to each column of the intermediate result.

Calculation for the column1:

8⋅ -1+20⋅0+32⋅1= 24

20⋅ -1+32⋅0+0⋅1= -20

32⋅ -1+0⋅0+0⋅1= -32

Column 2:
8⋅ -1+17⋅0+26⋅1= 18

17⋅ -1+26⋅0+0.1 = -17

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

Final result after applying v^:

Ifinal=[24 18 6 ; -20 -17 -6 ; -32 -26 -9 ]

 This detects edges in the vertical direction.


 This highlights vertical changes in intensity (edges).
3. Reverse the order of applying the 1D filters in step 2 (apply v̂ then ûT).
Show the results. Did the middle results change? Did the final results change?
Why?

sol//
In Matlab:

image = imread('C:\Users\ahmed\Pictures\computer
vision\Bike.png');
image = rgb2gray(image);

u = [1 0 -1];
v = [1; 2; 1];

intermediateResult = conv2(double(image), v, 'same');


finalResult = conv2(intermediateResult, u, 'same');

figure('Position', [100, 100, 1200, 400]);

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.

2. Did the final results change?


- No, the final results did not change. The final result after applying both filters (vertical and horizontal) r
emains the same regardless of the order.
- This is because convolution is a linear operation, and the Sobel filter is separable.

1. The Separable Filter:

H=v^⋅u^T=[−1 0 1]T⋅[1 2 1]

o u^T=[1,2,1] (horizontal smoothing filter).


o v^=[−1,0,1]T (vertical derivative filter).
2. Reversing the Order:
o Step 1: Apply v^ (vertical derivative) to the original image.
o Step 2: Apply u^T (horizontal smoothing) to the result.

Application to the Image:

The original image:

I=

1.Apply v ^=[−1,0,1]T (Vertical Derivative)

The filter v^ is applied vertically to each column of the image.

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

Intermediate Result After v^:

Imid2=[6 6 6; -4 -5 -6 ; -7 -8 -9 ]

2.Apply u^T=[1,2,1](Horizontal Smoothing)

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

% Convert theta to radians


theta_rad = deg2rad(theta);

% Compute the filter coefficients


[first_derivative_filter] = first_derivative_gaussian(sigma,
theta_rad);
[second_derivative_filter] = second_derivative_gaussian(sigma,
theta_rad);
[fourth_derivative_filter] = fourth_derivative_gaussian(sigma,
theta_rad);

% Display the filter coefficients


disp('First Derivative Filter Coefficients:');
disp(first_derivative_filter);

disp('Second Derivative Filter Coefficients:');


disp(second_derivative_filter);

disp('Fourth Derivative Filter Coefficients:');


disp(fourth_derivative_filter);
end

function [filter] = first_derivative_gaussian(sigma, theta)


% Define the grid
[x, y] = meshgrid(-3*sigma:sigma:3*sigma, -3*sigma:sigma:3*sigma);

% Gaussian function
G = (1 / (2 * pi * sigma^2)) * exp(-(x.^2 + y.^2) / (2 * sigma^2));

% First derivative in x and y


Gx = -(x / sigma^2) .* G;
Gy = -(y / sigma^2) .* G;

% Directed first derivative


filter = cos(theta) * Gx + sin(theta) * Gy;
end
function [filter] = second_derivative_gaussian(sigma, theta)
% Define the grid
[x, y] = meshgrid(-3*sigma:sigma:3*sigma, -3*sigma:sigma:3*sigma);

% Gaussian function
G = (1 / (2 * pi * sigma^2)) * exp(-(x.^2 + y.^2) / (2 * sigma^2));

% Second derivative in x and y


Gxx = ((x.^2 - sigma^2) / sigma^4) .* G;
Gyy = ((y.^2 - sigma^2) / sigma^4) .* G;
Gxy = (x .* y / sigma^4) .* G;

% Directed second derivative


filter = cos(theta)^2 * Gxx + 2 * cos(theta) * sin(theta) * Gxy +
sin(theta)^2 * Gyy;
end

function [filter] = fourth_derivative_gaussian(sigma, theta)


% Define the grid
[x, y] = meshgrid(-3*sigma:sigma:3*sigma, -3*sigma:sigma:3*sigma);

% Gaussian function
G = (1 / (2 * pi * sigma^2)) * exp(-(x.^2 + y.^2) / (2 * sigma^2));

% Fourth derivative in x and y


Gxxxx = ((x.^4 - 6 * x.^2 * sigma^2 + 3 * sigma^4) / sigma^8) .* G;
Gyyyy = ((y.^4 - 6 * y.^2 * sigma^2 + 3 * sigma^4) / sigma^8) .* G;
Gxxyy = ((x.^2 .* y.^2 - x.^2 * sigma^2 - y.^2 * sigma^2 + sigma^4)
/ sigma^8) .* G;

% Directed fourth derivative


filter = cos(theta)^4 * Gxxxx + 4 * cos(theta)^3 * sin(theta) *
Gxxyy + 6 * cos(theta)^2 * sin(theta)^2 * Gxxyy + 4 * cos(theta) *
sin(theta)^3 * Gxxyy + sin(theta)^4 * Gyyyy;
end

You might also like