有没有人有使用OpenCV和Python进行描述符提取的示例?

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)
我尝试用以下代码提取描述符:

The code I tried for extracting descriptors is:

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个回答

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中无法使用,所以你必须完成这个繁琐的过程。


成功了!终于!谢谢。有一个小修改:你写的是features.detect,但是features没有定义:我想你的意思是surfDetector.detect。看起来可以了。谢谢! - Ben
纠正了错误。很高兴能帮忙。 - Kkov

6

这是我最近为大学写的一段简单的代码。它可以从相机中捕获图像,并实时在输出图像上显示检测到的关键点。希望对你有用。

这里有一些文档(链接)

代码:

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'对象没有属性'SURF'。 - Ben
@Ben 你是完全使用了我的代码,还是将其中的一些部分复制到了你的代码中?我确定当我上次使用它时它是有效的,但那可能是在2.3.x版本上。不幸的是,我现在正在工作,无法在这里测试它。 - Pep_8_Guardiola
我按照您的代码尝试了一下...但是第三行代码(surf = cv2.SURF(85))出现了问题,而且看起来surf.detect函数与您使用的版本不同...也许我需要将OpenCV降级到2.3.x... - Ben
在我提供的文档中,cv2.SURF(_hessianThreshold[, _nOctaves[, _nOctaveLayers[, _extended[, _upright]]]])应该返回一个SURF对象,而cv2.SURF.detect(img, mask[, useProvidedKeypoints])则会返回关键点和描述符。很抱歉,我不确定问题出在哪里。 - Pep_8_Guardiola
@Ben 我刚刚找到了这个答案。我的代码没问题,最新版本的OpenCV存在问题,将在下一个版本中修复。该链接提供了提取描述符的示例。希望这可以帮到你。 - Pep_8_Guardiola
谢谢您的帮助!不幸的是,看起来v2.4就是不支持这些东西,或者至少如果它支持的话,那么它是未记录的。我尝试了提取描述符的代码,但是我不知道如何使用它们。我尝试按照您的代码进行操作,但现在它需要两个参数而不是三个,当我尝试传入两个参数时,它会导致段错误:surf_detector.detect(im, None)。 - Ben

5

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

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接