我已经在python中关注了opencv上的教程。我正在使用OpenCV 3.2和Python 3.6.1。代码写得像这样:
import cv2
import numpy as np
import matplotlib.pyplot as plt
MIN_MATCH_COUNT = 10
img1 = cv2.imread('test.jpg',0)
img2 = cv2.imread('hanapin_mo.jpg',0)
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k = 2)
good = []
for m,n in matches:
if m.distance <0.7*n.distance:
good.append(m)
if len(good)>MIN_MATCH_COUNT:
src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
matchesMask = mask.ravel().tolist()
h,w,d = img1.shape
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
dst = cv2.perspectiveTransform(pts,M)
img2 = cv2.polylines(img2,[np.int32(dst)], True, 255, 3, cv2.LINE_AA)
else:
print("Not enough matches are found - {}/{}".format(len(good), MIN_MATCH_COUNT) )
matchesMask = None
draw_params = dict(matchColor = (0,250,0),
singlePointColor = None,
matchesMask = matchesMask,
flags = 2)
img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, None, **draw_params)
plt.imshow(img3, 'gray'),plt.show()
当我运行模块时,发生错误:
追踪(最近一次通话): File&#34; C:/ Users / Albert Eli Reyes / Desktop / SIFT / SIFT.py&#34;,第35行,in h,w,d = img1.shape ValueError:没有足够的值来解包(预期3,得到2)
h,w,d = img1.shape
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
dst = cv2.perspectiveTransform(pts,M)
img2 = cv2.polylines(img2,[np.int32(dst)], True, 255, 3, cv2.LINE_AA)
当它说“没有足够的价值来打开包装”时意味着什么?我应该怎么做才能让它运行?
答案 0 :(得分:1)
Shape函数仅输出两个值的元组。您试图获得三个值,而该函数仅返回两个值
h,w = img1.shape
上述工作
答案 1 :(得分:1)
嗯,这意味着img1.shape
只返回两个值。因此,如果您删除d
变量初始化,则代码应该正常工作,因为我还没有看到您在代码中使用它。
h, w = img1.shape