Question

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!

Was it helpful?

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]));

OTHER TIPS

You can pad at the higher frequencies directly (without fftshift suggested by Luis Mendo)

>> BIG = padarray( amp*imFFT, [20 20], 0, 'post' );
>> big = ifft2( BIG );

If you want a strictly real result, then before you do the IFFT you need to make sure the zero-padded array is exactly conjugate symmetric. Adding the zeros off-center could prevent this required symmetry.

Due to finite numerical precision, you may still end up with a complex IFFT result, but the imaginary components will all be tiny values that are essentially equivalent to zero.

Your FFT library may contain a half-to-real (quarter-size input for 2D) version that enforces symmetry and throws away the almost-zero numerical noise for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top