傅立叶变换的逆给出“不支持的数据类型”错误

时间:2018-11-13 13:15:40

标签: python opencv image-processing fft opencv3.0

我正在尝试实现傅立叶逆变换。这是我的代码:

i=i

这样做时会发生错误:

import numpy as np 
import cv2 as cv
import math 
import cmath
from matplotlib import pyplot as plt
image="test2.bmp" 
img=cv.imread(image,cv.IMREAD_GRAYSCALE)
#foruior transform 
dft = cv.dft(np.float32(img),flags = cv.DFT_COMPLEX_OUTPUT)
#shifting for displaying 
dft_shift = np.fft.fftshift(dft)
#get the filter contains real and complex part 
t=1
a=0.1 
b=0.1
motion_filter=np.empty((img.shape[0],img.shape[1],2),dtype=np.complex_)
for x in range(img.shape[0]):
    for y in range(img.shape[1]):
        if x==0 and y==0:
            const1=math.pi*(1e-10)
        else:
            const1=math.pi*((x*a)+(y*b)) 
        #for real number
        motion_filter[x,y,0]=((t/const1)*(math.sin(const1))*(cmath.exp((0-1j)*(const1)))).real
        #for complex number
        motion_filter[x,y,1]=((t/const1)*(math.sin(const1))*(cmath.exp((0-1j)*(const1)))).imag
#processing
fshift = dft_shift*motion_filter
#shift back 
f_ishift = np.fft.ifftshift(fshift)
#inverse 
img_back = cv.idft(f_ishift)
#take real part 
img_back=img_back[:,:,0]
#show image 
plt.imshow(img_back,cmap='gray')
plt.show()

错误消息是:

  

不支持src数据类型= 15

如何修复代码?

1 个答案:

答案 0 :(得分:1)

根据this other question中的答案,OpenCV idft需要一个实值矩阵,其中实数和虚数分量都沿第三维存储。您创建此矩阵:

motion_filter=np.empty((img.shape[0],img.shape[1],2),dtype=np.complex_)

它的大小合适(对于实数和虚数分量,第3维的大小为2),但是它是复数值。接下来,将您的傅立叶域图像(在第三维上具有实部和虚部的实值矩阵)与该复杂矩阵相乘,以创建复杂值的输出:

fshift = dft_shift*motion_filter

此复数值输出不能在cv.idft中使用。而是将您的过滤器矩阵创建为实值矩阵:

motion_filter=np.empty((img.shape[0],img.shape[1],2))
相关问题