如何修复错误:操作数不能与形状一起广播(450,600,3)(277,330,3)


import math
import cv2
import numpy as np
original = cv2.imread(r"C:UsersHPDocumentsfypimg4.bmp", 1)
contrast = cv2.imread(r"C:UsersHPDocumentsfypimgdehaze4.png", 1)
def psnr(img1, img2):
mse = np.mean((img1 - img2) ** 2)
if mse == 0:
return 100
PIXEL_MAX = 255.0
return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
d = psnr(original, contrast)
print(d)

误差

runfile('C:/Users/HP/Documents/fyp/dcp/pnsr2.py', wdir='C:/Users/HP/Documents/fyp/dcp')
Traceback (most recent call last):
File ~Documentsfypdcppnsr2.py:15 in <module>
d = psnr(original, contrast)
File ~Documentsfypdcppnsr2.py:9 in psnr
mse = np.mean((img1 - img2) ** 2)
ValueError: operands could not be broadcast together with shapes (450,600,3) (277,330,3)

帮我解决这个问题。

两张图片形状不同。我不知道你在比较两个不同大小的图像做什么,但一种方法是将其中一个图像的大小调整为另一个的大小:

def psnr(img1, img2):
if img1.shape != img2.shape:
img2 = cv2.resize(img2, img1.shape, interpolation=cv2.INTER_LINEAR)

相关内容

  • 没有找到相关文章

最新更新