I was reading a text that shows that one can throw away 66% of the coefficients and gets an image that is still acceptable.

I tried to replicate it using python (last example) and selectively throwing away some of the coefficients but I couldn't get a great image, mine is looking like a ghost.

How can I discard that not so important to the image?

fft discarded coefficient example

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from scipy import signal

# loading the image
img = mpimg.imread('i/super_mario_head.png')

# image in frequency domain
fft_original = np.fft.rfft2(img)

# discarding some coefficients (except initial square 80x80)
for x in range(300):
    for y in range(300):
        discard_x = x > 80
        discard_y = y > 80

        if discard_x and discard_y:
            fft_original[x,y] = 0

# reverse from frequency to spatial
i_fft_original = np.fft.irfft2(fft_original)

f, (plt1, plt2) = plt.subplots(1, 2, figsize=(15,15))

plt1.axis('off');plt1.set_title('Original');plt1.imshow(img[:,:,0], cmap='gray');
plt2.axis('off');plt2.set_title('Reversed');plt2.imshow(i_fft_original[:,:,0], cmap='gray');
  • 1
    $\begingroup$ Compare not real but absolute value of fft. Or just pick some radius in the fourier domain to throw away everything outside of that radius. $\endgroup$ – mathreadler Mar 9 '17 at 22:13

You are throwing away coefficients before checking to see if their current magnitudes are small (smaller than typically visible).

(You might also want to rescale what's left to preserve total energy.)

  • $\begingroup$ Thanks, do you know how can I check if its magnitudes are small? (ex: fft_original[x,y] <= 0.01 ? ) $\endgroup$ – leandro moreira Mar 8 '17 at 20:51
  • $\begingroup$ "small" is relative to the psychology and physiology of human vision. Test, or read published research for prior opinions. $\endgroup$ – hotpaw2 Mar 8 '17 at 20:55
  • $\begingroup$ Thanks, a lot :D I'll try fft_original[x,y,0].real <= MIN $\endgroup$ – leandro moreira Mar 8 '17 at 20:59
  • $\begingroup$ I tried fft_original[x,y,0].real < 20 but it caused also issues and then I tried a range query > -2 and < 20 but it still weird. $\endgroup$ – leandro moreira Mar 8 '17 at 21:08
  • $\begingroup$ The size of a "small" enough coefficient is likely not a constant, but varies greatly depending on the location in the 2D FFT and what's nearby in both the FFT and the image. $\endgroup$ – hotpaw2 Mar 8 '17 at 21:38

Find largest value of fft_original and then Use a threshold which is 1/N times of it - say 1/10. Also compare with absolute value of each sample so that high magnitude negative values are preserved.

  • $\begingroup$ Thanks, is it the highest real value? or the imaginary part? $\endgroup$ – leandro moreira Mar 8 '17 at 21:15
  • $\begingroup$ I tried with 1/10, threshold = (1/10) * max_fft and then compared using absolutes values if abs(fft_original[x,y,0].real) <= threshold: but it still looking ghostly. $\endgroup$ – leandro moreira Mar 8 '17 at 21:23
  • $\begingroup$ I changed the color plane which I was working on (from 0 to 2) and then it looks more acceptable now. $\endgroup$ – leandro moreira Mar 8 '17 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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