user2323232
user2323232

Reputation: 249

Zero padding fourier of image

I'm trying to solve a question, given an image f(x,y) at size N,M with fourier transform F. we define function g, which its fourier transform G is define as follows:

G(x,y)=F(x,y) if x

which means that we pad the image with zeros. I tried to check it out using matlab with this code:

i1 = imread('1.bmp');
i1 = im2double(i1);
k=fft2(i1);
newmat = padarray(k,[84,84],0,'post');
mat2=ifft2(newmat);
imshow(mat2);

for some reason im getting a complex matrix, which I can't really tell something intersting about, what am I missing? (just to clarify, the image I tried has a size of 84x84).

Thanks!

Upvotes: 2

Views: 4186

Answers (1)

Cris Luengo
Cris Luengo

Reputation: 60444

The padding has to add high frequencies, which is not what you are doing. For a 1D FFT F, F(2) and F(end) correspond to the same frequency — in 2D this is exactly the same, for each image line along each image dimension. By padding with zeros by extending the array, you are creating a new F(end). That value no longer matches the one in F(2). For the inverse transform to be real-valued, those two values should be complex conjugates of each other.

The solution is to add the padding in the middle of the array, where the highest frequencies are. The easiest way to do this is to first use fftshift to move the zero frequency to the center of the array, then pad all around the array, then shift back:

k = fft2(i1);
k = fftshift(k);
k = padarray(k,[84,84]/2,'both');
k = ifftshift(k);
mat2 = ifft2(k);

This way you preserve the conjugate symmetry expected of the Fourier transform of a real-valued image.


It seems OP is confused about what happens when padding with zeros in different parts of the Fourier spectrum. Here's a little experiment:

% Create a test image, a simple Gaussian, purely real-valued
x = linspace(-3,3,84);
img = exp(-0.5*x.^2);
img = img.' * img;
imshow(img)

input image

% OP's method
k = fft2(img);
k = padarray(k,[84,84],0,'post');
k = complex(k); % This line does nothing
out = ifft2(k) * 4;
subplot(1,2,1); imshow(real(out)); title('real part')
subplot(1,2,2); imshow(imag(out)); title('imaginary part')

output

% Correct method
k = fft2(img);
k = fftshift(k);
k = padarray(k,[84,84]/2,'both');
k = ifftshift(k);
out = ifft2(k) * 4;
subplot(1,2,1); imshow(real(out)); title('real part')
subplot(1,2,2); imshow(imag(out)); title('imaginary part')

output

As you can see, when padding 'post', you introduce an asymmetry in the Fourier domain that translates to a non-real image in the spatial domain. In contrast, padding as I instructed in this answer leads to preserving the conjugate symmetry and hence a real-valued output (the imaginary part is all black).

(sorry for all the white space around the images)

Upvotes: 4

Related Questions