python - knn 在 python 中使用 opencv 进行搜索

标签 python opencv image-processing nearest-neighbor kdtree

我有这个用 C++ 编写的 opencv 示例代码:

flann::KDTreeIndexParams indexParams;
flann::Index kdtree(Mat(cloud2d).reshape(1), indexParams);
vector<float> query;
query.push_back(370); 
query.push_back(464); 
vector<int> indices;
vector<float> dists;
kdtree.knnSearch(query, indices, dists, 3);

我怎样才能在 python 中做同样的事情?已尝试,但无法使用 cv2 创建 kdtree 或 KDTreeIndexParams。

最佳答案

FLANN 是 ANN 的库,用 C++ 编写,独立于 OpenCV。它在 pyflann 中为 Python 提供绑定(bind).

可以找到一个例子here :

from pyflann import *
import numpy as np

dataset = np.array(
    [[1., 1, 1, 2, 3],
     [10, 10, 10, 3, 2],
     [100, 100, 2, 30, 1]
     ])
testset = np.array(
    [[1., 1, 1, 1, 1],
     [90, 90, 10, 10, 1]
     ])
flann = FLANN()
result, dists = flann.nn(
    dataset, testset, 2, algorithm="kmeans", branching=32, iterations=7, checks=16)
print result
print dists

dataset = np.random.rand(10000, 128)
testset = np.random.rand(1000, 128)
flann = FLANN()
result, dists = flann.nn(
    dataset, testset, 5, algorithm="kmeans", branching=32, iterations=7, checks=16)
print result
print dists

这个例子应该足以让你入门。

关于python - knn 在 python 中使用 opencv 进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46804954/

相关文章:

python - 覆盖 Django-Rest-Framework 序列化程序 is_valid 方法

python - 为什么批量导入不起作用,但单独导入定义却有效?

python - QTreeView 与 QStandardItemModel : How to implement an own sort model

Windows 7 中的 Javacv UnsatisfiedLinkError

python-2.7 - 读取、处理和显示 .EXR 格式图像中的像素

python - 使用 OpenCv 检测图像上的文本并计算使用的面积

OpenCV - 如何从轮廓中删除小线段?

python - 不确定如何在 Windows 下运行 Celery 来执行周期性任务

python - 如何使用 Tesseract 对图像进行 OCR

c# - 使用 OpenCV/EmguCV 提高人脸检测性能