使用opencv-python接口实现 2-D FFT

import numpy as np
import cv2


def shift_dft(src, dst=None):
    '''
        Rearrange the quadrants of Fourier image so that the origin is at
        the image center. Swaps quadrant 1 with 3, and 2 with 4.
    
        src and dst arrays must be equal size & type
    '''
    
    if dst is None:
        dst = np.empty(src.shape, src.dtype)
    elif src.shape != dst.shape:
        raise ValueError("src and dst must have equal sizes")
    elif src.dtype != dst.dtype:
        raise TypeError("src and dst must have equal types")
    
    if src is dst:
        ret = np.empty(src.shape, src.dtype)
    else:
        ret = dst
    
    h, w = src.shape[:2]
    
    cx1 = cx2 = int(w/2) # cx1 = cx2 = w/2
    cy1 = cy2 = int(h/2)
    
    # if the size is odd, then adjust the bottom/right quadrants
    if w % 2 != 0:
        cx2 += 1
    if h % 2 != 0:
        cy2 += 1
   
    # swap quadrants
    
    # swap q1 and q3
    ret[h-cy1:, w-cx1:] = src[0:cy1 , 0:cx1 ]   # q1 -> q3
    ret[0:cy2 , 0:cx2 ] = src[h-cy2:, w-cx2:]   # q3 -> q1

    # swap q2 and q4
    ret[0:cy2 , w-cx2:] = src[h-cy2:, 0:cx2 ]   # q2 -> q4
    ret[h-cy1:, 0:cx1 ] = src[0:cy1 , w-cx1:]   # q4 -> q2

    if src is dst:
        dst[:,:] = ret

    return dst


if __name__ == "__main__":
    
    #im2 = cv2.imread('E:\\Self\\BackGround\\test.jpg', cv2.IMREAD_GRAYSCALE) # cv2.IMREAD_COLOR
    # two way to change BGR to GRAY, it doesn't the same total;
    im = cv2.imread('E:\\Self\\BackGround\\test.jpeg')
    im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    
    im = cv2.resize(im, (512, 512), interpolation=cv2.INTER_LINEAR)
    
    h, w = im.shape[:2]
    
    realInput = im.astype(np.float64)
    
    # perform an optimally sized dft
    dft_M = cv2.getOptimalDFTSize(w)
    dft_N = cv2.getOptimalDFTSize(h)
    
    # copy A to dft_A and pad dft_A with zeros
    dft_A = np.zeros((dft_N, dft_M, 2), dtype=np.float64)
    dft_A[:h, :w, 0] = realInput
    

    # no need to pad bottom part of dft_A with zeros because of
    # use of nonzeroRows parameter in cv2.dft()
    
    cv2.dft(dft_A, dst=dft_A, nonzeroRows=h)    # 获取两个mat,一个存放dft变换的实部,一个存放虚部;初始时,实部就是图像本身,虚部为0
   
    cv2.namedWindow("test")
    cv2.imshow("test", im)
    
    
    # Split fourier into real and imaginary parts
    image_Re, image_Im = cv2.split(dft_A)
    
    # Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
    magnitude = cv2.sqrt(image_Re**2.0 + image_Im**2.0)
    
    # Compute log(1 + Mag)
    log_spectrum = cv2.log(1.0 + magnitude) # 计算 dft 变换后的幅值; log(1+ sqrt( Re(DFT(img))**2 + Im(DFT(img))**2 ))

    
    # Rearrange the quadrants of Fourier image so that the origin is at the image center
    shift_dft(log_spectrum, log_spectrum)

    #shift_dft(im, im)  # to display the change of the origin image
    
    
    # normalize and display the results as rgb
    cv2.normalize(log_spectrum, log_spectrum, 0.0, 1.0, cv2.NORM_MINMAX) # 线性归一化到 0.0 ~ 1.0 中
    cv2.namedWindow("magnitude")
    cv2.imshow("magnitude", log_spectrum)
 
    cv2.waitKey(0)
    cv2.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值