3

I've just started to learn about images frecuency domain.

I have this function:

def fourier_transform(img):
    f = np.fft.fft2(img)
    fshift = np.fft.fftshift(f)
    magnitude_spectrum = 20*np.log(np.abs(fshift))

    return magnitude_spectrum

And I want to implement this function:

def inverse_fourier_transform(magnitude_spectrum):

    return img

But I don't know how.

My idea is to use magnitude_spectrum to get the original img.

How can I do it?

5
  • 3
    You can't do it with just the manitudes, the phase information is lost
    – harold
    Dec 4, 2019 at 15:23
  • @harold Thanks. What can I do to don't lost the phase information?
    – VansFannel
    Dec 4, 2019 at 15:24
  • Having f would do it, or (more complicated) both the absolute values and the angles. What's convenient depends on what sort of processing you want to apply in frequency space
    – harold
    Dec 4, 2019 at 15:29
  • why assume the image is in the time domain ? I see you send the image into a fft call to transform it into the frequency domain ... why not consider the image to live in the frequency domain so its first transition will be into the time domain ... this does not obviate the need to create an inverse fourier transform step however it might make thinking about the necessary data transforms easier ... I wrote an inverse fft to send an image into audio ( freq domain -> time domain ) using golang .. if you do not want to craft this yourself many fft libraries come with its counterpart ifft function Dec 4, 2019 at 15:57
  • 3
    Just return fshift rather than magnitude_spectrum. You can reapply fftshift and ifft2 to get your input image again
    – tstanisl
    Dec 4, 2019 at 17:49

2 Answers 2

2

You are loosing phases here: np.abs(fshift).

np.abs takes only real part of your data. You could separate the amplitudes and phases by:

abs = fshift.real
ph = fshift.imag

In theory, you could work on abs and join them later together with phases and reverse FFT by np.fft.ifft2.

EDIT: You could try this approach:

import numpy as np
import matplotlib.pyplot as plt

# single chanel image
img = np.random.random((100, 100))
img = plt.imread(r'path/to/color/img.jpg')[:,:,0]

# should be only width and height
print(img.shape)

# do the 2D fourier transform
fft_img = np.fft.fft2(img)

# shift FFT to the center
fft_img_shift = np.fft.fftshift(fft_img)

# extract real and phases
real = fft_img_shift.real
phases = fft_img_shift.imag

# modify real part, put your modification here
real_mod = real/3

# create an empty complex array with the shape of the input image
fft_img_shift_mod = np.empty(real.shape, dtype=complex)

# insert real and phases to the new file
fft_img_shift_mod.real = real_mod
fft_img_shift_mod.imag = phases

# reverse shift
fft_img_mod = np.fft.ifftshift(fft_img_shift_mod)

# reverse the 2D fourier transform
img_mod = np.fft.ifft2(fft_img_mod)

# using np.abs gives the scalar value of the complex number
# with img_mod.real gives only real part. Not sure which is proper
img_mod = np.abs(img_mod)

# show differences
plt.subplot(121)
plt.imshow(img, cmap='gray')
plt.subplot(122)
plt.imshow(img_mod, cmap='gray')
plt.show()
4
  • Did you mean log(fshift) instead of fshift when computing magnitude and phase?
    – tstanisl
    Dec 4, 2019 at 17:50
  • 2
    from numpy import abs as npabs ; print(npabs(3+4j)) gives me 5.0 – under which assumptions is 5 the real part of 3+4j ?????
    – gboffi
    Dec 6, 2019 at 10:39
  • According to numpy docs: An ndarray containing the absolute value of each element in x. For complex input, a + ib, the absolute value is \sqrt{ a^2 + b^2 }. This is a scalar if x is a scalar.
    – Dawid
    Dec 7, 2019 at 16:33
  • 1
    @gboffi is right. You say that abs returns the real part of the data. It doesn’t. If returns the magnitude. There’s an important difference! Dec 7, 2019 at 16:53
2

You cannot recover the exact original image without the phase information, so you cannot only use the magnitude of the fft2. To use the fft2 to recover the image, you just need to call numpy.fft.ifft2. See the code below:

import numpy as np
from numpy.fft import fft2, ifft2, fftshift, ifftshift

#do the 2D fourier transform
fft_img = fftshift(fft2(img))

# reverse the 2D fourier transform
freq_filt_img = ifft2(ifftshift(fft_img))

freq_filt_img = np.abs(freq_filt_img)
freq_filt_img = freq_filt_img.astype(np.uint8)

Note that calling fftshift and ifftshift is not necessary if you just want to recover the original image directly, but I added them in case there is some plotting to be done in the middle or some other operation that requires the centering of the zero frequency.

The result of calling numpy.abs() or freq_filt_img.real (assuming positive values for each pixel) to recover the image should be the same because the imaginary part of the ifft2 should be really small. Of course, the complexity of numpy.abs() is O(n) while freq_filt_img.real is O(1)

2
  • "The result of calling numpy.abs() or freq_filt_img.real to recover the image should be the same [...]" except if img has negative values. Also, getting the real component is cheap (O(1) operation in Python), whereas abs is not (O(n), including an expensive square root for each pixel). Sep 2, 2020 at 22:18
  • Yes, I was assuming positive values for each pixel. I edited the post for clarity. Thank you Sep 2, 2020 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.