图像滤波算法 python

作者 : admin 本文共1528个字,预计阅读时间需要4分钟 发布时间: 2024-06-10 共2人阅读

1. 平均滤波 (Mean Filtering)

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

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)

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

# 应用高斯滤波
gaussian_filtered = cv2.GaussianBlur(image, (5, 5), 0)

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

3. 中值滤波 (Median Filtering)

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

# 应用中值滤波
median_filtered = cv2.medianBlur(image, 5)

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

4. 双边滤波 (Bilateral Filtering)

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

# 应用双边滤波
bilateral_filtered = cv2.bilateralFilter(image, 9, 75, 75)

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

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

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

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

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

6. 小波去噪 (Wavelet Denoising)

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

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()
本站无任何商业行为
个人在线分享 » 图像滤波算法 python
E-->