如本教程中所述,我试图使用 OpenCV ORB 匹配两个图像。
这是我的代码:
import numpy as np
import cv2
import six
import pyparsing
import dateutil
from matplotlib import pyplot as plt
import timeit
import os
import sys
img1_path = 'img1.jpg'
img2_path = 'img2.jpg'
img1 = cv2.imread(img1_path,0) # queryImage
img2 = cv2.imread(img2_path,0) # trainImage
orb = cv2.ORB()
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
FLANN_INDEX_LSH = 6
index_params= dict(algorithm = FLANN_INDEX_LSH,
table_number = 6, # 12
key_size = 12, # 20
multi_probe_level = 1) #2
search_params = dict(checks = 50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1,des2,k=2)
if len(matches)>0:
print "%d total matches found" % (len(matches))
else:
print "No matches were found - %d" % (len(good))
sys.exit()
# store all the good matches as per Lowe's ratio test.
good = []
for m,n in matches:
if m.distance < 0.6*n.distance:
good.append(m)
我用两个非常相似的图像运行了这个脚本。在大多数情况下,脚本可以正常工作并找到匹配的关键点。
但是,在某些情况下,我会收到此错误(它指的是最后三行代码):
Traceback (most recent call last):
for m,n in matches:
ValueError: need more than 1 value to unpack
当 img2 明显是 img1 的较小子图像时,就会发生这种情况。
(如果img2是原图,img1是修改后的图,说明有人在原图上加了细节)。
如果我在文件名 img1,img2 之间切换,则脚本运行没有问题。
查询图像 (img1) 必须小于还是等于火车图像 (img2)?