I need to compute Fourier coefficients in Python from a .tiff medical image. The code produces a memory error:
for filename in glob.iglob ('*.tif'):
imgfourier = scipy.misc.imread (filename, flatten = True)
image = numpy.array([imgfourier])#make an array
# Take the fourier transform of the image.
F1 = fftpack.fft2(image)
# Now shift so that low spatial frequencies are in the center.
F2 = fftpack.fftshift(F1)
# the 2D power spectrum is:
psd2D = np.abs(F2)**2
print psd2D
Any help would be really appreciated! Thanks
I found this discussion where they identified memory leaks in scipy.fftpack, suggesting to use numpy.fft package instead. Also, you can save memory avoiding the intermediate variables:
import numpy as np
import glob
import scipy.misc
for filename in glob.iglob('*.tif'):
imgfourier = scipy.misc.imread (filename, flatten = True)
print np.abs(np.fft.fftshift(np.fft.fft2(np.array([imgfourier]))))**2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With