zoukankan      html  css  js  c++  java
  • python的numpy库和cv2库实现图像傅里叶变换

    码字不易,如果对您有所帮助,记着点赞哦!


    一. 图像傅里叶变换原理:

        原理简介请参考:https://www.cnblogs.com/wojianxin/p/12529809.html

        对二维图像进行傅里叶变换用如下公式进行:


    图像长M,高N。F(u,v)表示频域图像,f(x,y)表示时域图像。u的范围为[0,M-1],v的范围为[0,N-1]  ↑
     
     

        对二维图像进行傅里叶逆变换用如下公式进行:


    图像长M,高N。f(x,y)表示时域图像, F(u,v)表示频域图像。x的范围为[0,M-1],y的范围为[0,N-1]  ↑
     

    二. python的numpy库中的图像傅里叶变换公式:

    #计算一维傅里叶变换

    numpy.fft.fft(a, n=None, axis=-1, norm=None)

    #计算二维的傅里叶变换

    numpy.fft.fft2(a, n=None, axis=-1, norm=None)

    #计算n维的傅里叶变换

    numpy.fft.fftn()

    #计算n维实数的傅里叶变换

    numpy.fft.rfftn()

    #返回傅里叶变换的采样频率

    numpy.fft.fftfreq()

    #将FFT输出中的直流分量移动到频谱中央

    numpy.fft.shift()


    三. 实验:python的numpy库实现图像傅里叶变换及反变换

     1 # writer:wojianxinygcl@163.com
     2 
     3 # date  : 2020.3.30
     4 
     5 import cv2 as cv
     6 
     7 import numpy as np
     8 
     9 from matplotlib import pyplot as plt
    10 
    11 #读取图像
    12 
    13 img = cv.imread('../head_g.jpg', 0)
    14 
    15 #傅里叶变换
    16 
    17 f = np.fft.fft2(img)
    18 
    19 fshift = np.fft.fftshift(f)
    20 
    21 res = np.log(np.abs(fshift))
    22 
    23 #傅里叶逆变换
    24 
    25 ishift = np.fft.ifftshift(fshift)
    26 
    27 iimg = np.fft.ifft2(ishift)
    28 
    29 iimg = np.abs(iimg)
    30 
    31 #展示结果
    32 
    33 plt.subplot(131), plt.imshow(img, 'gray'), plt.title('Original Image')
    34 
    35 plt.axis('off')
    36 
    37 plt.subplot(132), plt.imshow(res, 'gray'), plt.title('Fourier Image')
    38 
    39 plt.axis('off')
    40 
    41 plt.subplot(133), plt.imshow(iimg, 'gray'), plt.title('Inverse Fourier Image')
    42 
    43 plt.axis('off')
    44 
    45 plt.show()

    四. 实验结果:


    第三部分代码输出结果 ↑
     

    五. python的cv2库中的图像傅里叶变换公式:

    # 傅里叶变换

    dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT)

    # 傅里叶逆变换

    iimg = cv2.idft(ishift)

    # 返回傅里叶变换后iimg的幅值

    res2 = cv2.magnitude(iimg[:,:,0], iimg[:,:,1])


    六. 实验:python的cv库和numpy库实现图像傅里叶变换及反变换

     1 # writer:wojianxinygcl@163.com
     2 
     3 # date  : 2020.3.30
     4 
     5 import numpy as np
     6 
     7 import cv2
     8 
     9 from matplotlib import pyplot as plt
    10 
    11 #读取图像
    12 
    13 img = cv2.imread('../paojie_g.jpg', 0)
    14 
    15 #傅里叶变换
    16 
    17 dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT)
    18 
    19 dftshift = np.fft.fftshift(dft)
    20 
    21 res1= 20*np.log(cv2.magnitude(dftshift[:,:,0], dftshift[:,:,1]))
    22 
    23 #傅里叶逆变换
    24 
    25 ishift = np.fft.ifftshift(dftshift)
    26 
    27 iimg = cv2.idft(ishift)
    28 
    29 res2 = cv2.magnitude(iimg[:,:,0], iimg[:,:,1])
    30 
    31 #显示图像
    32 
    33 plt.subplot(131), plt.imshow(img, 'gray'), plt.title('Original Image')
    34 
    35 plt.axis('off')
    36 
    37 plt.subplot(132), plt.imshow(res1, 'gray'), plt.title('Fourier Image')
    38 
    39 plt.axis('off')
    40 
    41 plt.subplot(133), plt.imshow(res2, 'gray'), plt.title('Inverse Fourier Image')
    42 
    43 plt.axis('off')
    44 
    45 plt.show()

    七. 实验结果:


    第六部分代码输出结果 ↑
     

    八. 参考内容:

    https://www.jianshu.com/p/a00da3e03533


    九. 版权声明:

            未经作者允许,请勿随意转载抄袭,抄袭情节严重者,作者将考虑追究其法律责任,创作不易,感谢您的理解和配合!

  • 相关阅读:
    计算机网络【七】:可靠传输的实现 (tcp窗口滑动以及拥塞控制)【转】
    计算机网络【六】:传输层-TCP概述 【转】
    计算机网络【五】:路由选择协议 【转】
    计算机网络【三】:数据链路层 【转】
    计算机网络【二】:物理层【转】
    计算机网络【一】:概述 【转】
    装饰模式-Decorator
    Java中的文件上传和下载
    模板方法模式-TemplateMethod
    策略模式-Strategy
  • 原文地址:https://www.cnblogs.com/wojianxin/p/12530172.html
Copyright © 2011-2022 走看看