python计算两个三维图的psnr
时间: 2023-08-03 12:08:08 浏览: 187
要计算两个三维图像的PSNR,可以使用以下代码:
```python
import cv2
import numpy as np
import math
def psnr(target, ref):
# 将图像格式转为float64
target_data = np.array(target, dtype=np.float64)
ref_data = np.array(ref, dtype=np.float64)
# 直接相减,求差值
diff = ref_data - target_data
# 按第三个通道顺序把三维矩阵拉平
diff = diff.flatten('C')
# 计算MSE值
rmse = math.sqrt(np.mean(diff ** 2.))
# 精度
eps = np.finfo(np.float64).eps
if rmse == 0:
rmse = eps
return 20 * math.log10(255.0 / rmse)
# 读取两个三通道图像
IMG_1 = cv2.imread("image1.jpg")
IMG_2 = cv2.imread("image2.jpg")
# 计算PSNR值
psnr_value = psnr(IMG_1, IMG_2)
print("PSNR:", psnr_value)
```
请注意,这段代码假设输入的图像是三通道的。如果输入的图像是灰度图像,需要对代码进行相应的修改。此外,为了得到有意义的PSNR值,两个图像的大小应该相同。如果大小不同,可以使用`cv2.resize()`函数将它们调整为相同的大小。
#### 引用[.reference_title]
- *1* *2* [python/c++计算psnr](https://blog.csdn.net/cd_yourheart/article/details/121156270)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [【Python】计算psnr和ssim值](https://blog.csdn.net/Resume_f/article/details/103339722)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文