pro*_*tic 5 opencv image-processing feature-detection python-2.7 orb
如本教程所述,我试图使用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)
Run Code Online (Sandbox Code Playgroud)
我用两个非常相似的图像运行了该脚本。在大多数情况下,脚本可以正常工作并找到匹配的关键点。
但是,在某些情况下,我会收到此错误(它指向代码的最后三行):
Traceback (most recent call last):
for m,n in matches:
ValueError: need more than 1 value to unpack
Run Code Online (Sandbox Code Playgroud)
当img2明显是img1的较小子图像时,就会发生这种情况。
(如果img2是原始图像,而img1是修改后的图像,则意味着有人在原始图像中添加了详细信息)。
如果我在文件名img1,img2之间切换,则脚本可以正常运行。
查询图片(img1)是否必须小于火车图像(img2)?
小智 5
必须检查匹配列表的每个成员是否真的存在两个邻居。这与图像大小无关。
good = []
for m_n in matches:
if len(m_n) != 2:
continue
(m,n) = m_n
if m.distance < 0.6*n.distance:
good.append(m)
Run Code Online (Sandbox Code Playgroud)