请帮我写一个opencv python 程序,计算图片经过均值滤波、中值滤波、自适应中值滤波、两次小波分解滤波后的MSE,P SNR值,从而评价这几个滤波处理的结果的好坏
时间: 2024-11-18 10:16:58 浏览: 27
【opencv学习笔记】010之图像非线性滤波原理与操作(中值滤波、双边滤波)
在OpenCV中,计算图像处理后(如滤波)的MSE(均方误差)和PSNR(峰值信噪比)通常用于比较原始图像和处理后的图像质量。以下是使用Python和OpenCV来实现这个功能的一个简单示例,涵盖了你提到的几种滤波方法:
```python
import cv2
import numpy as np
from skimage.measure import compare_ssim
from scipy.fftpack import dct, idct
def calculate_mse(original_image, processed_image):
mse = np.mean((original_image - processed_image) ** 2)
return mse
def calculate_psnr(original_image, processed_image):
mse = calculate_mse(original_image, processed_image)
max_pixel_value = 255.0 # 对于8位灰度图像
psnr = 10 * np.log10(max_pixel_value**2 / mse)
return psnr
# 假设你已经有了原始图像和滤波后的图像
original_image = cv2.imread('original.jpg', 0) # 读取灰度图像
processed_images = {
"mean": cv2.blur(original_image, (3, 3)), # 均值滤波
"median": cv2.medianBlur(original_image, 5), # 中值滤波
"adaptive_median": cv2.adaptiveMedianFilter(original_image, (5, 5), cv2.ADAPTIVE_THRESH_GAUSSIAN_C), # 自适应中值滤波
"wavelet": np.abs(dct(dct(original_image, norm="ortho", axis=-1), norm="ortho", axis=-2)), # 二次小波分解滤波
}
mse_values = []
psnr_values = []
for name, filtered_image in processed_images.items():
mse = calculate_mse(original_image, filtered_image)
psnr = calculate_psnr(original_image, filtered_image)
mse_values.append(mse)
psnr_values.append(psnr)
print(f"{name} MSE: {mse:.4f}, PSNR: {psnr:.4f}")
# 结果总结
print("\nOverall comparison:")
for i, (method, (mse, psnr)) in enumerate(zip(processed_images.keys(), zip(mse_values, psnr_values))):
print(f"{i+1}. {method}: MSE = {mse:.4f}, PSNR = {psnr:.4f}")
# 如果需要,可以使用skimage.metrics.structural_similarity()计算SSIM作为额外的评估指标
ssim_values = [compare_ssim(original_image, filtered_image, multichannel=True) for filtered_image in processed_images.values()]
print("SSIM values:", ssim_values)
```
这个程序首先定义了计算MSE和PSNR的方法,然后读取原始图像并应用各种滤波操作。最后,它计算每种滤波方法下的MSE和PSNR值,并输出对比结果。
阅读全文