图片处理——傅里叶消噪声[scipy.fftpack]

登月图片消噪

  1. scipy.fftpack模块用来计算快速傅里叶变换;
  2. 速度比传统傅里叶变换更快,是对之前算法的改进;
  3. 图片是二维数据,注意使用fftpack的二维转变方法。
  • # 所有的函数多可以使用正弦波表示,所有函数都可以表示成多个正弦波叠加(多项式)
  • # 傅里叶变换复杂的数据转换成单一的数据
  • # 时域 -------> 频域

导包

import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np

import scipy.fftpack as fftpact

举栗子:无波动的数据即全黑数据为数字0

img = np.zeros(shape = (300,400,3),dtype = np.uint8)
img

# 数据没有波动
plt.imshow(img)

导入并显示噪声图片

moon = plt.imread('./moonlanding.png')
moon
Out:
array([[0.04705882, 0.        , 0.23921569, ..., 0.        , 0.00392157,
        0.53333336],
       [0.        , 0.        , 0.6784314 , ..., 0.10196079, 0.2901961 ,
        0.        ],
       [0.72156864, 0.10980392, 0.6039216 , ..., 0.        , 0.21568628,
        1.        ],
       ...,

moon.shape
Out:(474, 630)
 
plt.figure(figsize=(12,9))
''' Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap,
CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, 
PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, inferno, inferno_r, jet, jet_r, magma, magma_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r,
tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, viridis, viridis_r, winter, winter_r'''
plt.imshow(moon,cmap = 'gray')

  • # 图片中噪声点:白色的圆环
  • # 圆环上的数据和圆环里面和外面不同,所以肉眼显示出来
  • # 波动
  • # 噪声的地方,波动比较大

傅里叶变换数据-------> 频域(数据波动情况)——>将波动比较大的数据过滤掉,噪声过滤

  • 1、将时域(真实数据moon)----->频域

# 1、将时域(真实数据moon)----->频域
moon_fft = fftpact.fft2(moon)
# 实数 + 虚数
# x** = 4 --->正负二
# x** = -4 ---->虚数
moon_fft

Out:
array([[126598.45      +0.j       ,  -4608.5796 -1892.4688j   ,
          -322.093    -20.27744j  , ...,   -906.1585 +1539.3081j   ,
          -322.093    +20.27744j  ,  -4608.5796 +1892.4688j   ],
       [ -9421.1    +5242.1133j   ,   5224.016  -3171.7434j   ,
          1607.9927 +1269.4243j   , ...,   -677.34503 -936.16174j  ,
           354.6247 -1003.8348j   ,   1965.366  -2188.0593j   ],
       [ -2928.3513 +7280.916j    ,  -1116.4065 +1338.3179j   ,
          -474.20056 +385.40216j  , ...,    239.7723  -977.2129j   ,
          1582.9283  -261.95346j  ,   2641.927   -292.09366j  ],
       ...,

moon_fft.shape
Out:  (474, 630)
  • 2、异常值判断,并去除
np.abs(moon_fft).mean()
Out: 51.193375

# 2、将波动比较大的数据过滤掉,设置为0 【此处规定异常值为大于10倍均值的数值】
# 临界值500 大于500 波动情况比较大,过滤掉

cond = np.abs(moon_fft) > 500

moon_fft[cond] = 0
moon_fft
Out:
array([[   0.      +0.j       ,    0.      +0.j       ,
        -322.093  -20.27744j  , ...,    0.      +0.j       ,
        -322.093  +20.27744j  ,    0.      +0.j       ],
       [   0.      +0.j       ,    0.      +0.j       ,
           0.      +0.j       , ...,    0.      +0.j       ,
           0.      +0.j       ,    0.      +0.j       ],
       [   0.      +0.j       ,    0.      +0.j       ,
           0.      +0.j       , ...,    0.      +0.j       ,
           0.      +0.j       ,    0.      +0.j       ],
       ...,
  • 3、频域------>时域(数据)
moon_result = fftpact.ifft2(moon_fft)
moon_result

moon_result = np.real(moon_result)
moon_result

Out:
array([[-0.2158841 ,  0.08547963, -0.17341562, ...,  0.00313992,
        -0.1262884 , -0.12006474],
       [-0.07464715,  0.02630262, -0.05795981, ..., -0.10645279,
        -0.10607974, -0.06213436],
       [ 0.01300102, -0.04392404, -0.03069701, ..., -0.09355526,
        -0.09281505,  0.0542773 ],
       ...,
  • 4、显示去噪声后的图片
plt.figure(figsize=(12,9))
plt.imshow(moon_result,cmap = 'gray')

猜你喜欢

转载自blog.csdn.net/Dorisi_H_n_q/article/details/82460476
今日推荐