我正在使用金字塔进行图像混合...... 我得到一个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 = [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
答案 0 :(得分:10)
您似乎没有在此正确生成高斯金字塔:
# generate Gaussian pyramid for A
G = A.copy()
gpA = [G]
for i in xrange(6):
G = cv2.pyrDown(G)
gpA.append(G)
根据cv2.pyrDown上的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.pyrUp
(或dstsize
)的图片,向cv2.substract
提供正确的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)
然后,这一点也适用于重建部分:
# 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])
答案 1 :(得分:0)
以下代码适用于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)
参考:
https://docs.opencv.org/3.1.0/dc/dff/tutorial_py_pyramids.html