2015-04-02 55 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) 
相关问题