Reputation: 23
I have followed the tutorials on opencv in python. I am using OpenCV 3.2 and Python 3.6.1. The code was written like this:
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()
As I run the module, an error occured:
Traceback (most recent call last): File "C:/Users/Albert Eli Reyes/Desktop/SIFT/SIFT.py", line 35, in h,w,d = img1.shape ValueError: not enough values to unpack (expected 3, got 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)
What does it mean when it says "not enough values to unpack"? And what should I do to make it run?
Upvotes: 1
Views: 1107
Reputation: 2272
Well, this means that img1.shape
returns only two values. So if you remove the d
variable initialization, the code should work as I don't see you using it in your code anyway.
h, w = img1.shape
Upvotes: 1
Reputation: 9931
Shape function outputs a tuple of two values only. You are trying to get three values whereas the function returns two values only
h,w = img1.shape
The above would work
Upvotes: 1