图像滤波算法 python

1. 平均滤波 (Mean Filtering)

平均滤波是一种简单的线性滤波方法,通过取邻域内像素的平均值来平滑图像,从而去除噪声。

python 复制代码
import cv2
import numpy as np

# 读取图像
image = cv2.imread('image.jpg')

# 应用平均滤波
mean_filtered = cv2.blur(image, (5, 5))

# 显示结果
cv2.imshow('Mean Filter', mean_filtered)
cv2.waitKey(0)
cv2.destroyAllWindows()

2. 高斯滤波 (Gaussian Filtering)

高斯滤波通过使用高斯函数进行加权平均,能更有效地去除噪声,保留图像边缘。

python 复制代码
# 应用高斯滤波
gaussian_filtered = cv2.GaussianBlur(image, (5, 5), 0)

# 显示结果
cv2.imshow('Gaussian Filter', gaussian_filtered)
cv2.waitKey(0)
cv2.destroyAllWindows()

3. 中值滤波 (Median Filtering)

中值滤波是一种非线性滤波方法,使用邻域内像素的中值来替代中心像素值,特别适合去除椒盐噪声。

python 复制代码
# 应用中值滤波
median_filtered = cv2.medianBlur(image, 5)

# 显示结果
cv2.imshow('Median Filter', median_filtered)
cv2.waitKey(0)
cv2.destroyAllWindows()

4. 双边滤波 (Bilateral Filtering)

双边滤波在去噪的同时,能很好地保留图像的边缘信息。

python 复制代码
# 应用双边滤波
bilateral_filtered = cv2.bilateralFilter(image, 9, 75, 75)

# 显示结果
cv2.imshow('Bilateral Filter', bilateral_filtered)
cv2.waitKey(0)
cv2.destroyAllWindows()

5. 非局部均值滤波 (Non-Local Means Filtering)

非局部均值滤波通过比较图像中各个块的相似性来进行去噪,效果较好但计算量较大。

python 复制代码
# 应用非局部均值滤波
nlm_filtered = cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21)

# 显示结果
cv2.imshow('NLM Filter', nlm_filtered)
cv2.waitKey(0)
cv2.destroyAllWindows()

6. 小波去噪 (Wavelet Denoising)

小波去噪通过小波变换将图像分解为不同频率分量,并在小波域中进行阈值处理来去噪。

python 复制代码
import pywt
import pywt.data

# 使用示例图像
image = pywt.data.camera()

# 小波分解
coeffs2 = pywt.dwt2(image, 'bior1.3')
LL, (LH, HL, HH) = coeffs2

# 阈值处理
threshold = 0.04
LH = pywt.threshold(LH, threshold*max(LH))
HL = pywt.threshold(HL, threshold*max(HL))
HH = pywt.threshold(HH, threshold*max(HH))

# 小波重构
denoised_image = pywt.idwt2((LL, (LH, HL, HH)), 'bior1.3')

# 显示结果
plt.imshow(denoised_image, cmap='gray')
plt.title('Wavelet Denoising')
plt.show()
相关推荐
云和数据.ChenGuang23 分钟前
pycharm怎么将背景换成白色
ide·python·pycharm
10岁的博客28 分钟前
二维差分算法高效解靶场问题
java·服务器·算法
轻微的风格艾丝凡29 分钟前
锂电池 SOC 估计技术综述:成熟算法、新颖突破与车企应用实践
算法·汽车
Codeking__30 分钟前
动态规划算法经典问题——01背包问题
算法·动态规划
R-G-B31 分钟前
归并排序 (BM20 数组中的逆序对)
数据结构·算法·排序算法
少许极端33 分钟前
算法奇妙屋(十二)-优先级队列(堆)
数据结构·算法·leetcode·优先级队列··图解算法
我的xiaodoujiao1 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 25--数据驱动--参数化处理 Excel 文件 2
前端·python·学习·测试工具·ui·pytest
DO_Community1 小时前
基于AI Agent模板:快速生成 SQL 测试数据
人工智能·python·sql·ai·llm·ai编程
kupeThinkPoem1 小时前
哈希表有哪些算法?
数据结构·算法
小白程序员成长日记2 小时前
2025.11.16 力扣每日一题
算法