图像滤波算法 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()
相关推荐
R-G-B2 分钟前
OpenCV 实战篇——如何测算出任一副图片中的物体的实际尺寸?传感器尺寸与像元尺寸的关系?
人工智能·opencv·工业相机·传感器尺寸·像元·测算图片中的物体尺寸·像元与物体尺寸
正在走向自律3 分钟前
Ubuntu系统下Python连接国产KingbaseES数据库实现增删改查
开发语言·数据库·python·ubuntu·kingbasees·ksycopg2
CoovallyAIHub14 分钟前
推理提速一倍!SegDT:轻量化扩散 Transformer,医学图像分割的技术跨越
深度学习·算法·计算机视觉
CoovallyAIHub23 分钟前
无人机方案如何让桥梁监测更安全、更智能?融合RTK与超高分辨率成像,优于毫米精度
深度学习·算法·计算机视觉
lingran__26 分钟前
C语言制作扫雷游戏(拓展版赋源码)
c语言·算法·游戏
self_myth29 分钟前
【考研/面试必备】操作系统核心原理与IPO机制深度解析
大数据·算法
Calihen的学习日志1 小时前
【Pandas】3.1-数据预处理:列的基本操作
python·pandas
打螺丝否1 小时前
稠密矩阵和稀疏矩阵的对比
python·机器学习·矩阵
这里有鱼汤1 小时前
你以为 FastAPI 足够强?其实 Litestar 能让你的项目更轻量高效
后端·python
Greedy Alg1 小时前
LeetCode 240. 搜索二维矩阵 II
算法·leetcode·职场和发展