2017-10-18 3 views
1

Ich habe diesen Beispielcode für opencv in C++:knn Suche mit opencv in Python

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); 

wie kann ich in Python das gleiche tun? Versucht, aber kann nicht kdtree oder KDTreeIndexParams mit cv2 erstellen.

+1

@gsamaras bitte posten dies als Antwort und ich werde es akzeptieren. –

Antwort

1

FLANN ist eine Bibliothek für ANN, die in C++ geschrieben ist und unabhängig von OpenCV ist. Es bietet Bindungen für Python, in pyflann.

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 

Dieses Beispiel genug sein, um, sollten Sie zu erhalten begonnen:

Ein Beispiel kann here finden.