Strava

scipy的傅立叶变换简单使用

 
from  scipy.fftpack import fft2,ifft2 #fft_处理一维数据 ,fft2—-二维,ifft2_inverse
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
 
 
In [ ]:
 
 
 
 
 
moon=plt.imread('./moon.png')
plt.figure(figsize=(12,9))
plt.imshow(moon,cmap=plt.cm.gray)  #颜色gray-灰色
 
<matplotlib.image.AxesImage at 0x117087ef0>
 
 
In [ ]:
 
 
 
 
 
moon.shape
 
 
(555, 738, 4)
In [ ]:
 
 
 
 
 
moon
 
array([[[0.02745098, 0.02745098, 0.02745098, 1.        ],
        [0.01176471, 0.01176471, 0.01176471, 1.        ],
        [0.1882353 , 0.1882353 , 0.1882353 , 1.        ],
        ...,
        [0.15686275, 0.15686275, 0.15686275, 1.        ],
        [0.02352941, 0.02352941, 0.02352941, 1.        ],
        [0.24313726, 0.24313726, 0.24313726, 1.        ]],

       [[0.03137255, 0.03137255, 0.03137255, 1.        ],
        [0.02352941, 0.02352941, 0.02352941, 1.        ],
        [0.3764706 , 0.3764706 , 0.3764706 , 1.        ],
        ...,
 
In [ ]:
 
 
 
 
 
moon_fft=fft2(moon)#时域变频域
moon_fft
 
array([[[ 1.19326282e+03-0.0000000e+00j, -3.92156839e-03+5.8623914e+02j,
         -5.86258789e+02-0.0000000e+00j, -3.92156839e-03-5.8623914e+02j],
        [ 7.63338327e-01+1.8425717e+00j,  6.17021441e-01-2.4927109e-01j,
          2.61740685e-01+6.1575228e-01j, -6.09798074e-01+2.5232649e-01j],
        [-5.07767200e-01+8.8224435e-01j,  2.91649103e-01+1.6837674e-01j,
         -1.65383577e-01+2.9348469e-01j, -2.97111928e-01-1.7400441e-01j],
        ...,
 
In [ ]:
 
 
 
 
 
#计算所有数据波动频率平均值
np.abs(moon_fft).mean()
 
2.5597527
 
In [ ]:
 
 
 
 
 
#定义一个mean的10倍数波动值,大于这个值则过滤掉,abs(绝对值),因为波动是上下的
cond=np.abs(moon_fft)>20
cond
 
array([[[ True,  True,  True,  True],
        [False, False, False, False],
        [False, False, False, False],
        ...,
 

In [ ]:
 
 
 
 
 
#过滤重新赋值0,完成波动消除
moon_fft[cond]=0
 
 
 

消除完成,频域变时域,显示图片,查看看效果

In [ ]:
 
 
 
 
 
moon_result=ifft2(moon_fft)
moon_result
 
 
In [ ]:
 
 
 
 
 
moon1=np.real(moon_result)#去掉虚数
moon1.shape
moon2=moon1+0.2
 
 
In [ ]:
 
 
 
 
 
plt.figure(figsize=(12,9))
plt.imshow(moon2,cmap=plt.cm.gray)
 
<matplotlib.image.AxesImage at 0x105042748>
 
 
In [ ]:
 
 
 
 
 
moon1
#Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers)
   这句提示很重要 
 
In [ ]:
 
 
 
 
 
moon3=np.abs(moon1+0.2)
moon3
 
array([[[0.04771344, 0.04774603, 0.04772024, 0.27566767],
        [0.1647619 , 0.16474824, 0.16474599, 0.22775729],
        [0.29129887, 0.29129982, 0.2912937 , 0.1467748 ],
        ...,
        [0.27189398, 0.2718845 , 0.27187863, 0.15516104],
        [0.14559661, 0.14559676, 0.14559412, 0.20154473],
        [0.10776128, 0.10774096, 0.10776637, 0.21963957]],

       [[0.10564578, 0.10563988, 0.1056399 , 0.20192218],
        [0.21756186, 0.21753544, 0.21753542, 0.15374364],
        [0.38377625, 0.38375378, 0.38375378, 0.17153105],
        ...,
        [0.1592251 , 0.15919563, 0.15919557, 0.21983159],
        [0.18120806, 0.18119566, 0.18119562, 0.20554906],
        [0.22343929, 0.22344433, 0.22344434, 0.2761    ]],

       [[0.09483282, 0.09481055, 0.09482741, 0.18048806],
        [0.11367074, 0.11363061, 0.11365368, 0.21783048],
        [0.3184189 , 0.31838167, 0.3184024 , 0.22067577],
        ...,
        [0.27743402, 0.27739507, 0.27741295, 0.22029907],
        [0.09207404, 0.09203144, 0.09205086, 0.21834889],
        [0.05967532, 0.05965175, 0.05966662, 0.18094054]],

       ...,
 
In [ ]:
 
 
 
 
 
plt.figure(figsize=(12,9))
plt.imshow(moon3+0.245,cmap=plt.cm.gray)
 
<matplotlib.image.AxesImage at 0x111c1ce10>
 
 
 

难受没拿到原图(截图),虽然数据波动明显平缓了不少,however,还是显著存在

posted @ 2020-06-06 20:03  cheflone  阅读(267)  评论(0编辑  收藏  举报