-2

I'd like to compare two images using "cv2.resize" because I have different size of images. But it shows some errors.

(omitted) Source:

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)

Console:

(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

How to fix them?

Sato
  • 29
  • 2
  • Does this answer your question? https://stackoverflow.com/questions/58621644/comparison-between-two-images-in-python –  Jul 28 '21 at 12:46
  • 1
    What's unclear? Resize expects a tuple of 2 values, but you provided one containing 3 values. Solution: Provide a tuple of 2 values. For example by using the width and the height, but not the number of channels. – mkrieger1 Jul 28 '21 at 12:49

1 Answers1

0

One of the comments already answered the question. But just to be more clear you need to pass a tuple of (width, height) in cv2.resize argument. Below is an worked example :

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)

Hope this helps! Cheers !!! Also since this is an elementary question, I suggest please check the OpenCV docs before posting. Here's an example in the doc.

Suvo
  • 1,318
  • 11
  • 13
  • Thanks a lot, everyone. It works normally. I'll see the OpenCV docs before posting. – Sato Jul 28 '21 at 13:59