opencv python 使用特徵匹配和單應性查找對象

Feature Matching + Homography to find Objects html

聯合使用特徵提取和 calib3d 模塊中的 findHomography 在複雜圖像中查找已知對象.算法

以前在一張雜亂的圖像中找到了一個對象(的某些部分)的位置.這些信息足以幫助咱們在目標圖像中準確的找到(查詢圖像)對象.爲了達到這個目的能夠使用 calib3d 模塊中的cv2.findHomography()函數.若是將這兩幅圖像中的特徵點集傳給這個函數,他就會找到這個對象的透視圖變換.而後就能夠使用函數 cv2.perspectiveTransform() 找到這個對象了.至少要 4 個正確的點才能找到這種變換.在匹配過程可能會有一些錯誤,而這些錯誤會影響最終結果。爲了解決這個問題,算法使用 RANSACLEAST_MEDIAN(能夠經過參數來設定).因此好的匹配提供的正確的估計被稱爲 inliers,剩下的被稱爲outliers.cv2.findHomography() 返回一個掩模,這個掩模肯定了 inlier 和outlier 點.app

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()

clipboard.png

相關文章
相關標籤/搜索