2017-09-02 73 views
0

有兩張圖片,它們具有相同的面積,如:使用Python用的OpenCV實現圖像拼接

enter image description here

enter image description here

我想拼接2個images.My代碼如下

import numpy as np 
import cv2 

leftImg = cv2.imread('D:\\1.jpg') 
rightImg = cv2.imread('D:\\2.jpg') 
leftgray=cv2.cvtColor(leftImg,cv2.COLOR_BGR2GRAY) 
rightgray=cv2.cvtColor(rightImg,cv2.COLOR_BGR2GRAY) 

hessian=400 
surf=cv2.SURF(hessian) 
kp1,des1=surf.detectAndCompute(leftgray,None) 
kp2,des2=surf.detectAndCompute(rightgray,None) 


FLANN_INDEX_KDTREE=0 
indexParams=dict(algorithm=FLANN_INDEX_KDTREE,trees=5) 
searchParams=dict(checks=50) 
flann=cv2.FlannBasedMatcher(indexParams,searchParams) 
matches=flann.knnMatch(des1,des2,k=2) 

h,w=leftgray.shape[:2] 
good=[] 
for m,n in matches: 
    if m.distance < 0.7*n.distance: 
     good.append(m) 

src_pts = np.array([ kp1[m.queryIdx].pt for m in good]) 
dst_pts = np.array([ kp2[m.trainIdx].pt for m in good]) 
H=cv2.findHomography(src_pts,dst_pts) 
dst_corners=cv2.warpPerspective(leftgray,H,(w*2,h)) 

dst_corners[0:h,w:w*2]=rightgray 
cv2.imwrite('tiled.jpg',dst_corners) 
cv2.imshow('tiledImg',dst_corners) 

cv2.waitKey() 
cv2.destroyAllWindows() 

但我得到了一個錯誤,就是

dst_corners=cv2.warpPerspective(leftgray,H,(w*2,h)) 

TypeError: M is not a numerical tuple 

我該怎麼辦?請告訴我如何實現該功能,非常感謝!

回答

2

如果打印單應性矩陣,您會明白自己的錯誤。將此行更改爲。

dst_corners=cv2.warpPerspective(leftgray,H[0],(w*2,h)) 
+0

問題是解決了,謝謝 – chaoCN

0

正如this solution建議你應該改變

H, mask =cv2.findHomography(src_pts,dst_pts) 

線和分離輸出

+0

另外一個人的問題解決了,謝謝 – chaoCN