My main goal is to show that the convolution theorem works (just a reminder: the convolution theorem means that idft(dft(im) .* dft(mask)) = conv(im, mask)
). I'm trying to program that.
Here is my code:
function displayTransform( im )
% This routine displays the Fourier spectrum of an image.
%
% Input: im - a grayscale image (values in [0,255])
%
% Method: Computes the Fourier transform of im and displays its spectrum,
% (if F(u,v) = a+ib, displays sqrt(a^2+b^2)).
% Uses display techniques for visualization: log, and stretch values to full range,
% cyclic shift DC to center (use fftshift).
% Use showImage to display and fft2 to apply transform.
%displays the image in grayscale in the Frequency domain
imfft = fft2(im);
imagesc(log(abs(fftshift(imfft))+1)), colormap(gray);
% building mask and padding it with Zeros in order to create same size mask
b = 1/16*[1 1 1 1;1 1 1 1; 1 1 1 1; 1 1 1 1];
paddedB = padarray(b, [floor(size(im,1)/2)-2 floor(size(im,2)/2)-2]);
paddedB = fft2(paddedB);
C = imfft.*paddedB;
resIFFT = ifft2(C);
%reguler convolution
resConv = conv2(im,b);
showImage(resConv);
end
I want to compare resIFFT
and resConv
. I think I'm missing some casting because I am getting numbers in the matrix closer one to another if I'm using casting to double.
Maybe I have some mistake in the place of the casting or the padding?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…