2013-11-25 17 views
1

由於Index_是flann庫中不推薦使用的類,因此我試圖使用GenericIndex類,這是一個模板類。我不知道如何爲這個類創建一個對象。如何在opencv中使用GenericIndex類

在flann.hpp類定義爲如下:

template <typename Distance> 
class GenericIndex 
{ 
public: 
     typedef typename Distance::ElementType ElementType; 
     typedef typename Distance::ResultType DistanceType; 

     GenericIndex(const Mat& features, const ::cvflann::IndexParams& params, Distance distance = Distance()); 

     ~GenericIndex(); 

     void knnSearch(const vector<ElementType>& query, vector<int>& indices, 
         vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& params); 
     void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& params); 

     int radiusSearch(const vector<ElementType>& query, vector<int>& indices, 
         vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& params); 
     int radiusSearch(const Mat& query, Mat& indices, Mat& dists, 
         DistanceType radius, const ::cvflann::SearchParams& params); 

     void save(std::string filename) { nnIndex->save(filename); } 

     int veclen() const { return nnIndex->veclen(); } 

     int size() const { return nnIndex->size(); } 

     ::cvflann::IndexParams getParameters() { return nnIndex->getParameters(); } 

     FLANN_DEPRECATED const ::cvflann::IndexParams* getIndexParameters() { return nnIndex->getIndexParameters(); } 

private: 
     ::cvflann::Index<Distance>* nnIndex; 
}; 

回答

1

要使用GenericIndex作爲與Index_做了你必須在模板實例指定的,而不是要素類型的距離函子。在flann/dist.h中定義了許多距離函子:L1,L2,MinkowskyDistance等。這些自身是模板,通過要素類型進行參數化。

因此,凡與Index_你聲明:

cv::flann::Index_<int> index; 

隨着GenericIndex你(做示例):

cv::flann::GenericIndex<cvflann::L2<int> > index; 

哪裏cvflann::L2是實現L2 norm基於距離度量函子。請注意,仿函數的名稱空間是cvflann,而不是cv::flannGenericIndex(爲什麼開發人員決定讓這兩個相似但不太完全的名稱空間不在我身邊)。

Index_GenericIndex有其他非常相似的接口,所以你可能不需要改變任何東西。

+0

嗨,你能給我一些關於如何構建'cv :: flann :: GenericIndex'的指導,我發現opencv doc很難理解。非常感謝你。 'cv :: flann :: GenericIndex > index(feature,params,Distance)',我知道特徵是cv :: Mat,但我對如何構造參數和距離感到困惑。真的需要一些幫助。 – ted930511

+0

正如[here]所述(https://docs.opencv.org/trunk/db/d18/classcv_1_1flann_1_1GenericIndex.html#a8fff14185f9f3d2f2311b528f65b146c),對於'params'參數,您必須創建一個'IndexParams'子類的實例,例如'KDTreeIndexParams','CompositeIndexParams'等。不同的參數子類將產生具有不同搜索策略的索引。 'distance'參數可以省略,因爲它被賦予一個默認值。 – xperroni

相關問題