opencv错误:输入参数的大小不匹配 [英] opencv error:Sizes of input arguments do not match

查看:120
本文介绍了opencv错误:输入参数的大小不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用金字塔进行图像融合... 我收到一个opencv错误. 我正在关注官方的opencv教程. http://docs.opencv.org/3.0-beta/doc/py_tutorials /py_tutorials.html

i m doing image blending using pyramid... m getting an opencv error.. i m following the official opencv tutorials. 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 = [G]
for i in xrange(6):
    G = cv2.pyrDown(G)
    gpA.append(G)

# generate Gaussian pyramid for B
G = B.copy()
gpB = [G]
for i in xrange(6):
    G = cv2.pyrDown(G)
    gpB.append(G)

# generate Laplacian Pyramid for A
lpA = [gpA[5]]
for i in xrange(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 xrange(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
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 xrange(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)

以下是错误:-

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

推荐答案

似乎您在此处未正确生成高斯金字塔:

It seems you are not generating your Gaussian pyramid properly here:

# generate Gaussian pyramid for A
G = A.copy()
gpA = [G]
for i in xrange(6):
    G = cv2.pyrDown(G)
    gpA.append(G)

根据 cv2.pyrDown ,如果您未指定dstsize,它将默认为((src.cols+1)/2, (src.rows+1)/2).但是,您总是在原始G副本上进行下采样. 如果我理解正确,我认为您必须将其应用于最后一个降采样的图像:

According to OpenCV documentation on cv2.pyrDown, if you don't specify the dstsize, it will default to ((src.cols+1)/2, (src.rows+1)/2). BUT, you are always downsampling on the original G copy. If I undertand correctly, I think you have to apply it on the last downsampled image:

# generate Gaussian pyramid for A
G = A.copy()
gpA = [G]
for i in xrange(6):
    G = cv2.pyrDown(gpA[i])
    gpA.append(G)

很可能,您的 B金字塔也是如此.

Obiously, the same applies to your B pyramid.

现在,由于cv2.pyrDown计算默认大小的原因,如果图像具有偶数形状但不具有奇数形状,则脚本将起作用.在这种情况下,必须根据用于执行cv2.substract(或cv2.add)的图像将cv2.pyrUp适当的参数赋予cv2.pyrUp.

Now, your script will work if your images have an even shape but not with an odd shape because of how cv2.pyrDown computes the default size. In this case, you have to give to cv2.pyrUp the proper dstsize paramater according to the image you use to do the cv2.substract (or cv2.add).

# generate Laplacian Pyramid for A
lpA = [gpA[5]]
for i in xrange(5,0,-1):
    size = (gpA[i-1].shape[1], gpA[i-1].shape[0])
    GE = cv2.pyrUp(gpA[i], dstsize = size)
    L = cv2.subtract(gpA[i-1],GE)
    lpA.append(L)

# generate Laplacian Pyramid for B
lpB = [gpB[5]]
for i in xrange(5,0,-1):
    size = (gpB[i-1].shape[1], gpB[i-1].shape[0])
    GE = cv2.pyrUp(gpB[i], dstsize = size)
    L = cv2.subtract(gpB[i-1],GE)
    lpB.append(L)

然后,这一点也适用于重建部分:

Then, this point applies to the reconstruction part too:

# 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])

这篇关于opencv错误:输入参数的大小不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