cv2.error: OpenCV(4.7.0)


import cv2
import numpy as np

img = cv2.imread('D:\\grade1\\05\\IMG_1415.JPG')
resized = cv2.resize(img, (1200, 1920), interpolation=cv2.INTER_AREA)
print(img.shape)
img1 = np.ones((1200,1920,3),np.uint8)*50
cv2.imshow('1',img1)
result = cv2.add(resized,img1)

cv2.imshow('result',result)
cv2.waitKey(0)

只有图片长宽一样才能运行,不然就报错,如下:

cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:650: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'cv::arithm_op'

cv2.resize 的参数是 (w,h) ,不是 (h,w),这很特殊。
所以 cv2.resize(img, (1200, 1920)) 后图片尺寸变成了 (h,w)=(1920,1200)
而 img1 = np.ones((1200,1920,3) 的图片尺寸是 (h,w)=(1200,1920)

img


看下例子