2017-05-04 96 views
1

我遵循OpenCV Feature Detection and Description tutorial並在OpenCV中使用SIFT和其他算法來查找2個圖像之間的匹配特徵點。據我所知,這些算法可以找到2個圖像之間的相似區域。但我有興趣確定不同或不相似的地區。

如何在兩張圖像上繪製所有不匹配的特徵點?此外,我可以在這些非匹配點周圍繪製邊界,以便能夠顯示2幅圖像中的哪些區域不同?OpenCV繪製非匹配點

我在Windows 7上使用Python代碼,並從最新的OpenCV源代碼構建。

+0

你能提供一些你的代碼(特別是你爲這兩個圖像生成關鍵點和描述符的部分,以及你在哪裏做'匹配')? –

+0

我可以解決它..我張貼我的回答下面 –

+0

你仍然需要一些提示您的問題的第二部分? –

回答

0

原來是簡單的列表操作任務。這裏是我的Python代碼

# code copied from 
# http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_feature_homography/py_feature_homography.html 

import numpy as np 
import cv2 
from matplotlib import pyplot as plt 
from scipy.spatial.distance import euclidean 

MIN_MATCH_COUNT = 10 

img1 = cv2.imread('Src.png',0) # queryImage 
img2 = cv2.imread('Dest.png',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) 

    kp1_matched=([ kp1[m.queryIdx] for m in good ]) 
    kp2_matched=([ kp2[m.trainIdx] for m in good ]) 

    kp1_miss_matched=[kp for kp in kp1 if kp not in kp1_matched] 
    kp2_miss_matched=[kp for kp in kp2 if kp not in kp2_matched] 

    # draw only miss matched or not matched keypoints location 
    img1_miss_matched_kp = cv2.drawKeypoints(img1,kp1_miss_matched, None,color=(255,0,0), flags=0) 
    plt.imshow(img1_miss_matched_kp),plt.show() 

    img2_miss_matched_kp = cv2.drawKeypoints(img2,kp2_miss_matched, None,color=(255,0,0), flags=0) 
    plt.imshow(img2_miss_matched_kp),plt.show() 

    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) 

else: 
    print "Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT) 
    matchesMask = None 
2
  1. 畫上兩個圖像的所有不匹配的特徵點:

這個任務是非常簡單的,一旦你知道了Matcher objects的結構產生來自匹配的兩個描述符(matches = bf.match(des1,des2))。有關這個問題的2名匹配器對象的屬性有以下幾種:

  • DMatch.trainIdx:描述符的索引(或從列車圖像關鍵點)在列車描述符
  • DMatch.queryIdx:描述符的索引(或從查詢圖像關鍵點)查詢描述符

然後,知道該信息,並作爲@uz air_syed說,這只是一個simple list operations task


  • 周圍畫不匹配點的邊界:
  • 要做到這一點,我會做這樣的事情:

    • 爲每個非匹配點創建一個白色像素的黑色蒙版
    • 根據的密度的非匹配點的簇,擴張具有大內核(即15 x 15像素)。
    • Erode具有相同內核大小的掩碼。
    • 最後,在掩碼上應用findContours函數以獲取不匹配點的邊界。

    欲瞭解更多信息,你可以看看這個question and its answer

    希望它能讓您走上正軌!