knn 在 python 中使用 opencv 进行搜索
knn search with opencv in python
我有这个用 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 提供绑定
可以找到一个例子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
此示例应该足以让您入门。
我有这个用 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 提供绑定可以找到一个例子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
此示例应该足以让您入门。