傅里叶变换(python)

import cv2
import matplotlib.pyplot as plt
import matplotlib.cm as cm
%matplotlib inline
def imshow(img):
    plt.imshow(img,cm.gray_r,interpolation='nearest')
import numpy as np
dim=31
img=np.zeros((dim,dim))
mx=int(dim/2)
for i in range(dim):
    for j in range(dim):
        if ((i-mx)*(i-mx)+(j-mx)*(j-mx))<10:
            img[i,j]=255
imshow(img)

傅里叶变换(python)_第1张图片
F[i,j]=Mp=1Nq=1f(p,q)e2πi(ipM+jqN)

lv=np.complex(0,1)
def FT(img):
    w,h=img.shape
    res=np.zeros((h,w))
    for i in range(h):
        for j in range(w):
            temp=-1*lv*2*np.pi
            for p in range(h):
                for q in range(w):
                    res[i,j]+=img[p,q]*np.exp(temp*(i*p/h+j*q/w))
            res[i,j]=np.abs(res[i,j])
    return res
ft=FT(img)
imshow(ft)

傅里叶变换(python)_第2张图片

b=np.fft.fft2(img)
b=np.abs(b)
imshow(b)

你可能感兴趣的:(编程)