两幅图像的比较

2024-06-26 13:47:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用“cv2.resize”比较两个图像,因为我有不同大小的图像。 但它显示出一些错误

(略)资料来源:

def img_compare(passA, passB):
    imageA = cv2.imread(passA)
    imageB = cv2.imread(passB)

    print (imageA.shape) # for debug
    print (imageB.shape) # for debug

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

控制台:

(728, 1034, 3)
(721, 1020, 3)
Traceback (most recent call last):
  File "comp.py", line 156, in <module>
    throw_compare()
  File "comp.py", line 150, in throw_compare
    img_compare(passA, passB)
  File "comp.py", line 89, in img_compare
    imageA = cv2.resize(imageA, imageB.shape)
cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'resize'
> Overload resolution failed:
>  - Can't parse 'dsize'. Expected sequence length 2, got 3
>  - Can't parse 'dsize'. Expected sequence length 2, got 3

如何修复它们


Tags: inpy图像imglinecv2filecompare
1条回答
网友
1楼 · 发布于 2024-06-26 13:47:26

其中一条评论已经回答了这个问题。但是为了更清楚,您需要在cv2.resize参数中传递一个(宽度、高度)元组。下面是一个工作示例:

import numpy as np
import cv2

im_1 = np.random.rand(64, 64, 3)
im_2 = np.random.rand(90, 75, 3)

new_im = cv2.resize(im_2, (im_1.shape[0], im_1.shape[1]),
                    interpolation = cv2.INTER_AREA)

print (new_im.shape)


>>> (64, 64, 3)

希望这有帮助!干杯此外,由于这是一个基本问题,我建议在发布之前请检查OpenCV文档Here's文档中的一个示例

相关问题 更多 >