16

我正在尝试使用 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

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

4

4 回答 4

14

这是我为使用 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 中不起作用,您必须经历这个繁琐的过程。

于 2012-05-29T17:41:27.640 回答
6

Here is a simple bit of code I did for uni fairly recently. It captures the image from a camera and displays the detected keypoints on the output image in real-time. I hope it is of use to you.

There is some documentation here.

Code:

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
于 2012-05-29T13:12:48.947 回答
5

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

import cv2
surf = cv2.SURF()
keypoints, descriptors = surf.detectAndCompute(img,None,useProvidedKeypoints = True)
于 2012-11-29T10:02:43.157 回答
2

todofixthis 我遵循你的代码,我得到了这个

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 = 描述符

于 2012-11-24T06:35:01.393 回答