This document contains solutions to two questions about image processing techniques. Question 1 involves implementing Sobel edge detection using 4 convolution masks and calculating the approximate gradient magnitude. Question 2 involves implementing a corner detector by building a neighborhood matrix around each pixel, computing its singular values via SVD, and highlighting locations where the second singular value exceeds a threshold after removing neighboring duplicates.
1. Tutorial 3
Question 1
Write a program to implement Sobel edge detector using 4 convolution masks and the
approximate gradient magnitude calculation.
Question 2
Write a program to implement corner detector. The neighborhood is q x q, where q is the
first input of the function. The second input is the threshold. Highlight the location of
each detected corner by drawing a box in the neighborhood.
Tutorial 3 Solution
Question 1
function sobeled4(t)
% to perform Sobel edge detection using 4 convolution masks
% and approximate gradient magnitude calculation
% threshold is t
[f_name, f_path] = uigetfile('*.bmp', 'Select an input image');
in_name = [f_path, f_name];
image = imread(in_name);
[height, width] = size(image);
imshow(image)
h = fspecial('sobel');
sx = imfilter(image, h);
v = h';
sy = imfilter(image, v);
d = [2 1 0; 1 0 -1; 0 -1 -2];
sd = imfilter(image, d);
a = [0 1 2; -1 0 1; -2 -1 0];
sa = imfilter(image, a);
for i = 1:height
for j = 1:width
mag(i,j) = abs(sx(i,j))+abs(sy(i,j))+abs(sd(i,j))+abs(sa(i,j));
if mag(i,j) > t
sedimage(i,j) = 255;
else
sedimage(i,j) = 0;
end
end
end
figure
imshow(sedimage)
% save edge detection result
out_name = [f_path, 'sed4_', f_name];
imwrite(sedimage, out_name, 'BMP');
end
2. Question 2
Select image
Compute Gx, Gy
Compute Gx2, Gy2, GxGy
For each pixel
Build matrix C
Use svd to compute λ
If λ2 is larger than threshold
Save coordinates and λ2
Use sort to sort λ2 in decreasing order
Delete neighboring corners with lower λ2
Overlay corners on original image and show result