用Python计算图像的MSE,PSNR
时间: 2024-04-07 15:07:32 浏览: 66
计算图像的MSE和PSNR需要用到Python的NumPy和OpenCV库。
首先,读取两幅图像并将它们转换为灰度图像:
```python
import cv2
import numpy as np
# 读取两幅图像
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
# 转换为灰度图像
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
```
接下来,计算两幅图像的MSE:
```python
mse = np.mean((gray1 - gray2) ** 2)
print("MSE:", mse)
```
最后,计算两幅图像的PSNR:
```python
psnr = 10 * np.log10(255**2 / mse)
print("PSNR:", psnr)
```
完整代码:
```python
import cv2
import numpy as np
# 读取两幅图像
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
# 转换为灰度图像
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# 计算MSE
mse = np.mean((gray1 - gray2) ** 2)
print("MSE:", mse)
# 计算PSNR
psnr = 10 * np.log10(255**2 / mse)
print("PSNR:", psnr)
```
阅读全文