Does anyone have any examples of using OpenCV with python for descriptor extraction?

杀马特。学长 韩版系。学妹 提交于 2019-12-02 22:54:44

Here's an example of some code I've written for extracting SURF features using Python 2.7 and OpenCV 2.4.

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)

This works and returns a set of descriptors. Unfortunately since cv2.SURF() doesn't work in 2.4, you have to go through this tedious process.

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

Using open cv 2.4.3, you can do the following:

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

todofixthis I follow your code and I get this

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

where

l = keypoints

d = descriptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!