标题: 频域滤波降噪和图像增强之Python [打印本页] 作者: lsekfe 时间: 2023-3-14 13:11 标题: 频域滤波降噪和图像增强之Python 图像处理已经成为我们日常生活中不可或缺的一部分,涉及到社交媒体和医学成像等各个领域。通过数码相机或卫星照片和医学扫描等其他来源获得的图像可能需要预处理以消除或增强噪声。频域滤波是一种可行的解决方案,它可以在增强图像锐化的同时消除噪声。
快速傅里叶变换(FFT)是一种将图像从空间域变换到频率域的数学技术,是图像处理中进行频率变换的关键工具。通过利用图像的频域表示,我们可以根据图像的频率内容有效地分析图像,从而简化滤波程序的应用以消除噪声。本文将讨论图像从FFT到逆FFT的频率变换所涉及的各个阶段,并结合FFT位移和逆FFT位移的使用。
本文使用了三个Python库,即openCV、Numpy和Matplotlib。
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('sample.png',0) # Using 0 to read image in grayscale mode
plt.imshow(img, cmap='gray') #cmap is used to specify imshow that the image is in greyscale
plt.xticks([]), plt.yticks([]) # remove tick marks
plt.show()
1、快速傅里叶变换(FFT)
快速傅里叶变换(FFT)是一种广泛应用的数学技术,它允许图像从空间域转换到频率域,是频率变换的基本组成部分。利用FFT分析,可以得到图像的周期性,并将其划分为不同的频率分量,生成图像频谱,显示每个图像各自频率成分的振幅和相位。
f = np.fft.fft2(img) #the image 'img' is passed to np.fft.fft2() to compute its 2D Discrete Fourier transform f
mag = 20*np.log(np.abs(f))
plt.imshow(mag, cmap = 'gray') #cmap='gray' parameter to indicate that the image should be displayed in grayscale.
plt.title('Magnitude Spectrum')
plt.xticks([]), plt.yticks([])
plt.show()
def idealFilterLP(D0,imgShape):
base = np.zeros(imgShape[:2])
rows, cols = imgShape[:2]
center = (rows/2,cols/2)
for x in range(cols):
for y in range(rows):
if distance((y,x),center) < D0:
base[y,x] = 1
return base
def idealFilterHP(D0,imgShape):
base = np.ones(imgShape[:2])
rows, cols = imgShape[:2]
center = (rows/2,cols/2)
for x in range(cols):
for y in range(rows):
if distance((y,x),center) < D0:
base[y,x] = 0
return base
下面函数生成一个高斯高、低通滤波器与所需的圆形掩膜
import math
def distance(point1,point2):
return math.sqrt((point1[0]-point2[0])**2 + (point1[1]-point2[1])**2)
def gaussianLP(D0,imgShape):
base = np.zeros(imgShape[:2])
rows, cols = imgShape[:2]
center = (rows/2,cols/2)
for x in range(cols):
for y in range(rows):
base[y,x] = math.exp(((-distance((y,x),center)**2)/(2*(D0**2))))
return base
def gaussianHP(D0,imgShape):
base = np.zeros(imgShape[:2])
rows, cols = imgShape[:2]
center = (rows/2,cols/2)
for x in range(cols):
for y in range(rows):
base[y,x] = 1 - math.exp(((-distance((y,x),center)**2)/(2*(D0**2))))
return base
过滤示例
fig, ax = plt.subplots(2, 2) # create a 2x2 grid of subplots
fig.suptitle('Filters') # set the title for the entire figure
# plot the first image in the top-left subplot
im1 = ax[0, 0].imshow(np.abs(idealFilterLP(50, img.shape)), cmap='gray')
ax[0, 0].set_title('Low Pass Filter of Diameter 50 px')
ax[0, 0].set_xticks([])
ax[0, 0].set_yticks([])
# plot the second image in the top-right subplot
im2 = ax[0, 1].imshow(np.abs(idealFilterHP(50, img.shape)), cmap='gray')
ax[0, 1].set_title('High Pass Filter of Diameter 50 px')
ax[0, 1].set_xticks([])
ax[0, 1].set_yticks([])
# plot the third image in the bottom-left subplot
im3 = ax[1, 0].imshow(np.abs(gaussianLP(50 ,img.shape)), cmap='gray')
ax[1, 0].set_title('Gaussian Filter of Diameter 50 px')
ax[1, 0].set_xticks([])
ax[1, 0].set_yticks([])
# plot the fourth image in the bottom-right subplot
im4 = ax[1, 1].imshow(np.abs(gaussianHP(50 ,img.shape)), cmap='gray')
ax[1, 1].set_title('Gaussian Filter of Diameter 50 px')
ax[1, 1].set_xticks([])
ax[1, 1].set_yticks([])
# adjust the spacing between subplots
fig.subplots_adjust(wspace=0.5, hspace=0.5)
# save the figure to a file
fig.savefig('filters.png', bbox_inches='tight')
# Shift the zero-frequency component to the center of the frequency spectrum
centered = np.fft.fftshift(img_in_freq_domain)
# Multiply the filter with the centered spectrum
filtered_image_in_freq_domain = centered * filter_used
# Shift the zero-frequency component back to the top-left corner of the frequency spectrum
inverse_fftshift_on_filtered_image = np.fft.ifftshift(filtered_image_in_freq_domain)
# Apply the inverse Fourier transform to obtain the final filtered image
final_filtered_image = np.fft.ifft2(inverse_fftshift_on_filtered_image)
filters = [(f, d) for f in [idealFilterLP, idealFilterHP, gaussianLP, gaussianHP] for d in [50, 100, 150]]
for row, (filter_name, filter_diameter) in enumerate(filters):
# Plot each filter output on a separate subplot
result = Freq_Trans(img, filter_name(filter_diameter, img.shape))
for col, title, img_array in zip(range(7),
["Original Image", "Spectrum", "Centered Spectrum", f"{filter_name.__name__} of Diameter {filter_diameter} px",
f"Centered Spectrum multiplied by {filter_name.__name__}", "Decentralize", "Processed Image"],
[img, np.log(1+np.abs(result[0])), np.log(1+np.abs(result[1])), np.abs(result[2]), np.log(1+np.abs(result[3])),
np.log(1+np.abs(result[4])), np.abs(result[5])]):