图像的2d DFT然后是它的逆

时间:2018-06-13 12:04:58

标签: python-3.x opencv image-processing dft

我使用以下公式计算2D-DFT: img1 = Wm @ img @ Wn,图像的大小是M N,这里,img是我的图像,@表示矩阵乘法。 Wm是M M矩阵给出的,  Wm(m,k)=((1 / M)^ 0.5)exp(( - j * 2 * pi k m)/ M)和Wn是由N * N矩阵给出的 WN(N,L)=((1 / N)^ 0.5)EXP(( - J * 2 * PI N)/ N) 其中M * N是图像的大小 但计算逆却不会产生原始图像。

import numpy as np
import math
import cv2
path='E:\\python programs\\peppers.pgm'

#load image into img
img=cv2.imread(path,0)

height,width=img.shape
img=img.astype(np.complex64)

#img1 will contain dft of image
img1=np.zeros((height,width),dtype=np.complex64)
img=img.astype(np.complex64)

#performing centering of image
for x in range(width):
        for y in range(height):
            img[y,x]=img[y,x]*pow(1,x+y)

#obtaining twiddle matrix Wm                
tmp=1/(pow(height,0.5))
Wm=np.zeros((height,height),dtype=np.complex64)
for x in range(height):
   for y in range(height):
       Wm[y,x]=tmp*math.exp((-1*6.28*x*y)/height)

##obtaining twiddle matrix Wn        
tmp1=1/(pow(width,0.5))
Wn=np.zeros((width,width),dtype=np.complex64)
for x in range(width):
   for y in range(width):
        Wn[y,x]=tmp1*math.exp((-1*6.28*x*y)/width)

#computing dft
img1=Wm@img@Wn        

#img2 will contain inverse dft                      
img2=np.zeros((height,width),dtype=np.complex64)

我通过在dft公式中用它们的复共轭代替Wm和Wn来计算idft     img2 = np.conj(Wm)@ img1 @ np.conj(Wn)

#performing decentering
for i in range(width):
        for j in range(height):
            img2[j,i]=img2[j,i]*pow(1,i+j)

img2=img2.astype(np.uint8)

print(img2)
cv2.imshow('image',img2)
cv2.waitKey(0)&0xFF
cv2.destroyAllWindows()

0 个答案:

没有答案