Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用SURF和FLANN比较图像与图像数据库_Python_Opencv_Flann - Fatal编程技术网

Python 使用SURF和FLANN比较图像与图像数据库

Python 使用SURF和FLANN比较图像与图像数据库,python,opencv,flann,Python,Opencv,Flann,我正在尝试比较单个图像与图像数据库。 我目前正在使用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_

我正在尝试比较单个图像与图像数据库。
我目前正在使用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中是否有类似的内容?

或者我应该使用不同的方法吗?

在OpenCV 3.3.0中,函数名为
knnMatch

示例用法可在此页上基于法兰的匹配器下找到:


编辑: 对不起,我现在意识到我误解了你。
knnSearch
函数现在位于
flann.Index()
下,可按如下方式使用。确保描述符数据库和查询对象都是浮动的

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

看看旧文档,输出是不同的:我只能提供一个描述符,它会找到正确的匹配项和索引。在新的计算中,我每次都要计算第二个。或者我错过了什么?有人认为,我必须搜索图像与数据库与数以千计的图像谢谢!这当然是一个愚蠢的问题,但是如何构建
des_all
numpy数组呢?我试图连接它们,但我得到了一个臭名昭著的
所有输入数组必须具有相同数量的维数
error您的
des
变量的大小是多少,您是如何连接它们的?不管怎样,我做了一些非常愚蠢的事情。谢谢你的帮助@对不起,我也在为同样愚蠢的问题挣扎。你是怎么解决的?