Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Describe what the following MATLAB code does. The original function name has bee

ID: 3673803 • Letter: D

Question

Describe what the following MATLAB code does. The original function name has been replaced by fname. Insert the right line of code where it says "%comment taken out,"

function [gx,gy]=fname(IM,sigma)

%(comment taken out)

% [gx,gy]=fname(IM,sigma) (comment taken out)

%

epsilon=1e-2;

halfsize=ceil(sigma*sqrt(-2*log(sqrt(2*pi)*sigma*epsilon)));

size=2*halfsize+1;

%(comment taken out)

for i=1:size

    for j=1:size

        u=[i-halfsize-1 j-halfsize-1];

        hx(i,j)=gauss(u(1),sigma)*dgauss(u(2),sigma);

    end

end

hx=hx/sqrt(sum(sum(abs(hx).*abs(hx))));

%(comment taken out)

hy=hx';

%(comment taken out)

gx=imfilter(IM,hx,'replicate','conv');

gy=imfilter(IM,hy,'replicate','conv');

function y = gauss(x,sigma)

%(comment taken out)

y = exp(-x^2/(2*sigma^2)) / (sigma*sqrt(2*pi));

function y = dgauss(x,sigma)

%(comment taken out)

y = -x * gauss(x,sigma) / sigma^2;

Explanation / Answer

function [gx,gy]=fname(IM,sigma)
%fname Gradient using first order derivative of Gaussian.
% [gx,gy]=fname(IM,sigma) outputs the gradient image gx and gy of
% image IM using a 2-D Gaussian kernel. Sigma is the standard deviation of
% this kernel along both directions.
%
% Contributed by Guanglei Xiong (xgl99@mails.tsinghua.edu.cn)
% at Tsinghua University, Beijing, China.

%determine the appropriate size of kernel. The smaller epsilon, the larger
%size.
epsilon=1e-2;
halfsize=ceil(sigma*sqrt(-2*log(sqrt(2*pi)*sigma*epsilon)));
size=2*halfsize+1;
%generate a 2-D Gaussian kernel along x direction
for i=1:size
for j=1:size
u=[i-halfsize-1 j-halfsize-1];
hx(i,j)=gauss(u(1),sigma)*dgauss(u(2),sigma);
end
end
hx=hx/sqrt(sum(sum(abs(hx).*abs(hx))));
%generate a 2-D Gaussian kernel along y direction
hy=hx';
%2-D filtering
gx=imfilter(IM,hx,'replicate','conv');
gy=imfilter(IM,hy,'replicate','conv');

function y = gauss(x,sigma)
%Gaussian
y = exp(-x^2/(2*sigma^2)) / (sigma*sqrt(2*pi));

function y = dgauss(x,sigma)
%first order derivative of Gaussian
y = -x * gauss(x,sigma) / sigma^2;