基于关键点的Python图像匹配提速方案咨询(附实现代码)
图像实时匹配性能优化需求
场景与问题描述
将相册图片存入本地文件夹并打印为纸质版,使用笔记本摄像头拍摄纸质图片,现有Python代码可识别匹配文件夹中的对应图片。但当文件夹内图片数量增至约1000张时,匹配速度变慢(单帧匹配耗时约0.4秒),需实现20fps的实时匹配效果。
已尝试方案
- 深度学习方案:无效;
- FLANN特征匹配器:匹配速度无明显提升,耗时仍约0.4秒。
现有实现代码
import cv2 import os from pathlib import Path import time # Define path pathImages = 'ImagesQuery' # Set the Number of detected Features orb = cv2.ORB.create(nfeatures=1000) # Set the threshold of minimum Features detected to give a positive, around 20 to 30 thres = 27 # List Images and Print out their Names and how many there are in the Folder images = [] classNamesImages = [] myListImages = os.listdir(pathImages) print(myListImages) print('Total Classes Detected', len(myListImages)) # this will read in the images for cl in myListImages: imgCur = cv2.imread(f'{pathImages}/{cl}', 0) images.append(imgCur) # delete the file extension classNamesImages.append(os.path.splitext(cl)[0]) print(classNamesImages) # this will find the matching points in the images def findDes(images): desList = [] for img in images: kp, des = orb.detectAndCompute(img, None) desList.append(des) return desList # this will compare the matches and find the corresponding image def findID(img, desList): image_matching_time = time.time() kp2, des2 = orb.detectAndCompute(img, None) bf = cv2.BFMatcher() matchList = [] finalVal = -1 try: for des in desList: matches = bf.knnMatch(des, des2, k=2) good = [] for m, n in matches: # the distance of the matches in comparison to each other if m.distance < 0.75 * n.distance: good.append([m]) matchList.append(len(good)) except: pass # uncomment to see how many positive matches, according to this the thres is set # print(matchList) if len(matchList) != 0: if max(matchList) > thres: finalVal = matchList.index(max(matchList)) print("matching time --- %s seconds ---" % (time.time() - image_matching_time)) return finalVal desList = findDes(images) print(len(desList)) # open Webcam cap = cv2.VideoCapture(1) while True: success, img2 = cap.read() imgOriginal = img2.copy() # convert Camera to Grayscale img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # if Matching with Image in List, send the respective Name id = findID(img2, desList) if id != -1: # put text for the found Image cv2.putText(imgOriginal, classNamesImages[id], (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3) # show the final Image cv2.imshow('img2', imgOriginal) cv2.waitKey(1)
补充说明
- 场景演示:通过摄像头拍摄纸质打印图,匹配本地文件夹内对应图片
- 示例图片:纸质打印图与原相册图的匹配示例
内容的提问来源于stack exchange,提问作者Qazi Ammar
相关产品推荐
相关产品推荐

