matlabimage-processingfftimage-enlarge

image processing - enlarging an image using FFT (Matlab code)


I'm trying to write a simple matlab code which enlarges an image using fft. I tried the known algorithm for image expansion, which computes the Fourier transform of the image, pads it with zeros and computes the inverse Fourier of the padded image. However, the inverse Fourier transform returns an image which contains complex numbers. Therefore, when I'm trying to show the result using imshow, I'm getting the following error:

Warning: Displaying real part of complex input.

Do you have an idea what am I doing wrong?

my code:

im = imread('fruit.jpg');
imFFT = fft2(im);
bigger = padarray(imFFT,[10,10]);
imEnlarged = ifft2(bigger);

Thanks!


Solution

  • That's because the FFT returns values corresponding to the discrete (spatial) frequencies from 0 through Fs, where Fs is the (spatial) sampling rate. You need to insert zeros at high frequencies, which are located at the center of the returned FFT, not in its end.

    You can use fftshift to shift the high frequencies to the end, pad with zeros, and then shift back with ifftshift (thanks to @Shai for the correction):

    bigger = ifftshift(padarray(fftshift(imFFT),[10,10]));
    

    Also, note that padding with zeros decreases the values in the enlarged image. You can correct that using a suitable amplification factor amp, which in this case would be equal to (1+2*10/length(im))^2:

    bigger = ifftshift(padarray(fftshift(amp*imFFT),[10,10]));