arithm.cpp:650: error: (-209:Sizes of input arguments do not match).

问题描述:

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 . error:OpenCV(4 . 7 . 0)D:\ a \ OpenCV-python \ OpenCV-python \ OpenCV \ modules \ core \ src \ arithm . CPP:650:错误:(-209:输入参数的大小不匹配)。该操作既不是“数组运算数组”(其中数组具有相同的大小和相同的通道数),也不是“数组运算标量”,也不是函数“cv::arithm_op”中的“标量运算数组”。

问题解答:

猜测是两张图片在做加减与或非运算时,大小不一致。

背景是我在学习OpenCV-Python中文教程时,有一小节叫做,使用金字塔进行图像融合。

源码如下:

import cv2
import numpy as np,sys
A =cv2.imread('apple.jpg')
B =cv2.imread('orange.jpg')
#generate Gaussian pyramid for A
G =A.copy()
gpA= [G]
for i in  range(6):
    G = cv2.pyrDown(G)
    gpA.append(G)
#generate Gaussian pyramid for B
G =B.copy()
gpB= [G]
for i in range(6):
    G = cv2.pyrDown(G)
    gpB.append(G)
#generate Laplacian Pyramid for A
lpA= [gpA[5]]
for i in range(5,0,-1):
    GE = cv2.pyrUp(gpA[i])
    L = cv2.subtract(gpA[i-1],GE)
    lpA.append(L)
#generate Laplacian Pyramid for B
lpB= [gpB[5]]
for i in range(5,0,-1):
    GE = cv2.pyrUp(gpB[i])
    L = cv2.subtract(gpB[i-1],GE)
    lpB.append(L)
#Now add left and right halves of images in each level
#numpy.hstack(tup)
#Take a sequence of arrays and stack them horizontally
#to make a single array.
LS= []
for la,lb in zip(lpA,lpB):
    rows,cols,dpt = la.shape
    ls = np.hstack((la[:,0:cols/2],lb[:,cols/2:]))
    LS.append(ls)
#now reconstruct
ls_= LS[0]
for i in range(1,6):
    ls_ =cv2.pyrUp(ls_)
    ls_ =cv2.add(ls_,LS[i])
#image with direct connecting each half
real= np.hstack((A[:,:cols/2],B[:,cols/2:]))
cv2.imwrite('Pyramid_blending2.jpg',ls_)
cv2.imwrite('Direct_blending.jpg',real)

奇怪的是,我采用的两种图像大小完全一致,但是在进行金字塔融合时,出现了两张图片大小不一致的错误。因此,对上面原码进行修改。

大致修改内容就是在进行融合加减之前,先使用

cv2.resize()函数进行统一尺寸大小。

修改后又出现了这个错误,TypeError: slice indices must be integers or None or have an __index__ method。

具体如下:

C:\Users\aoqia\anaconda3\envs\yolov5\python.exe C:/Users/aoqia/PycharmProjects/pythonProject/tezhengjinzita.py
Traceback (most recent call last):
  File "C:/Users/aoqia/PycharmProjects/pythonProject/tezhengjinzita.py", line 42, in <module>
    ls = np.hstack((la[:,0:cols/2],lb[:,cols/2:]))
TypeError: slice indices must be integers or None or have an __index__ method

Process finished with exit code 1

接下来,我把里面的/修改为了//,即将除号修改为了整除。

两个步骤的修改之后的代码如下:

import cv2
import numpy as np,sys
A =cv2.imread('apple.jpg')
B =cv2.imread('orange.jpg')
#generate Gaussian pyramid for A
G =A.copy()
gpA= [G]
for i in  range(6):
    G = cv2.pyrDown(G)
    gpA.append(G)
#generate Gaussian pyramid for B
G =B.copy()
gpB= [G]
for i in range(6):
    G = cv2.pyrDown(G)
    gpB.append(G)
#generate Laplacian Pyramid for A
lpA= [gpA[5]]
for i in range(5,0,-1):
    GE = cv2.pyrUp(gpA[i])
    GE = cv2.resize(GE, (gpA[i-1].shape[1], gpA[i-1].shape[0]))
    L = cv2.subtract(gpA[i-1],GE)
    lpA.append(L)
#generate Laplacian Pyramid for B
lpB= [gpB[5]]
for i in range(5,0,-1):
    GE = cv2.pyrUp(gpB[i])
    GE = cv2.resize(GE, (gpB[i - 1].shape[1], gpB[i - 1].shape[0]))
    L = cv2.subtract(gpB[i-1],GE)
    lpB.append(L)
#Now add left and right halves of images in each level
#numpy.hstack(tup)
#Take a sequence of arrays and stack them horizontally
#to make a single array.
LS= []
for la,lb in zip(lpA,lpB):
    rows,cols,dpt = la.shape
    ls = np.hstack((la[:,0:cols//2],lb[:,cols//2:]))
    LS.append(ls)
#now reconstruct
ls_= LS[0]
for i in range(1,6):
    ls_ =cv2.pyrUp(ls_)
    LS[i] = cv2.resize(LS[i], (ls_.shape[1], ls_.shape[0]))
    ls_ =cv2.add(ls_,LS[i])
#image with direct connecting each half
real= np.hstack((A[:,:cols//2],B[:,cols//2:]))
cv2.imwrite('Pyramid_blending2.jpg',ls_)
cv2.imwrite('Direct_blending.jpg',real)

 最终的运行结果如下图所示。

第一个图是直接拼接,之间会有连接区域图像像素的不连续性,整幅图的效果看起来会很差。

第二个图是金字塔融合,融合之后如下所示,看着结果就比较协调连续。

###################################################################

 

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'.这个错误的原因是 L = cv2.subtract(gpA[i-1],GE)和L = cv2.subtract(gpA[i-1],GE)、 ls_ =cv2.add(ls_,LS[i])两张图片在做加减与或非运算时,大小不一致。但是问题是,我的输入apple.jpg和orange.jpg两张图片的大小是一致的,为什么他们的子图就是不一致了呢?解释一下两个夫图大小是一致的,但是处理后的子图是大小不一样的原因是什么呢?

即使输入的两张图像 apple.jpgorange.jpg 尺寸一致,但在生成高斯金字塔和拉普拉斯金字塔的过程中,图像的大小会逐渐减小。每次使用 cv2.pyrDown() 函数时,图像的尺寸都会减半,因此在处理过程中生成的子图的大小将会不同。

具体来说:

  1. 高斯金字塔:每次 pyrDown 操作都会使图像宽高各减半。
  2. 拉普拉斯金字塔:在从高斯金字塔中生成拉普拉斯金字塔时,通过从上一级的高斯图像中减去上一级金字塔的上采样图像(使用 pyrUp)得到。因此,金字塔的每一层图像大小都会与其下一级图像不同。

因此,虽然原始图像的大小相同,但在逐层处理的过程中,每一层的图像大小逐渐减少,导致后续的操作中出现尺寸不匹配的错误。确保在每一步操作前检查和调整图像大小,以避免这些问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神笔馬良

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值