【问题标题】:Does anyone have any examples of using OpenCV with python for descriptor extraction?有没有人有任何使用 OpenCV 和 python 进行描述符提取的例子?
【发布时间】:2012-06-03 16:54:55
【问题描述】:

我正在尝试使用 OpenCV 从图像中提取 SURF 描述符。我正在使用 OpenCV 2.4 和 Python 2.7,但我很难找到任何提供有关如何使用这些函数的信息的文档。我已经能够使用以下代码来提取特征,但是我找不到任何明智的方法来提取描述符:

import cv2

img = cv2.imread("im1.jpg")
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

surf = cv2.FeatureDetector_create('SURF')
detector = cv2.GridAdaptedFeatureDetector(surf, 50) # max number of features
fs = detector.detect(img2)

我尝试提取描述符的代码是:

import cv2
img = cv2.imread("im3.jpg")
sd = cv2.FeatureDetector_create("SURF")
surf = cv2.DescriptorExtractor_create("SURF")
keypoints = []
fs = surf.compute(img, keypoints) # returns empty result
sd.detect(img) # segmentation faults

有没有人有任何做这种事情的示例代码,或指向任何提供示例的文档的指针?

【问题讨论】:

    标签: python image-processing opencv surf


    【解决方案1】:

    使用 open cv 2.4.3,您可以执行以下操作:

    import cv2
    surf = cv2.SURF()
    keypoints, descriptors = surf.detectAndCompute(img,None,useProvidedKeypoints = True)
    

    【讨论】:

      【解决方案2】:

      tod​​ofixthis 我遵循你的代码,我得到了这个

      import cv2
      img = cv2.imread("im3.jpg")
      sd = cv2.FeatureDetector_create("SURF")
      surf = cv2.DescriptorExtractor_create("SURF")
      keypoints = sd.detect(img) # segmentation faults
      l,d = surf.compute(img, keypoints) # returns empty result
      

      在哪里

      l = 关键点

      d = 描述符

      【讨论】:

        【解决方案3】:

        这是我为使用 Python 2.7 和 OpenCV 2.4 提取 SURF 特征而编写的一些代码示例。

        im2 = cv2.imread(imgPath)
        im = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)
        surfDetector = cv2.FeatureDetector_create("SURF")
        surfDescriptorExtractor = cv2.DescriptorExtractor_create("SURF")
        keypoints = surfDetector.detect(im)
        (keypoints, descriptors) = surfDescriptorExtractor.compute(im,keypoints)
        

        这有效并返回一组描述符。 不幸的是,由于 cv2.SURF() 在 2.4 中不起作用,您必须经历这个繁琐的过程。

        【讨论】:

        • 成功了!最后!谢谢你。一个小的编辑:您有 features.detect,但未定义功能:我认为您的意思是 surfDetector.detect。这似乎行得通。谢谢!
        • 更正了错误。很高兴我能帮上忙。
        【解决方案4】:

        这是我最近为 uni 编写的一段简单代码。它从摄像头捕获图像并在输出图像上实时显示检测到的关键点。希望对你有用。

        有一些文档here。

        代码:

        import cv2
        
        #Create object to read images from camera 0
        cam = cv2.VideoCapture(0)
        
        #Initialize SURF object
        surf = cv2.SURF(85)
        
        #Set desired radius
        rad = 2
        
        while True:
            #Get image from webcam and convert to greyscale
            ret, img = cam.read()
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        
            #Detect keypoints and descriptors in greyscale image
            keypoints, descriptors = surf.detect(gray, None, False)
        
            #Draw a small red circle with the desired radius
            #at the (x, y) location for each feature found
            for kp in keypoints:
                x = int(kp.pt[0])
                y = int(kp.pt[1])
                cv2.circle(img, (x, y), rad, (0, 0, 255))
        
            #Display colour image with detected features
            cv2.imshow("features", img)
        
            #Sleep infinite loop for ~10ms
            #Exit if user presses <Esc>
            if cv2.waitKey(10) == 27:
                break
        

        【讨论】:

        • 谢谢!奇怪的是,虽然文档显示了 OpenCV v2.4.0 的 cv2.SURF,但当我尝试它时,它不起作用:AttributeError: 'module' object has no attribute 'SURF'
        • @Ben 您是完全使用我的代码,还是将位复制到您的代码中?我确定我上次使用它时它确实有效,但这可能是在版本 2.3.x 上。不幸的是,我正在工作,无法在这里进行测试。
        • 我按原样尝试了您的代码...它在第三行代码(surf = cv2.SURF(85))上犹豫了,但看起来 surf.detect 函数已从版本更改你正在使用...也许我需要将 OpenCV 降级到 2.3.x...
        • 在我链接的文档中,它看起来完全一样: cv2.SURF(_hessianThreshold[, _nOctaves[, _nOctaveLayers[, _extended[, _upright]]]]) 应该返回一个 SURF 对象。 cv2.SURF.detect(img, mask[, useProvidedKeypoints]) 然后返回关键点、描述符。我不确定是什么问题,抱歉。
        • @Ben 我刚刚找到this answer。我的代码很好,最新版本的 OpenCV 存在问题,将在下一个版本中修复。有一个提取该链接中提供的描述符的示例。希望这会有所帮助
        猜你喜欢
        • 2012-02-22
        • 2013-09-05
        • 2010-12-10
        • 2012-07-18
        • 2012-07-06
        • 2017-01-14
        • 1970-01-01
        • 2016-02-23
        • 2011-06-22
        相关资源
        最近更新 更多