【问题标题】:Compare image vs database of images using SURF and FLANN使用 SURF 和 FLANN 比较图像与图像数据库
【发布时间】:2017-09-29 02:20:32
【问题描述】:

我正在尝试将单个图像与图像数据库进行比较。
我目前正在使用 Python 2.7 和 OpenCV 3.3.0。
经过一番谷歌搜索,我想出了这段代码:

scanned = 'tests/temp_bw.png'
surf = cv2.xfeatures2d.SURF_create(400)
surf.setUpright(True)

img1 = cv2.imread(scanned, 0)
kp1, des1 = surf.detectAndCompute(img1, None)

FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)

flann = cv2.FlannBasedMatcher(index_params, search_params)

for filename in os.listdir('images'):
    img2 = cv2.imread('images/' + filename, 0)
    kp2, des2 = surf.detectAndCompute(img2, None)
    flann.add([des2])

print str(len(flann.getTrainDescriptors()))

print "Training..."
flann.train()

print "Matching..."
indexes, matches = flann.knnSearch(des1, 2, params={})

主要问题是在 OpenCV 3.3.0 中 FlannBasedMatcher 没有方法 knnSearch。我检查了当前的代码文档,在 2.4 中有这样的方法,现在它被删除了。

OpenCV 3.3.0 中有类似的东西吗?
还是我应该使用不同的方法?

【问题讨论】:

    标签: python opencv flann


    【解决方案1】:

    在 OpenCV 3.3.0 中,该函数被称为 knnMatch

    可以在此页面上的基于 FLANN 的 Matcher 下找到示例用法: http://docs.opencv.org/trunk/dc/dc3/tutorial_py_matcher.html


    编辑: 对不起,我现在知道我误解了你。 knnSearch 函数现在在flann.Index() 下,可以如下使用。确保您的描述符数据库和查询对象都是 float32

    flann = cv2.flann.Index()
    print "Training..."
    flann.build(des_all, index_params)
    print "Matching..."
    indexes, matches = flann.knnSearch(des1, 2)
    

    【讨论】:

    • 查看旧文档,输出不同:我只能提供一个描述符,它会找到正确的匹配和索引。在新的中,我每次都必须计算第二个。或者我错过了什么?有人认为我必须搜索图像与包含数千张图像的数据库
    • 谢谢!这肯定是一个愚蠢的问题,但我如何构建des_all numpy 数组?我试图连接它们,但我得到了臭名昭著的all the input array must have same number of dimensions 错误
    • des 变量之一的大小是多少,您打算如何连接它们?
    • 没关系,我正在做一些非常愚蠢的事情。那你来帮忙!
    • @tampe125 对不起,但我正在努力解决同样的愚蠢问题。你是怎么解决的?
    猜你喜欢
    • 1970-01-01
    • 2013-02-22
    • 2012-07-14
    • 2021-06-07
    • 2016-10-12
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多