2015-04-02 53 views
1

我正在使用opencv-Python如何創建KeyPoint來計算SIFT?

我已經確定了使用cv2.cornerHarris的角點。輸出類型爲「dst」

我需要計算角點的SIFT特徵。輸入到sift.compute()必須是類型爲「關鍵點」

的我無法弄清楚如何使用cv2.KeyPoint()

我該怎麼辦呢?

謝謝:)

回答

0

我想你完全搞錯了。 輸出類型爲「dst」 - >請注意,dst函數返回cv2.cornerHarris是包含在圖像中檢測到的哈里斯角的浮點Mat。

我在python中使用的代碼的一個例子是用於計算圖像中的角點。您可以使用返回數據並將其轉換爲KeyPoints類型。請注意,關鍵點結構定義爲OpenCV KeyPoint Structure,每個關鍵點由Point2f類型的圖像空間2d座標指定。只需將每個檢測到的角點轉換爲Point2f並將其用於篩選功能即可。

#sample code to read image and estimate the harris corner. 
import cv2 
import numpy as np 


def cvComputeHarrisCorner(img):                
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)            
    gray = np.float32(gray)                 
    dst = cv2.cornerHarris(gray,2,3,0.04)             
    dst = cv2.dilate(dst,None)                
    img[dst>0.01*dst.max()]=[0,0,255]              
    return img                    


def main():                     
    img = cv2.imread('img.jpg')         
    cvComputeHarrisCorner(img)                  
    cv2.imshow('dst',img)                 
    if cv2.waitKey(0) & 0xff == 27:               
     cv2.destroyAllWindows()                

if __name__ == "__main__":             
    main()                     

而不是解釋所有你需要在這裏,我會指導你這個OpenCV Python教程,這是非常好的書面和後備箱解釋。請通過他們,你會逐漸瞭解這個概念。

OpenCV Python Tutorials

+0

請包括代碼將每個檢測到的角點轉換爲Point2f – 2016-12-11 16:52:23

0

哈里斯探測器返回DST,它必須與你的圖像相同的形狀。哈里斯在dst認爲角落的地方標記。所以,你必須從dst中提取關鍵點。

def harris(self, img): 
    ''' 
    Harris detector 
    :param img: an color image 
    :return: keypoint, image with feature marked corner 
    ''' 

    gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
    gray_img = np.float32(gray_img) 
    dst = cv2.cornerHarris(gray_img, 2, 3, 0.04) 
    result_img = img.copy() # deep copy image 

    # Threshold for an optimal value, it may vary depending on the image. 
    result_img[dst > 0.01 * dst.max()] = [0, 0, 255] 

    # for each dst larger than threshold, make a keypoint out of it 
    keypoints = np.argwhere(dst > 0.01 * dst.max()) 
    keypoints = [cv2.KeyPoint(x[1], x[0], 1) for x in keypoints] 

    return (keypoints, result_img) 
相關問題