python - 使用 opencv 2.4.6.1 和 Python 的 SURF 描述符

标签 python opencv surf

我正在尝试使用 Python 和 opencv 进行 SURF 特征检测。我在 stackoverflow 上找到了这些示例 OpenCV 2.4.1 - computing SURF descriptors in Python ,但不幸的是,他们没有使用最新版本的 opencv,即 2.4.6.1。 cv2.SURF.detect() 命令必须已经更改,因为它现在只允许两个参数:

cv2.SURF.detect(image[, mask]) → keypoints¶

所以我只能获取关键点,但我如何获取描述符?没有找到解决方案。希望你能在这里帮助我。谢谢

最佳答案

根据 Abid Rahman K 在评论中发布的教程,我修改了这个示例代码 OpenCV 2.4.1 - computing SURF descriptors in Python所以它与 opencv 2.4.6.1 一起工作

获取SURF关键点和描述符的函数修改为:

cv2.SURF.detectAndCompute(image, mask[, descriptors[, useProvidedKeypoints]]) → keypoints, descriptors

所以这是链接的修改示例:

import cv2
import numpy

opencv_haystack =cv2.imread('haystack.jpg')
opencv_needle =cv2.imread('needle.jpg')

ngrey = cv2.cvtColor(opencv_needle, cv2.COLOR_BGR2GRAY)
hgrey = cv2.cvtColor(opencv_haystack, cv2.COLOR_BGR2GRAY)

# build feature detector and descriptor extractor
hessian_threshold = 5000
detector = cv2.SURF(hessian_threshold)
hkeypoints,hdescriptors = detector.detectAndCompute(hgrey,None)
nkeypoints,ndescriptors = detector.detectAndCompute(ngrey,None)

# extract vectors of size 64 from raw descriptors numpy arrays
rowsize = len(hdescriptors) / len(hkeypoints)
if rowsize > 1:
    hrows = numpy.array(hdescriptors, dtype = numpy.float32).reshape((-1, rowsize))
    nrows = numpy.array(ndescriptors, dtype = numpy.float32).reshape((-1, rowsize))
    #print hrows.shape, nrows.shape
else:
    hrows = numpy.array(hdescriptors, dtype = numpy.float32)
    nrows = numpy.array(ndescriptors, dtype = numpy.float32)
    rowsize = len(hrows[0])

# kNN training - learn mapping from hrow to hkeypoints index
samples = hrows
responses = numpy.arange(len(hkeypoints), dtype = numpy.float32)
#print len(samples), len(responses)
knn = cv2.KNearest()
knn.train(samples,responses)

# retrieve index and value through enumeration
for i, descriptor in enumerate(nrows):
    descriptor = numpy.array(descriptor, dtype = numpy.float32).reshape((1, rowsize))
    #print i, descriptor.shape, samples[0].shape
    retval, results, neigh_resp, dists = knn.find_nearest(descriptor, 1)
    res, dist =  int(results[0][0]), dists[0][0]
    #print res, dist

    if dist < 0.1:
        # draw matched keypoints in red color
        color = (0, 0, 255)
    else:
        # draw unmatched in blue color
        color = (255, 0, 0)
    # draw matched key points on haystack image
    x,y = hkeypoints[res].pt
    center = (int(x),int(y))
    cv2.circle(opencv_haystack,center,2,color,-1)
    # draw matched key points on needle image
    x,y = nkeypoints[i].pt
    center = (int(x),int(y))
    cv2.circle(opencv_needle,center,2,color,-1)

cv2.imshow('haystack',opencv_haystack)
cv2.imshow('needle',opencv_needle)
cv2.waitKey(0)
cv2.destroyAllWindows()

关于python - 使用 opencv 2.4.6.1 和 Python 的 SURF 描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18578855/

相关文章:

opencv - 关于使用 SURF 的对象识别和特征的困惑

python - 检查 Python 条件循环中的非对角条目

python - 从 Seaborn Boxplot 中提取异常值

c++ - 无法通过 opencv3.2 加载 squeezeNet 1.0 或 1.1

c++ - 如何使用 OpenCV 计算图像中所有点的 SURF 点?

opencv - 从特征关键点在 OpenCV 中手动进行成对匹配

python - Numba @guvectorize([(float64[ :], int64)], '(n),()->(n)' ) IndexError: 列表索引超出范围

python - 使用click创建复杂的命令行界面

opencv - 将libsvm与OpenCV结合使用可预测类

macos - Qt5.1/Qt5.2 + Mac OS 10.9 (Mavericks) + XCode 5.0.2,架构 x86_64 的 undefined symbol