TypeError: src data type = 15 is not supported

2019-02-25 03:46发布

问题:

I want to use Fast Fourier Transform but already trying a simple back and forth transformation doesn't work. The code is

import cv2
import numpy as np

img = cv2.imread('Picture.bmp',0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])

and the error is

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    img_back = cv2.idft(f_ishift)
TypeError: src data type = 15 is not supported

How can this be fixed?

回答1:

I think I figured it out. cv2.idft() wants the complex numbers in a different format. I had to extract the real and imaginary part separately and write them in the third dimension:

import cv2
import numpy as np

img = cv2.imread('Bild.bmp',0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
f_ishift = np.fft.ifftshift(fshift)
d_shift = np.array(np.dstack([f_ishift.real,f_ishift.imag]))
img_back = cv2.idft(d_shift)
img = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])