2

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!

user2323232
  • 249
  • 2
  • 11
  • It seems that you are zero-padding your complex matrix `k` with real numbers. When then performing an ifft I would suspect that those real numbers would become complex. Try padding your complex matrix `k` with complex zeros '0+i0'. I don't know whether this is actually going to solve your problem, but worth a try. – matiastofteby Sep 30 '18 at 08:09
  • thanks it works. please add it as an answer and il vote it... another thing, what is the explanation for the result?(same image but darker...) – user2323232 Sep 30 '18 at 09:13
  • I have added it as an answer. I do not know why your image becomes darker. Try to pad equal amount of zeros on the end of it to see whether it has an impact. Again, only speculating. – matiastofteby Sep 30 '18 at 09:37
  • WilliamW, it is normal that your image turns darker, because a consequence of zero padding before taking the fourier transform is that magnitudes decrease. Refer to [this topic](https://dsp.stackexchange.com/questions/8167/fft-zero-padding-amplitude-change) on the digital signal processing stack exchange if you are interested as to why, there is a pretty good explanation there. If you have doubled your image size, the luminosity should have decreased by a factor of two. – Floris Sep 30 '18 at 12:41
  • @FlorisSA you have a mistake, I padded after taking it to the fourier domain – user2323232 Sep 30 '18 at 13:40
  • @WilliamW: your selected answer is incorrect. Please take a second look, there is no way it solved your problem. – Cris Luengo Sep 30 '18 at 15:17
  • Also, @FlorisSA’s explanation for the dimming is correct. – Cris Luengo Sep 30 '18 at 15:20
  • @WilliamW as pointed out by others my answer is not correct and misleading. You should accept the other answers so I can delete my answer. – matiastofteby Sep 30 '18 at 16:59
  • @WilliamW; sorry, I should have made myself clearer. Matlabs convention for the `fft()` and `ifft()` functions is to have the factor `1/N` appear in the expression of the inverse transform (see [here](https://ch.mathworks.com/help/matlab/ref/ifft.html#bvizm5c-7), the second equation is the IDFT). Therefore, if you increase the number of samples by a factor of two before you take the **inverse** transform, you will increase the `N` in this equation by a factor of two as well, thereby halving the amplitudes. Hope this makes more sense! – Floris Oct 01 '18 at 08:25
  • @matiastofteby you were correct, it was since 0 is not complex, i had to add this line: newmat=complex(newmat); , anyway if you want I can accept other answer or you can just update , since your answer was indeed correct.(just not fully) – user2323232 Oct 01 '18 at 16:20
  • @CrisLuengo also, I'm talking about padding the fft not before fft... so how is his answer relevant? – user2323232 Oct 01 '18 at 16:46

1 Answers1

4

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)

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120