Python 离散傅里叶变换:低通滤波器不能切断正确的频率
我在Python 离散傅里叶变换:低通滤波器不能切断正确的频率,python,opencv,numpy,python-3.5,Python,Opencv,Numpy,Python 3.5,我在python3.5中使用cv2.dft(),步骤如下 余弦图像为: N=32 Icos=np.零((32,32)) 对于范围(0,N)内的i: 对于范围(0,N)内的j: myPi=2*math.pi/N th1=8*i+6*j th2=4*i+2*j th3=2*j Icos[i,j]=(0.25*math.cos(myPi*th1)+0.75*math.cos(myPi*th2)+math.cos(myPi*th3)) 图像=Icos dft=cv2.dft(np.float32(图像)
python3.5
中使用cv2.dft()
,步骤如下
余弦图像为:
N=32
Icos=np.零((32,32))
对于范围(0,N)内的i:
对于范围(0,N)内的j:
myPi=2*math.pi/N
th1=8*i+6*j
th2=4*i+2*j
th3=2*j
Icos[i,j]=(0.25*math.cos(myPi*th1)+0.75*math.cos(myPi*th2)+math.cos(myPi*th3))
图像=Icos
dft=cv2.dft(np.float32(图像),flags=cv2.dft\u复数输出)
#向中间移动
dft_shift=np.fft.fft移位(dft)
#震级计算
ms=np.log(1+cv2.量级(dft_移位[:,:,0],dft_移位[:,:,1]))
屏蔽输出='屏蔽低通'
掩码=np.0((行,列,2),np.uint8)
对于范围(0,行)中的i:
对于范围内的j(0,cols):
point=math.sqrt(math.pow((i-crow),2)+math.pow((j-ccol),2))
如果点
N=32
Icos = np.zeros((32,32))
for i in range(0,N):
for j in range(0,N):
myPi = 2*math.pi/N
th1 = 8*i + 6*j
th2 = 4*i + 2*j
th3 = 2*j
Icos[i,j] = (0.25 * math.cos(myPi*th1) + 0.75*math.cos(myPi*th2) + math.cos(myPi*th3))
image = Icos
dft = cv2.dft(np.float32(image),flags =cv2.DFT_COMPLEX_OUTPUT)
#Shift to the center
dft_shift = np.fft.fftshift(dft)
#Magnitude Calculation
ms = np.log(1+cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
mask_out = 'Mask LowPass'
mask = np.zeros((rows,cols,2),np.uint8)
for i in range(0,rows):
for j in range(0,cols):
point = math.sqrt(math.pow((i-crow),2)+math.pow((j-ccol),2))
if point <= radius1:
mask[i,j]=1
#apply mask
fshift = dft_shift*mask
#Inverse
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
#Fourier Transform of the resulted image
dft_res = cv2.dft(np.float32(img_back),flags =cv2.DFT_COMPLEX_OUTPUT)
#Shift to the center
dft_shift_res = np.fft.fftshift(dft_res)
#Magnitude Calculation
ms_res = np.log(1+cv2.magnitude(dft_shift_res[:,:,0],dft_shift_res[:,:,1]))