I use FFT to do filtering in frequency domain, but when I use big kernel I got shift on border, I think it's due to nature of cyclical convolution and I need to do more zero padding.
But how to calculate required padding size? should it be power of 2 in matlab?
Here is my matlab code:
tic
I= rgb2gray(imread('im.jpg'));
h = fspecial('gaussian',555);
F = fft2(double(I));
H = fft2(double(h), size(I,1), size(I,2));
F_fH = H.*F;
ffi = ifft2(F_fH);
toc
figure, imshow(ffi,[])
UPDATE:
Here is solution:
%The zero-padding is just a way to make cyclical convolutions
%(which is what FFT-based convolutions are) act like linear convolutions.
%the convolution theorem yields the desired linear convolution result only
%if x(n) and h(n) are padded with zeros prior to the DFT such that their
%respective lengths are Nx+Nh-1, essentially zeroing out all circular artifacts.
tic
%Frequency Domain Filtering
I= rgb2gray(imread('im.jpg'));
ker_sz= 55;
h = fspecial('gaussian',ker_sz);
F = fft2(double(I), size(I,1)+ker_sz-1, size(I,2)+ker_sz-1);
H = fft2(double(h), size(I,1)+ker_sz-1, size(I,2)+ker_sz-1);
F_fH = F.*H;
ffi = ifft2(F_fH);
ffi= ffi((ker_sz-1)/2+1:(ker_sz-1)/2+size(I,1), (ker_sz-1)/2+1:(ker_sz-1)/2+size(I,2));
toc
%Display results
figure, imshow(ffi,[])