簡體   English   中英

Python:特征匹配+同形異義查找多個對象

[英]Python : Feature Matching + Homography to find Multiple Objects

我試圖通過python使用opencv在火車圖像中找到多個對象,並將其與從查詢圖像中檢測到的關鍵點進行匹配。對於我而言,我試圖在下面提供的圖像中檢測網球場。 II看了在線教程,只發現它只能檢測到1個對象。 我想在其中插入一個循環以查找多個對象,但是我沒有這樣做。 對如何做有任何想法嗎? *我使用SIFT是因為ORB在我的情況下效果不佳

這是代碼和一組示例圖像。

import numpy as np
import cv2
from matplotlib import pyplot as plt

MIN_MATCH_COUNT = 10
img1 = cv2.imread('Image 11.jpg',0)          # queryImage
img2 = cv2.imread('Image 5.jpg',0) # trainImage

# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()

# find the keypoints and descriptors with SIFT
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)

# store all the good matches as per Lowe's ratio test.
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 = 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,255,0), # draw matches in green color
                   singlePointColor = None,
                   matchesMask = matchesMask, # draw only inliers
                   flags = 2)
img3 = cv2.drawMatches(img1,kp1,img2,kp2,good,None,**draw_params)
plt.imshow(img3, 'gray'),plt.show()

火車圖像

查詢圖片

提前致謝!

如果多次具有相同的圖像,則在尋找單應性方面會遇到一些問題。 即使出現循環,您的關鍵點描述也可能會圍繞不同的相同圖像混合在一起。 您可以進行預處理並重新組合關鍵點以進行多次匹配,但是對於不同尺寸的不同圖像,它可能很復雜,我建議使用模板匹配,但是困難在於縮放和旋轉不變性。 您可以閱讀本文以獲取一些幫助https://www.pyimagesearch.com/2015/01/26/multi-scale-template-matching-using-python-opencv/

希望對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM