Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python opencv错误:输入参数的大小不匹配_Python_Opencv_Numpy_Matplotlib - Fatal编程技术网

Python opencv错误:输入参数的大小不匹配

Python opencv错误:输入参数的大小不匹配,python,opencv,numpy,matplotlib,Python,Opencv,Numpy,Matplotlib,我用金字塔做图像混合。。。 我收到一个opencv错误。。 我正在学习opencv官方教程。 错误如下:- OpenCV Error: 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 'scala

我用金字塔做图像混合。。。 我收到一个opencv错误。。 我正在学习opencv官方教程。

错误如下:-

OpenCV Error: 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 arithm_op, file /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/arithm.cpp, line 1287
Traceback (most recent call last):
  File "programs/test11.py", line 25, in <module>
    L = cv2.subtract(gpA[i-1],GE)
cv2.error: /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/arithm.cpp:1287: error: (-209) 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 arithm_op
OpenCV错误:在arithm_op文件/build/buildd/OpenCV-2.4.8+dfsg1/modules/core/src/arithm.cpp第1287行中,输入参数的大小不匹配(操作既不是“array op array”(其中数组的大小和通道数相同),也不是“array op scalar”,也不是“scalar op array”
回溯(最近一次呼叫最后一次):
文件“programs/test11.py”,第25行,在
L=cv2.减去(gpA[i-1],GE)
cv2.error:/build/buildd/opencv-2.4.8+dfsg1/modules/core/src/arithm.cpp:1287:error:(-209)该操作既不是“数组运算数组”(其中数组具有相同大小和相同数量的通道),也不是“数组运算标量”,也不是函数arithm_op中的“标量运算数组”

这里似乎没有正确生成高斯金字塔:

# generate Gaussian pyramid for A
G = A.copy()
gpA = [G]
for i in xrange(6):
    G = cv2.pyrDown(G)
    gpA.append(G)
根据上的OpenCV文档,如果不指定
dstsize
,它将默认为
((src.cols+1)/2,(src.rows+1)/2)
。但是,您总是在原始
G
副本上进行下采样。 如果我理解正确,我认为您必须将其应用于最后一张下采样图像:

# generate Gaussian pyramid for A
G = A.copy()
gpA = [G]
for i in xrange(6):
    G = cv2.pyrDown(gpA[i])
    gpA.append(G)
显然,这同样适用于你的B金字塔

现在,如果图像的形状为偶数,而不是奇数,那么脚本将正常工作,因为
cv2.pyrDown
是如何计算默认大小的。在这种情况下,您必须根据用于执行
cv2.substract
(或
cv2.add
)的图像,向
cv2.pyrUp
提供适当的
dstsize
参数

那么,这一点也适用于重建部分:

# now reconstruct
ls_ = LS[0]
for i in xrange(1,6):
    size = (LS[i].shape[1], LS[i].shape[0])
    ls_ = cv2.pyrUp(ls_, dstsize = size)
    ls_ = cv2.add(ls_, LS[i])

以下代码适用于Python3,请尝试:

import cv2
import numpy as np,sys

A = cv2.imread('/home/grayhat/apple.jpg')
B = cv2.imread('/home/grayhat/orange.jpg')

# generate Gaussian pyramid for A
G = A.copy()
print(G)
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)

lpA = [gpA[5]]
for i in range(6,0,-1):
    print(i)
    GE = cv2.pyrUp(gpA[i])
    GE=cv2.resize(GE,gpA[i - 1].shape[-2::-1])
    L = cv2.subtract(gpA[i-1],GE)
    lpA.append(L)

# generate Laplacian Pyramid for B

lpB = [gpB[5]]
for i in range(6,0,-1):
    print(i)
    GE = cv2.pyrUp(gpB[i])
    GE = cv2.resize(GE, gpB[i - 1].shape[-2::-1])
    L = cv2.subtract(gpB[i-1],GE)
    print(L.shape)
    lpB.append(L)

