opencv error:输入参数大小不匹配
本帖最后由 神龙教 于 2018-10-13 10:17 编辑我正在使用金字塔进行图像混合…然后却得到一个opencv错误,,正在学习官方的opencv教程
http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_tutorials.html
代码如下:
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()
gpA =
for i in xrange(6):
G = cv2.pyrDown(G)
gpA.append(G)
# generate Gaussian pyramid for B
G = B.copy()
gpB =
for i in xrange(6):
G = cv2.pyrDown(G)
gpB.append(G)
# generate Laplacian Pyramid for A
lpA = ]
for i in xrange(5,0,-1):
GE = cv2.pyrUp(gpA)
L = cv2.subtract(gpA,GE)
lpA.append(L)
# generate Laplacian Pyramid for B
lpB = ]
for i in xrange(5,0,-1):
GE = cv2.pyrUp(gpB)
L = cv2.subtract(gpB,GE)
lpB.append(L)
# Now add left and right halves of images in each level
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
for i in xrange(1,6):
ls_ = cv2.pyrUp(ls_)
ls_ = cv2.add(ls_, LS)
# 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)报的错误如下:
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,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有木有可以帮忙解决的哇
本帖最后由 3529956381 于 2018-10-13 10:17 编辑
你这个应该应用高斯金字塔有问题:
# generate Gaussian pyramid for A
G = A.copy()
gpA =
for i in xrange(6):
G = cv2.pyrDown(G)
gpA.append(G)
根据OpenCV关于cv2的文档。pyrDown,如果不指定dstsize,它将默认为((src.cols+1)/2, (src.rows+1)/2)
但是,你总是对原来的G拷贝采样。如果我没错的话,我觉得你应该把它应用到最后一个向下采样的图像上
# generate Gaussian pyramid for A
G = A.copy()
gpA =
for i in xrange(6):
G = cv2.pyrDown(gpA)
gpA.append(G)
显然,B金字塔也是如此
如果图像的形状是偶数而不是奇数,你的脚本就会工作,因为cv2。pyrDown计算默认大小,
在这种情况下,必须根据用来做cv2.substact图像的pyrUp的正确dstize参数给cv2,可能表诉不清楚,直接上代码
# generate Laplacian Pyramid for A
lpA = ]
for i in xrange(5,0,-1):
size = (gpA.shape, gpA.shape)
GE = cv2.pyrUp(gpA, dstsize = size)
L = cv2.subtract(gpA,GE)
lpA.append(L)
# generate Laplacian Pyramid for B
lpB = ]
for i in xrange(5,0,-1):
size = (gpB.shape, gpB.shape)
GE = cv2.pyrUp(gpB, dstsize = size)
L = cv2.subtract(gpB,GE)
lpB.append(L)重建部分也是一样的
# now reconstruct
ls_ = LS
for i in xrange(1,6):
size = (LS.shape, LS.shape)
ls_ = cv2.pyrUp(ls_, dstsize = size)
ls_ = cv2.add(ls_, LS)
页:
[1]