2015-10-06 74 views
2

我使用金字塔進行圖像混合... m得到一個opencv錯誤.. 我遵循正式的opencv教程。 http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_tutorials.htmlopencv錯誤:輸入參數的大小不匹配

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

那麼,什麼是GPA [I-1]的'尺寸'和'GE'在該行/ – Divakar

回答

8

看來你是不產生你的高斯金字塔正確的位置:在cv2.pyrDown

# 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副本上進行下采樣。 如果我undertand正確的,我認爲你必須運用它的最後一個採樣圖象上:

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

Obiously,同樣也適用於您的乙金字塔

現在,如果由於cv2.pyrDown計算默認大小,您的圖像具有均勻的形狀但不具有奇怪的形狀,那麼您的腳本將起作用。在這種情況下,根據您用來做cv2.substract(或cv2.add)的圖像,您必須給cv2.pyrUp正確dstsize參數。

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

獲得?完全相同的錯誤,這次也是 – Zeeshan

+0

@ user3394251好吧,我想你的圖像有一個奇怪的形狀。我編輯了我測試過的東西。 – Gall

+1

感謝它的工作.... – Zeeshan

相關問題