Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory error using scipy.fftpack, how to avoid it?

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

like image 847
KvasDub Avatar asked Apr 25 '26 11:04

KvasDub


1 Answers

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
like image 197
Saullo G. P. Castro Avatar answered Apr 27 '26 01:04

Saullo G. P. Castro