Python 二维离散傅里叶变换

Python 二维离散傅里叶变换

需要的库

import numpy as np
import cv2
import matplotlib.pyplot as plt

计算两张图片的PSNR

def PSNR(A, B):
    MSE = np.sum((A - B) ** 2) / A.shape[0] / A.shape[1]
    return 10 * np.log10((255 ** 2) / MSE)

二维离散傅里叶变换

def DFT_2(f, N, M):
    '''
    :param f: 2_dim marix
    :param N: even; denote number of rows
    :param M: even; denote number of cols
    :return: DFT results without shifting(complex number)
    '''
    if N % 2 != 0 or M % 2 != 0:
        print("param is wrong")
        return
    f = f.astype('float64')
    rows = f.shape[0]
    cols = f.shape[1]
    n = np.arange(0, N, 1).reshape((N, 1))
    row = np.arange(0, rows, 1).reshape((1, rows))
    left = np.exp(-1j*2*np.pi/N*(n @ row))
    col = np.arange(0, cols, 1).reshape((cols, 1))
    m = np.arange(0, M, 1).reshape((1, M))
    right = np.exp(-1j*2*np.pi/M*(col @ m))
    F = left @ f @ right
    return F

二维离散傅里叶逆变换

def IDFT_2(F, rows, cols, option=True):
    '''
    :param F: 2-dim freqency spectrum without shifting
    :param rows: rows of original picture
    :param cols: cols of original picture
    :param option: whether make some process for results
    :return: picture in space domain
    '''
    if cols % 2 != 0 or rows % 2 != 0:
        print("IDFT params are wrong")
        return
    N = F.shape[0]
    M = F.shape[1]
    row = np.arange(0, rows, 1).reshape((rows, 1))
    n = np.arange(0, N, 1).reshape((1, N))
    left = np.exp(1j*2*np.pi/N*(row @ n))
    m = np.arange(0, M, 1).reshape((M, 1))
    col = np.arange(0, cols, 1).reshape((1, cols))
    right = np.exp(1j*2*np.pi/M*(m @ col))
    f = (left @ F @ right) / rows / cols
    if option:
        f = np.abs(f)
        f = np.around((f - np.min(f)) / (np.max(f) - np.min(f)) * 255)
    return f

其中option为True时,返回值为复数的模,否则返回值为复数。


频域平移

DFT的结果具有共轭对称性,因此需要将结果平移以后才能看出结果的这种对称性。

def DFT_shifting(F):
    F_shift = 1j + np.zeros(F.shape)
    N = F.shape[0]
    M = F.shape[1]
    F_shift[N>>1:N, M>>1:M] = F[0:N>>1, 0:M>>1]
    F_shift[N>>1:N, 0:M>>1] = F[0:N>>1, M>>1:M]
    F_shift[0:N>>1, M>>1:M] = F[N>>1:N, 0:M>>1]
    F_shift[0:N>>1, 0:M>>1] = F[N>>1:N, M>>1:M]
    return F_shift

其中F表示上述DFT_2()求出的频域结果。


绘制频域图像

由于DFT得到的结果是复数,需要对其求模值,同时取log

def DFT_2_pic(f, N, M):
    '''
    :param f: 2_dim marix
    :param N: even; denote number of rows
    :param M: even; denote number of cols
    :return: DFT results with shifting(complex number) and make log
    '''
    if N % 2 != 0 or M % 2 != 0:
        print("param is wrong")
        return
    F = DFT_2(f, N, M)
    shift = DFT_shifting(F)
    F_pic = np.log2(np.abs(shift))
    F_pic = np.round((F_pic-np.min(F_pic)) / (np.max(F_pic) - np.min(F_pic)) * 255)   # make pixel value in 0 - 255
    F_pic = F_pic.astype('uint8')
    return shift, F_pic

其中F_pic返回取对数以后的频谱;shift表示进行平移以后的复数频谱。


二维离散傅里叶变换(2D DFT)是一种将二维离散信号转换到频域的方法,它在图像处理和信号处理中广泛应用。移位特性是指在进行2D DFT时,对输入信号进行平移操作,会导致频域中的相位谱发生相应的平移。 在MATLAB中,可以使用fft2函数来进行二维离散傅里叶变换。该函数的语法如下: ```matlab Y = fft2(X) ``` 其中,X是输入的二维离散信号,Y是变换后的频域表示。默认情况下,MATLAB会对输入信号进行零填充以满足变换的要求。 对于移位特性,可以通过对输入信号进行平移操作来观察频域中的相位谱平移。具体操作如下: ```matlab % 生成一个二维方波信号 X = zeros(64, 64); X(16:48, 16:48) = 1; % 进行二维离散傅里叶变换 Y = fft2(X); % 对输入信号进行平移操作 X_shifted = circshift(X, [10, 10]); % 进行平移后的二维离散傅里叶变换 Y_shifted = fft2(X_shifted); % 显示原始信号和平移后的信号 subplot(2, 2, 1); imshow(X); title('原始信号'); subplot(2, 2, 2); imshow(abs(Y), []); title('频域表示'); subplot(2, 2, 3); imshow(X_shifted); title('平移后的信号'); subplot(2, 2, 4); imshow(abs(Y_shifted), []); title('平移后的频域表示'); ``` 上述代码中,首先生成一个二维方波信号X,然后进行二维离散傅里叶变换得到频域表示Y。接着对输入信号进行平移操作,生成平移后的信号X_shifted,并进行平移后的二维离散傅里叶变换得到频域表示Y_shifted。最后通过subplot函数将原始信号、频域表示、平移后的信号和平移后的频域表示显示在一个图像窗口中。 希望以上介绍对您有帮助!如果还有其他问题,请随时提问。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值