【问题标题】:How do I create KeyPoints to compute SIFT?如何创建关键点来计算 SIFT?
【发布时间】:2015-06-07 13:42:55
【问题描述】:

我正在使用 OpenCV-Python。

我已经使用cv2.cornerHarris 识别了角点。输出类型为dst

我需要计算角点的 SIFT 特征。 sift.compute() 的输入必须是 KeyPoint 类型。

我不知道如何使用cv2.KeyPoint()

我该怎么做?

【问题讨论】:

    标签: python opencv sift


    【解决方案1】:

    Harris 检测器返回 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)
    

    【讨论】:

    • 谢谢,这绝对是完美的,为我工作!这应该被标记为答案
    【解决方案2】:

    我认为你完全弄错了。 输出的类型是“dst” --> 请注意,函数 cv2.cornerHarris 返回的 dst 是一个浮点 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

    【讨论】:

    • 请包含将每个检测到的角转换为 Point2f 的代码
    猜你喜欢
    • 2012-07-22
    • 2011-10-21
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2015-01-01
    相关资源
    最近更新 更多