python中的图像尺寸错误

新手上路,请多包涵

试图匹配两个图像以找出它们之间的分数。但它显示了一些尺寸错误。无法解决问题。我的代码如下:

 from skimage.measure import compare_ssim
#import argparse
#import imutils
import cv2

img1="1.png"
img2="2.png"

# load the two input images
imageA = cv2.imread(img1)
imageB = cv2.imread(img2)

# convert the images to grayscale
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)

# compute the Structural Similarity Index (SSIM) between the two
# images, ensuring that the difference image is returned
(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
print("SSIM: {}".format(score))

这给 n 一个错误:

 raise ValueError('Input images must have the same dimensions.')

ValueError: Input images must have the same dimensions.

如何解决这个问题?

原文由 Shaon Paul 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 3k
1 个回答

修改 Saurav Panda 的回答:

您可以像这样将其中一张图像重塑为其他图像的大小:

 imageB=cv2.resize(imageB,imageA.shape)

注意

(H, W) = imageA.shape
# to resize and set the new width and height
imageB = cv2.resize(imageB, (W, H))

cv2.resize 函数输入期望(W,H)。这是 cv2.shape (H,W) 输出的逆序,所以你需要抓住它,否则在比较非正方形图像时你会得到同样的错误。

原文由 Marc Maxmeister 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题