Python OpenCV,ORB检测:与多个图像相比,如何返回最佳匹配?
在将此图像与数组中的多个图像进行比较时,我尝试从输入图像中获得最佳匹配,更具体地说,尝试分析一个书皮并将其与数组中的书皮进行比较,返回正确的一个。有两种方法我想这样做,但我不完全确定如何Python OpenCV,ORB检测:与多个图像相比,如何返回最佳匹配?,python,numpy,opencv,cv2,brute-force,Python,Numpy,Opencv,Cv2,Brute Force,在将此图像与数组中的多个图像进行比较时,我尝试从输入图像中获得最佳匹配,更具体地说,尝试分析一个书皮并将其与数组中的书皮进行比较,返回正确的一个。有两种方法我想这样做,但我不完全确定如何 import cv2 import numpy as np import matplotlib.pyplot as plt import time class imageCapture(object): def __init__(self): self.image = None
import cv2
import numpy as np
import matplotlib.pyplot as plt
import time
class imageCapture(object):
def __init__(self):
self.image = None
self.storedImages = [r'C:\pythonImg\image1.jpg']
def captureImage(self):
time.sleep(2)
cap = cv2.VideoCapture(0)
if cap.isOpened():
ret, frame = cap.read()
print("The image has been captured: " + str(ret))
else:
ret = False
img1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #this converts the colours to RGB from BGR
self.image = img1
def get_Matches_Orb(self): #at this point in time we are comparing it to a saved path image - eventually save the vectors
#trainImg = cv2.imread(self.image,0)
for image in self.storedImages:
storedImg = cv2.imread(image,0)
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(self.image,None) #this finds keypoints and descriptors with SIFT
kp2, des2 = orb.detectAndCompute(storedImg,None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) #create a bfMatcher object
matches = bf.match(des1,des2) #Match descriptors
matches = sorted(matches, key = lambda x:x.distance) #sorts them in order of their distance - lowest distance first.
sum = 0
for i in matches:
sum = sum + i.distance
avg = sum / len(matches)
print(avg)
#print(avg.distance)
img3 = cv2.drawMatches(self.image,kp1,storedImg,kp2,matches[:10],None, flags=2) #helps us to draw the matches.
plt.imshow(img3)
plt.show()
testobj = imageCapture()
testobj.captureImage()
testobj.get_Matches_Orb()
我的一个想法是使用DMatch.distance获得DMatch返回的平均距离,并返回平均距离最低的图像-到目前为止,我只生成了平均值,并使用不同的书籍封面对其进行了测试。事实上,与正确的书皮相比,平均距离似乎更低,但我想知道这是否准确。这就引出了一个问题:有没有办法使用kp1和kp2中存储的关键点来更准确地比较图像?比较所有匹配距离是个坏主意,因为这种方法存在大量异常值 你需要做的是使用findHomography。这将为您提供当前图像和字典图像之间的转换 然后,您必须选择一些参数来验证或拒绝这种单应性 对我来说,最简单的方法是将字典中的一个方框投影到您当前的图像中,并尝试查看方框的形状是否足够好,区域是否在一个良好的范围比例?角是否具有允许的角度?等或者,您可以设置一个阈值的数目inlier,他们的重新分区等 为了使某个东西变得健壮,您可能会使用几个标准 看看这个教程。他们使用了surf keypoint,但您可以使用orb