广告位
468x60
正文内嵌广告1
¥500/月
课程简介
课程简介
本课程深入讲解图像滤波与增强技术,涵盖线性滤波、非线性滤波和频域滤波三大核心方法。通过理论解析与代码实战结合,帮助学员理解滤波原理,掌握图像去噪、锐化等增强技术。课程包含对比实验和完整项目案例,适合计算机视觉领域开发者提升图像处理能力。
前置依赖课程
课程大纲
第1章第一章 图像滤波基础
- 1.1 滤波原理与分类45min
- 1.2 线性滤波方法60min
- 1.3 非线性滤波技术60min
第2章第二章 频域滤波方法
- 2.1 傅里叶变换基础60min
- 2.2 频域滤波原理60min
- 2.3 频域滤波实现90min
第3章第三章 图像增强实战
- 3.1 图像锐化技术60min
- 3.2 图像去噪方法60min
- 3.3 实战项目:图像质量提升120min
广告位
336x280
正文内嵌广告2
¥700/月
课程资料
图像滤波与增强技术详解
第一章 图像滤波基础
1.1 滤波原理与分类
图像滤波(Image Filtering)是通过卷积运算修改像素值的过程,核心目标是抑制噪声或增强特定特征。滤波器(Kernel)作为核心工具,其数学表达式为:
$$ g(x,y) = \sum_{s=-a}^{a}\sum_{t=-b}^{b} w(s,t) \cdot f(x+s,y+t) $$其中 \(f(x,y)\) 为输入图像,\(w(s,t)\) 为滤波器权重,\(g(x,y)\) 为输出图像。
滤波器分类:
- 线性滤波:输出是输入的线性组合(如均值滤波、高斯滤波)
- 非线性滤波:输出与输入非线性相关(如中值滤波、双边滤波)
- 频域滤波:在傅里叶变换后的频域操作(如低通滤波、高通滤波)
1.2 线性滤波方法
线性滤波通过卷积核实现,常见类型包括:
均值滤波:用邻域均值替换中心像素
import cv2 import numpy as np def mean_filter(image, kernel_size=3): kernel = np.ones((kernel_size, kernel_size), np.float32) / (kernel_size**2) return cv2.filter2D(image, -1, kernel) # 示例 img = cv2.imread('noisy.jpg', 0) filtered = mean_filter(img, 5)高斯滤波:权重按高斯分布分配
def gaussian_filter(image, kernel_size=3, sigma=1.0): kernel = cv2.getGaussianKernel(kernel_size, sigma) kernel_2d = kernel * kernel.T return cv2.filter2D(image, -1, kernel_2d)
对比实验:
| 滤波方法 | 噪声抑制 | 边缘保留 | 计算复杂度 |
|---|---|---|---|
| 均值滤波 | ★★☆ | ★☆☆ | 低 |
| 高斯滤波 | ★★★ | ★★☆ | 中 |
1.3 非线性滤波技术
非线性滤波通过非线性运算处理像素:
中值滤波:取邻域中值
def median_filter(image, kernel_size=3): return cv2.medianBlur(image, kernel_size)双边滤波:结合空间距离和像素相似度
def bilateral_filter(image, d=9, sigma_color=75, sigma_space=75): return cv2.bilateralFilter(image, d, sigma_color, sigma_space)
效果对比:
import matplotlib.pyplot as plt
noisy_img = cv2.imread('salt_pepper.jpg', 0)
median_result = median_filter(noisy_img)
bilateral_result = bilateral_filter(noisy_img)
plt.figure(figsize=(12,4))
plt.subplot(131), plt.imshow(noisy_img, 'gray'), plt.title('Noisy Image')
plt.subplot(132), plt.imshow(median_result, 'gray'), plt.title('Median Filter')
plt.subplot(133), plt.imshow(bilateral_result, 'gray'), plt.title('Bilateral Filter')
plt.show()
本节小结
- 线性滤波适合高斯噪声,但会模糊边缘
- 非线性滤波能有效处理脉冲噪声,保留更多细节
- 双边滤波计算量较大,适合高质量要求场景
第二章 频域滤波方法
2.1 傅里叶变换基础
傅里叶变换(Fourier Transform)将图像从空间域转换到频域:
$$ F(u,v) = \sum_{x=0}^{M-1}\sum_{y=0}^{N-1} f(x,y) \cdot e^{-j2\pi(\frac{ux}{M}+\frac{vy}{N})} $$Python实现:
def fft_transform(image):
f = np.fft.fft2(image)
fshift = np.fft.fftshift(f) # 中心化
magnitude_spectrum = 20*np.log(np.abs(fshift))
return magnitude_spectrum
2.2 频域滤波原理
频域滤波通过修改频谱实现:
- 低通滤波:保留低频(平滑)
- 高通滤波:保留高频(锐化)
- 带通滤波:保留特定频段
理想低通滤波器:
$$ H(u,v) = \begin{cases} 1 & \text{if } D(u,v) \leq D_0 \\ 0 & \text{otherwise} \end{cases} $$2.3 频域滤波实现
完整流程代码:
def frequency_filter(image, filter_type='lowpass', D0=30):
# 傅里叶变换
f = np.fft.fft2(image)
fshift = np.fft.fftshift(f)
# 创建滤波器
rows, cols = image.shape
crow, ccol = rows//2, cols//2
x, y = np.ogrid[:rows, :cols]
mask = np.sqrt((x-crow)**2 + (y-ccol)**2)
if filter_type == 'lowpass':
mask = mask <= D0
elif filter_type == 'highpass':
mask = mask > D0
# 应用滤波器
fshift_filtered = fshift * mask
# 逆变换
f_ishift = np.fft.ifftshift(fshift_filtered)
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)
return img_back
效果对比:
img = cv2.imread('texture.jpg', 0)
lowpass = frequency_filter(img, 'lowpass', 50)
highpass = frequency_filter(img, 'highpass', 30)
plt.figure(figsize=(12,4))
plt.subplot(131), plt.imshow(img, 'gray'), plt.title('Original')
plt.subplot(132), plt.imshow(lowpass, 'gray'), plt.title('Lowpass Filter')
plt.subplot(133), plt.imshow(highpass, 'gray'), plt.title('Highpass Filter')
plt.show()
本节小结
- 频域滤波适合周期性噪声处理
- 理想滤波器会产生振铃效应,实际应用中常用高斯滤波器
- 频域处理计算量较大,适合离线处理场景
第三章 图像增强实战
3.1 图像锐化技术
锐化通过增强高频分量实现,常用方法:
拉普拉斯算子:
def laplacian_sharpen(image, kernel_size=3): kernel = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]], np.float32) return cv2.filter2D(image, -1, kernel)非锐化掩模(Unsharp Masking):
def unsharp_mask(image, k=0.5): blurred = cv2.GaussianBlur(image, (0,0), 3) detail = image - blurred return image + k*detail
效果对比:
img = cv2.imread('blurry.jpg', 0)
laplacian = laplacian_sharpen(img)
unsharp = unsharp_mask(img)
plt.figure(figsize=(12,4))
plt.subplot(131), plt.imshow(img, 'gray'), plt.title('Original')
plt.subplot(132), plt.imshow(laplacian, 'gray'), plt.title('Laplacian')
plt.subplot(133), plt.imshow(unsharp, 'gray'), plt.title('Unsharp Mask')
plt.show()
3.2 图像去噪方法
综合去噪方案:
def denoise_image(image):
# 先进行双边滤波
bilateral = cv2.bilateralFilter(image, 9, 75, 75)
# 再进行非局部均值去噪
denoised = cv2.fastNlMeansDenoising(bilateral, None, 10, 7, 21)
return denoised
3.3 实战项目:图像质量提升
完整项目流程:
- 读取图像并分析噪声类型
- 选择合适滤波方法组合
- 评估处理效果(PSNR/SSIM)
from skimage.metrics import peak_signal_noise_ratio, structural_similarity
def evaluate_image(original, processed):
psnr = peak_signal_noise_ratio(original, processed)
ssim = structural_similarity(original, processed)
print(f"PSNR: {psnr:.2f} dB")
print(f"SSIM: {ssim:.4f}")
# 完整处理流程
def complete_pipeline(image_path):
# 读取图像
original = cv2.imread(image_path, 0)
# 添加噪声(模拟)
noise = np.random.normal(0, 25, original.shape)
noisy = np.clip(original + noise, 0, 255).astype(np.uint8)
# 处理流程
denoised = denoise_image(noisy)
sharpened = unsharp_mask(denoised)
# 评估
evaluate_image(original, sharpened)
# 显示结果
plt.figure(figsize=(12,4))
plt.subplot(131), plt.imshow(original, 'gray'), plt.title('Original')
plt.subplot(132), plt.imshow(noisy, 'gray'), plt.title('Noisy')
plt.subplot(133), plt.imshow(sharpened, 'gray'), plt.title('Processed')
plt.show()
complete_pipeline('test_image.jpg')
本节小结
- 图像增强需要平衡去噪与细节保留
- 不同场景需要组合多种滤波方法
- 客观指标(PSNR/SSIM)应与主观评价结合
- 实战中需根据图像特点调整参数
课程总结 本课程系统讲解了图像滤波与增强技术,从基础原理到实战应用全面覆盖。学员应掌握:
- 线性/非线性滤波的特点与实现
- 频域滤波的工作原理
- 图像锐化与去噪的组合方案
- 完整图像增强项目的开发流程
后续可深入学习图像分割、目标检测等高级计算机视觉任务。
广告位
300x250
正文内嵌广告3
¥600/月