PSNR PYTHON
时间: 2024-03-23 22:34:46 浏览: 139
PSNR(Peak Signal-to-Noise Ratio)是一种用于衡量图像或视频质量的指标,它可以用来评估原始图像与压缩或失真后的图像之间的差异程度。在Python中,可以使用OpenCV库来计算PSNR。
以下是使用Python计算PSNR的示例代码:
```python
import cv2
import numpy as np
def calculate_psnr(original_image, compressed_image):
# 读取原始图像和压缩图像
img1 = cv2.imread(original_image)
img2 = cv2.imread(compressed_image)
# 将图像转换为灰度图像
gray_img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray_img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# 计算MSE(Mean Squared Error)
mse = np.mean((gray_img1 - gray_img2) ** 2)
# 计算PSNR(Peak Signal-to-Noise Ratio)
psnr = 10 * np.log10((255 ** 2) / mse)
return psnr
# 示例用法
original_image = "path/to/original_image.jpg"
compressed_image = "path/to/compressed_image.jpg"
psnr_value = calculate_psnr(original_image, compressed_image)
print("PSNR:", psnr_value)
```
请注意,上述示例代码中的`original_image`和`compressed_image`需要替换为实际的图像文件路径。
阅读全文