Scipy简介
- Scipy依赖于Numpy
- Scipy包含的功能:最优化、线性代数、积分、插值、拟合、特殊函数、快速傅里叶变换、信号处理、图像处理、常微分方程求解器等
- 应用场景:Scipy是高端科学计算工具包,用于数学、科学、工程学等领域
- Scipy由一些特定功能的子模块组成:
图片消噪处理
- scipy.fftpack模块用来计算快速傅里叶变换
速度比传统傅里叶变换更快,是对之前算法的改进
图片是二维数据,注意使用fftpack的二维转变方法
from scipy.fftpack import fft2, ifft2
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
#读取照片数据
moon = plt.imread('moonlanding.png') ==>
array([[ 0.04705882, 0. , 0.23921569, ..., 0. ,
0.00392157, 0.53333336],
[ 0. , 0. , 0.67843139, ..., 0.10196079,
0.29019609, 0. ],
[ 0.72156864, 0.10980392, 0.60392159, ..., 0. ,
0.21568628, 1. ],
...,
[ 0.00392157, 0. , 1. , ..., 1. ,
1. , 0.95686275],
[ 0. , 0. , 0.15686275, ..., 0. ,
0. , 0.35294119],
[ 1. , 0.52156866, 0.04705882, ..., 0. ,
0. , 1. ]], dtype=float32
#数据形式
moon.shape ==> (474, 630)
#展示图片
plt.imshow(moon, cmap='gray')
#cmap 指定RGB的Z值,如果是三维数组则忽略cmap值
# 可选择的颜色映射参数为: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, Vega10, Vega10_r, Vega20, Vega20_r, Vega20b, Vega20b_r, Vega20c, Vega20c_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, 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, spectral, spectral_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
# 傅里叶变换消噪
# 把时域空间转换到频域空间
f_moon = fft2(moon)
#关键步骤,过滤条件必须写好
# 在频域空间对高幅度的波进行过滤
f_moon[f_moon>3e2]