有两张图片,它们具有相同的区域,例如:
我想拼接两张图片。我的代码如下
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
我该怎么办?请告诉我如何实现这个功能,非常感谢!
答案 0 :(得分:2)
如果您打印单应矩阵,您将理解您的错误。将此行更改为。
dst_corners=cv2.warpPerspective(leftgray,H[0],(w*2,h))
答案 1 :(得分:0)