查看: 1781|回复: 1

opencv error:输入参数大小不匹配

[复制链接]

7

主题

28

帖子

79

积分

注册会员

Rank: 2

积分
79
发表于 2018-10-13 09:14:13 | 显示全部楼层 |阅读模式
本帖最后由 神龙教 于 2018-10-13 10:17 编辑

我正在使用金字塔进行图像混合…然后却得到一个opencv错误,,正在学习官方的opencv教程



代码如下:
  1. import cv2
  2. import numpy as np,sys

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

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

  11. # generate Gaussian pyramid for B
  12. G = B.copy()
  13. gpB = [G]
  14. for i in xrange(6):
  15.     G = cv2.pyrDown(G)
  16.     gpB.append(G)

  17. # generate Laplacian Pyramid for A
  18. lpA = [gpA[5]]
  19. for i in xrange(5,0,-1):
  20.    GE = cv2.pyrUp(gpA[i])
  21.    L = cv2.subtract(gpA[i-1],GE)
  22.    lpA.append(L)

  23. # generate Laplacian Pyramid for B
  24. lpB = [gpB[5]]
  25. for i in xrange(5,0,-1):
  26.     GE = cv2.pyrUp(gpB[i])
  27.     L = cv2.subtract(gpB[i-1],GE)
  28.     lpB.append(L)

  29. # Now add left and right halves of images in each level
  30. LS = []
  31. for la,lb in zip(lpA,lpB):
  32.     rows,cols,dpt = la.shape
  33.     ls = np.hstack((la[:,0:cols/2], lb[:,cols/2:]))
  34.     LS.append(ls)

  35. # now reconstruct
  36. ls_ = LS[0]
  37. for i in xrange(1,6):
  38.     ls_ = cv2.pyrUp(ls_)
  39.     ls_ = cv2.add(ls_, LS[i])

  40. # image with direct connecting each half
  41. real = np.hstack((A[:,:cols/2],B[:,cols/2:]))

  42. cv2.imwrite('Pyramid_blending2.jpg',ls_)
  43. cv2.imwrite('Direct_blending.jpg',real)
复制代码
报的错误如下:
  1. 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
  2. Traceback (most recent call last):
  3.   File "programs/test11.py", line 25, in <module>
  4.     L = cv2.subtract(gpA[i-1],GE)
  5. 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
复制代码
有木有可以帮忙解决的哇






回复

使用道具 举报

1

主题

49

帖子

118

积分

注册会员

Rank: 2

积分
118
发表于 2018-10-13 09:28:01 | 显示全部楼层
本帖最后由 3529956381 于 2018-10-13 10:17 编辑

你这个应该应用高斯金字塔有问题:
  1. # generate Gaussian pyramid for A
  2. G = A.copy()
  3. gpA = [G]
  4. for i in xrange(6):
  5.     G = cv2.pyrDown(G)
  6.     gpA.append(G)
复制代码

根据OpenCV关于cv2的文档。pyrDown,如果不指定dstsize,它将默认为((src.cols+1)/2, (src.rows+1)/2)
但是,你总是对原来的G拷贝采样。如果我没错的话,我觉得你应该把它应用到最后一个向下采样的图像上
  1. # generate Gaussian pyramid for A
  2. G = A.copy()
  3. gpA = [G]
  4. for i in xrange(6):
  5.     G = cv2.pyrDown(gpA[i])
  6.     gpA.append(G)
复制代码

显然,B金字塔也是如此
如果图像的形状是偶数而不是奇数,你的脚本就会工作,因为cv2。pyrDown计算默认大小,
在这种情况下,必须根据用来做cv2.substact图像的pyrUp的正确dstize参数给cv2,可能表诉不清楚,直接上代码
  1. # generate Laplacian Pyramid for A
  2. lpA = [gpA[5]]
  3. for i in xrange(5,0,-1):
  4.     size = (gpA[i-1].shape[1], gpA[i-1].shape[0])
  5.     GE = cv2.pyrUp(gpA[i], dstsize = size)
  6.     L = cv2.subtract(gpA[i-1],GE)
  7.     lpA.append(L)

  8. # generate Laplacian Pyramid for B
  9. lpB = [gpB[5]]
  10. for i in xrange(5,0,-1):
  11.     size = (gpB[i-1].shape[1], gpB[i-1].shape[0])
  12.     GE = cv2.pyrUp(gpB[i], dstsize = size)
  13.     L = cv2.subtract(gpB[i-1],GE)
  14.     lpB.append(L)
复制代码
重建部分也是一样的
  1. # now reconstruct
  2. ls_ = LS[0]
  3. for i in xrange(1,6):
  4.     size = (LS[i].shape[1], LS[i].shape[0])
  5.     ls_ = cv2.pyrUp(ls_, dstsize = size)
  6.     ls_ = cv2.add(ls_, LS[i])
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表