# Now add left and right halves of images in each level
LS = []
lpAc=[]
for i in range(len(lpA)):
    b=cv2.resize(lpA[i],lpB[i].shape[-2::-1])
    lpAc.append(b)
print(len(lpAc))
print(len(lpB))
j=0
for i in zip(lpAc,lpB):
    la,lb = i
    rows,cols,dpt = la.shape
    ls = np.hstack((la[:,0:cols//2], lb[:,cols//2:]))
    j=j+1
    LS.append(ls)

ls_ = LS[0]
for i in range(1,6):
    ls_ = cv2.pyrUp(ls_)
    ls_= cv2.resize(ls_, LS[i].shape[-2::-1])
    ls_ = cv2.add(ls_, LS[i])

# image with direct connecting each half
B= cv2.resize(B, A.shape[-2::-1])
real = np.hstack((A[:,:cols//2],B[:,cols//2:]))
cv2.imwrite('Pyramid_blending2.jpg',ls_)
cv2.imwrite('Direct_blending.jpg',real)
参考:


那么,那一行的
gpA[i-1]
GE
的大小是多少?/@user3394251好的,我想你的图像有一个奇怪的形状。我编辑了测试中的内容。你能告诉我在哪里学习opencv吗?我是这方面的初学者。我必须开发一个虚拟镜像。任何好的资源都会非常有用。@user3394251很高兴我能帮上忙。我只使用OpenCV做一些基本的工作,所以当我不理解输入/输出时,我主要使用doc和python教程(如您所提到的)以及stackoverflow。但我想一个好办法是编写/查找一个算法,然后看看每个部分都有什么功能。有视频资源吗?我从来没有使用过视频,但你可以参考或了解基本知识。
# now reconstruct
ls_ = LS[0]
for i in xrange(1,6):
    size = (LS[i].shape[1], LS[i].shape[0])
    ls_ = cv2.pyrUp(ls_, dstsize = size)
    ls_ = cv2.add(ls_, LS[i])
import cv2
import numpy as np,sys

A = cv2.imread('/home/grayhat/apple.jpg')
B = cv2.imread('/home/grayhat/orange.jpg')

# generate Gaussian pyramid for A
G = A.copy()
print(G)
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)

lpA = [gpA[5]]
for i in range(6,0,-1):
    print(i)
    GE = cv2.pyrUp(gpA[i])
    GE=cv2.resize(GE,gpA[i - 1].shape[-2::-1])
    L = cv2.subtract(gpA[i-1],GE)
    lpA.append(L)

# generate Laplacian Pyramid for B

lpB = [gpB[5]]
for i in range(6,0,-1):
    print(i)
    GE = cv2.pyrUp(gpB[i])
    GE = cv2.resize(GE, gpB[i - 1].shape[-2::-1])
    L = cv2.subtract(gpB[i-1],GE)
    print(L.shape)
    lpB.append(L)

# Now add left and right halves of images in each level
LS = []
lpAc=[]
for i in range(len(lpA)):
    b=cv2.resize(lpA[i],lpB[i].shape[-2::-1])
    lpAc.append(b)
print(len(lpAc))
print(len(lpB))
j=0
for i in zip(lpAc,lpB):
    la,lb = i
    rows,cols,dpt = la.shape
    ls = np.hstack((la[:,0:cols//2], lb[:,cols//2:]))
    j=j+1
    LS.append(ls)

ls_ = LS[0]
for i in range(1,6):
    ls_ = cv2.pyrUp(ls_)
    ls_= cv2.resize(ls_, LS[i].shape[-2::-1])
    ls_ = cv2.add(ls_, LS[i])

# image with direct connecting each half
B= cv2.resize(B, A.shape[-2::-1])
real = np.hstack((A[:,:cols//2],B[:,cols//2:]))
cv2.imwrite('Pyramid_blending2.jpg',ls_)
cv2.imwrite('Direct_blending.jpg',real)