图像滤波算法 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()
相关推荐
艾上编程15 小时前
第三章——爬虫工具场景之Python爬虫实战:学术文献摘要爬取,助力科研高效进行
开发语言·爬虫·python
d111111111d15 小时前
在STM32函数指针是什么,怎么使用还有典型应用场景。
笔记·stm32·单片机·嵌入式硬件·学习·算法
Hi_kenyon15 小时前
FastAPI+VUE3创建一个项目的步骤模板(二)
python·fastapi
拉普拉斯妖10816 小时前
DAY38 Dataset和DataLoader
python
AI科技星16 小时前
质量定义方程常数k = 4π m_p的来源、推导与意义
服务器·数据结构·人工智能·科技·算法·机器学习·生活
摇摆的含羞草16 小时前
哈希(hash)算法使用特点及常见疑问解答
算法·哈希算法
Michelle802316 小时前
24大数据 16-1 函数复习
python
dagouaofei16 小时前
AI自动生成PPT工具对比分析,效率差距明显
人工智能·python·powerpoint
ku_code_ku17 小时前
python bert_score使用本地模型的方法
开发语言·python·bert
仰泳的熊猫17 小时前
1077 Kuchiguse
数据结构·c++·算法·pat考试