摘要:為了解決這個問題,算法使用和可以通過參數(shù)來設(shè)定所以好的匹配提供的正確的估計被稱為,剩下的被稱為返回一個掩模,這個掩模確定了和點
Feature Matching + Homography to find Objects
聯(lián)合使用特征提取和 calib3d 模塊中的 findHomography 在復(fù)雜圖像中查找已知對象.
之前在一張雜亂的圖像中找到了一個對象(的某些部分)的位置.這些信息足以幫助我們在目標(biāo)圖像中準(zhǔn)確的找到(查詢圖像)對象.為了達到這個目的可以使用 calib3d 模塊中的cv2.findHomography()函數(shù).如果將這兩幅圖像中的特征點集傳給這個函數(shù),他就會找到這個對象的透視圖變換.然后就可以使用函數(shù) cv2.perspectiveTransform() 找到這個對象了.至少要 4 個正確的點才能找到這種變換.在匹配過程可能會有一些錯誤,而這些錯誤會影響最終結(jié)果。為了解決這個問題,算法使用 RANSAC 和 LEAST_MEDIAN(可以通過參數(shù)來設(shè)定).所以好的匹配提供的正確的估計被稱為 inliers,剩下的被稱為outliers.cv2.findHomography() 返回一個掩模,這個掩模確定了 inlier 和outlier 點.
import numpy as np import cv2 import matplotlib.pyplot as plt MIN_MATCH_COUNT = 10 img1 = cv2.imread("img.jpg",0) # queryImage img2 = cv2.imread("img1.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 = 0 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 - %d/%d" % (len(good),MIN_MATCH_COUNT)) matchesMask = None # Finally we draw our inliers (if successfully found the object) or matching keypoints (if failed). 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()
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/42098.html
摘要:理論前面講的角點檢測器中的角點在旋轉(zhuǎn)的圖像中也是角點,但是縮放呢如果縮放圖像,則角可能不是角例如,檢查下面的簡單圖像,當(dāng)在同一窗口中放大時,小窗口內(nèi)的小圖像中的角是平坦的所以角點檢測器不是尺度不變的所以,在年,不列顛哥倫比亞大學(xué)的在他的論 Introduction to SIFT (Scale-Invariant Feature Transform) 理論 前面講的Harris角點檢...
摘要:匹配器匹配非常簡單,首先在第一幅圖像中選取一個關(guān)鍵點然后依次與第二幅圖像的每個關(guān)鍵點進行描述符距離測試,最后返回距離最近的關(guān)鍵點對于匹配器,首先我們必須使用創(chuàng)建對象。 Feature Matching Brute-Force匹配器 Brute-Force匹配非常簡單,首先在第一幅圖像中選取一個關(guān)鍵點然后依次與第二幅圖像的每個關(guān)鍵點進行(描述符)距離測試,最后返回距離最近的關(guān)鍵點. 對于...
閱讀 1474·2021-11-24 09:39
閱讀 1445·2021-11-04 16:12
閱讀 2816·2021-09-24 09:47
閱讀 3424·2021-09-01 10:50
閱讀 1559·2019-08-30 15:55
閱讀 1501·2019-08-30 15:43
閱讀 724·2019-08-30 11:08
閱讀 3655·2019-08-23 18:33