我不知道该怎么评价了,我也不知道我傅里叶变换,然后去掉高频点之后到底去掉的是什么,我不知道我成功还是失败了,不过,这个做成动画还蛮有意思的哦哈哈哈~
需要安装的模块好像还挺多的,反正就是numpy系列的吧?好多模块我都不知道自己什么时候安装的。。。缺哪个安哪个吧。。
代码:
from skimage import data, io
import numpy as np
from scipy.fftpack import fft,ifft
import matplotlib.pyplot as plt
from matplotlib.pylab import mpl
import matplotlib.animation as ani
mpl.rcParams['font.sans-serif'] = ['SimHei'] #显示中文
mpl.rcParams['axes.unicode_minus']=False #显示负号
# 动画更新方法
def update(i):
# img1 = 255 - img1 # 反色开关
max_t = max1 * m[i]
fft_img1[ fft_img1 > max_t ] = 0
fft_img1[ fft_img1 < -max_t ] = 0
ifft_img1 = ifft(fft_img1)
img1 = np.short(ifft_img1.T)
# img1 = 255 - img1 # 反色开关
img1.shape = img_shape
plt.title(tle[i])
return plt.imshow(img1, animated=True),
# 获取猫猫图片
img = data.chelsea()
# 保存图片形状
img_shape = img.shape
# 一维化三个通道
img.shape = -1, 3
# 转置方便变换
img = img.T
fft_img1 = fft(img)
# 逆变换
ifft_img1 = ifft(fft_img1)
# 再转置回去,顺便把转回的数字转成0-256的整型
img1 = np.short(ifft_img1.T)
# 把三个通道再次二维化为原来的形状
img1.shape = img_shape
max1 = np.abs(fft_img1).max()
m = [1, 0.9, 0.65, 0.3, 0.1, 0.05, 0.005, 0.001, 0.0005, 0.0001]
tle = ['原图','90%','65%','30%','10%','5%','0.5%','0.1%', '0.05%', '0.01%']
fig,ax = plt.subplots()
ani1 = ani.FuncAnimation(fig, update, np.arange(0, 10), interval=200, blit=True,repeat=False)
# ani1.save('猫猫褪色.gif', writer='imagemagick') # 保存动图
plt.show()
'''
max_t = max1 * 0.003
fft_img1[ fft_img1 >= max_t ] = 0
fft_img1[ fft_img1 <= -max_t ] = 0
ifft_img1 = ifft(fft_img1)
img1 = np.short(ifft_img1.T)
img1 = 255 - img1
img1.shape = img_shape
plt.title("五光十色的白")
plt.imshow(img1)
plt.show()
'''
''' 下面是从低频开始去除,就是会变得越来越模糊
fft_img1 = fft(img)
ifft_img1 = ifft(fft_img1)
# 再转置回去,顺便把转回的数字转成0-256的整型
img1 = np.short(ifft_img1.T)
# 把三个通道再次二维化为原来的形状
img1.shape = img_shape
m = [0.001, 0.004, 0.008, 0.024, 0.05, 0.1, 0.3, 0.6]
tle = ['原图', '0.1%','0.4%','0.8%','2.4%','5%','10%','30%','60%']
for i in range(7):
plt.subplot(2,4,i + 1)
plt.title(tle[i])
plt.imshow(img1)
max_t = max1 * m[i]
fft_img1[ (fft_img1 < max_t) & (fft_img1 > -max_t) ] = 0
ifft_img1 = ifft(fft_img1)
img1 = np.short(ifft_img1.T)
img1.shape = img_shape
plt.subplot(2,4,8)
plt.title(tle[7])
plt.imshow(img1)
plt.show()
'''