有没有人有任何使用OpenCV与python进行描述符提取的例子?

Ben*_*Ben 16 python opencv image-processing surf

我正在尝试使用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)
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

有没有人有任何样本代码可以做这种事情,或指向任何提供样本的文档的指针?

Kko*_*kov 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)
Run Code Online (Sandbox Code Playgroud)

这适用于并返回一组描述符.不幸的是,因为cv2.SURF()在2.4中不起作用,所以你必须经历这个繁琐的过程.


Gar*_*ber 6

这是我最近为uni做的一小段代码.它从相机捕获图像并实时显示检测到的输出图像上的关键点.我希望它对你有用.

还有一些文档在这里.

码:

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
Run Code Online (Sandbox Code Playgroud)


liz*_*zie 5

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

import cv2
surf = cv2.SURF()
keypoints, descriptors = surf.detectAndCompute(img,None,useProvidedKeypoints = True)
Run Code Online (Sandbox Code Playgroud)