Question: Write the function gradient_magnitude that takes one input: a grayscale image (a matrix, not a filename). This function should return an image (a matrix, not
Write the function gradient_magnitude that takes one input: a grayscale image (a matrix, not a filename). This function should return an image (a matrix, not a file) of type double with the same dimensions as the input. This output contains the magnitude of the gradient. You should compute the gradient using the Sobel masks using your spatial_filter function(code below).
function [ outputImage ] = spatial_filter( I,M )
%SPATIAL_FILTER Summary of this function goes here
% Detailed explanation goes here
corr = M;
pad1 = size(corr,1) - 1;
pad2 = size(corr,2) - 1;
output = uint8(zeros(size(I)));
if size(corr,1) == 1
B = zeros(size(I,1),size(A,2)+pad2);
m = 0;
n = floor(size(corr,2)/2);
sz1 = size(B,1);
sz2 = size(B,2) - pad2;
elseif size(corr,2) == 1
B = zeros(size(A,1) + pad1, size(A,2));
m = floor(size(corr,1)/2);
n = 0;
sz1 = size(B,1) - pad1;
sz2 = size(B,2);
else
B = zeros(size(I,1) + pad1, size(I,2) + pad2);
m = floor(size(corr,1)/2);
n = floor(size(corr,2)/2);
sz1 = size(B,1) - pad1;
sz2 = size(B,2) - pad2;
end
for i = 1:size(I,1)
for j = 1:size(I,2)
B(i+m,j+n) = I(i,j);
end
end
szcorr1 = size(corr,1);
szcorr2 = size(corr,2);
for i = 1:sz1
for j = 1:sz2
sum = 0;
m = i;
n = j;
for x = 1:szcorr1
for y = 1:szcorr2
sum = sum + (B(m,n)*corr(x,y));
n = n + 1;
end
n = j;
m = m + 1;
end
output(i,j) = sum;
end
end
outputImage = double(output);
end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
