图像滤波算法 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()
相关推荐
做怪小疯子19 小时前
华为笔试0429
python·numpy
Warson_L19 小时前
Dictionary
python
王老师青少年编程20 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【哈夫曼贪心】:合并果子
c++·算法·贪心·csp·信奥赛·哈夫曼贪心·合并果子
叼烟扛炮21 小时前
C++第二讲:类和对象(上)
数据结构·c++·算法·类和对象·struct·实例化
天疆说21 小时前
【哈密顿力学】深入解读航天器交会最优控制中的Hamilton函数
人工智能·算法·机器学习
寒山李白21 小时前
解决 python-docx 生成的 Word 文档打开时弹出“无法读取内容“警告
python·word·wps·文档·docx·qoder
wuweijianlove21 小时前
关于算法设计中的代价函数优化与约束求解的技术7
算法
leoufung1 天前
LeetCode 149: Max Points on a Line - 解题思路详解
算法·leetcode·职场和发展
样例过了就是过了1 天前
LeetCode热题100 最长公共子序列
c++·算法·leetcode·动态规划
2401_832365521 天前
JavaScript中rest参数(...args)取代arguments的优势
jvm·数据库·python