本节需要的图像数据放在评论区.
import numpy as np
from PIL import Image
import scipy.fft as fp
from scipy import fftpack
import matplotlib.pylab as plt
非卷积操作的高通滤波是通过简单粗暴地将频域图像的低频部分去掉,而只保留高频部分的矩阵操作,称为非卷积操作的高通滤波.
im = np.array(Image.open('images/lena.jpg').convert('L')) #
freq = fp.fft2(im)
(w, h) = freq.shape
half_w, half_h = int(w/2), int(h/2) # 确定频谱图像的中心位置
# 高通滤波器
freq1 = np.copy(freq)
freq2 = fftpack.fftshift(freq1) # 将低频部分移至中心位置
freq2[half_w-20:half_w+21,half_h-20:half_h+21] = 0 # select all but the first 20x20 (low) frequencies
im1 = np.clip(fp.ifft2(fftpack.ifftshift(freq2)).real,0,255) # 将两端的像素值缩至端点
plt.imshow(im, cmap='gray')
plt.axis('off')
plt.show()
plt.imshow((20*np.log10( 0.1 + freq2)).astype(int),cmap='gray')
plt.axis('off')
plt.show()
plt.imshow(im1, cmap='gray')
plt.axis('off')
plt.show()
非卷积操作的低通滤波是通过简单粗暴地将频域图像的高频部分去掉,而只保留低频部分的矩阵操作,称为非卷积操作的低通滤波.
im = np.array(Image.open('images/lena.jpg').convert('L')) #
freq = fp.fft2(im)
(w, h) = freq.shape
half_w, half_h = int(w/2), int(h/2) # 确定频谱图像的中心位置
# 低通滤波
freq1 = np.copy(freq)
freq2 = fftpack.fftshift(freq1)
freq2_low = np.copy(freq2)
freq2_low[half_w-40:half_w+41,half_h-40:half_h+41] = 0
freq2 -= freq2_low # select only the first 20x20 (low) frequencies
im1 = np.clip(fp.ifft2(fftpack.ifftshift(freq2)).real,0,255)
plt.imshow(im, cmap='gray')
plt.axis('off')
plt.show()
plt.figure(figsize=(10,10))
plt.imshow( (20*np.log10( 0.1 + freq2)).astype(int),cmap='gray')
plt.show()
plt.imshow(im1, cmap='gray')
plt.axis('off')
plt.show()
带通滤波(band-pass filtering)是一个允许特定频段的波通过同时屏蔽其他频段的操作。
im = np.array(Image.open('images/lena.jpg').convert('L')) #rhino
freq = fp.fft2(im)
(w, h) = freq.shape
half_w, half_h = int(w/2), int(h/2)
# band pass filter
freq1 = np.copy(freq)
freq2 = fftpack.fftshift(freq1)
freq2[:half_w+10,:half_h+10] = freq2[half_w+30:,half_h+30:] = 0 # select only the middle 20x20 (low) frequencies
im1 = fp.ifft2(fftpack.ifftshift(freq2)).real
plt.imshow(im, cmap='gray')
plt.axis('off')
plt.show()
plt.figure(figsize=(10,10))
plt.imshow( (20*np.log10( 0.1 + freq2)).astype(int))
plt.show()
plt.imshow(im1, cmap='gray')
plt.axis('off')
plt.show()