00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef _OPENCV_COMPOSITETREE_H_
00032 #define _OPENCV_COMPOSITETREE_H_
00033
00034 #include "opencv2/flann/general.h"
00035 #include "opencv2/flann/nn_index.h"
00036
00037 namespace cvflann
00038 {
00039
00040
00041 struct CompositeIndexParams : public IndexParams {
00042 CompositeIndexParams(int trees_ = 4, int branching_ = 32, int iterations_ = 11,
00043 flann_centers_init_t centers_init_ = CENTERS_RANDOM, float cb_index_ = 0.2 ) :
00044 IndexParams(COMPOSITE),
00045 trees(trees_),
00046 branching(branching_),
00047 iterations(iterations_),
00048 centers_init(centers_init_),
00049 cb_index(cb_index_) {};
00050
00051 int trees;
00052 int branching;
00053 int iterations;
00054 flann_centers_init_t centers_init;
00055 float cb_index;
00056
00057 flann_algorithm_t getIndexType() const { return algorithm; }
00058
00059 void print() const
00060 {
00061 logger().info("Index type: %d\n",(int)algorithm);
00062 logger().info("Trees: %d\n", trees);
00063 logger().info("Branching: %d\n", branching);
00064 logger().info("Iterations: %d\n", iterations);
00065 logger().info("Centres initialisation: %d\n", centers_init);
00066 logger().info("Cluster boundary weight: %g\n", cb_index);
00067 }
00068 };
00069
00070
00071
00072 template <typename ELEM_TYPE, typename DIST_TYPE = typename DistType<ELEM_TYPE>::type >
00073 class CompositeIndex : public NNIndex<ELEM_TYPE>
00074 {
00075 KMeansIndex<ELEM_TYPE, DIST_TYPE>* kmeans;
00076 KDTreeIndex<ELEM_TYPE, DIST_TYPE>* kdtree;
00077
00078 const Matrix<ELEM_TYPE> dataset;
00079
00080 const IndexParams& index_params;
00081
00082
00083 public:
00084
00085 CompositeIndex(const Matrix<ELEM_TYPE>& inputData, const CompositeIndexParams& params = CompositeIndexParams() ) :
00086 dataset(inputData), index_params(params)
00087 {
00088 KDTreeIndexParams kdtree_params(params.trees);
00089 KMeansIndexParams kmeans_params(params.branching, params.iterations, params.centers_init, params.cb_index);
00090
00091 kdtree = new KDTreeIndex<ELEM_TYPE, DIST_TYPE>(inputData,kdtree_params);
00092 kmeans = new KMeansIndex<ELEM_TYPE, DIST_TYPE>(inputData,kmeans_params);
00093
00094 }
00095
00096 virtual ~CompositeIndex()
00097 {
00098 delete kdtree;
00099 delete kmeans;
00100 }
00101
00102
00103 flann_algorithm_t getType() const
00104 {
00105 return COMPOSITE;
00106 }
00107
00108
00109 size_t size() const
00110 {
00111 return dataset.rows;
00112 }
00113
00114 size_t veclen() const
00115 {
00116 return dataset.cols;
00117 }
00118
00119
00120 int usedMemory() const
00121 {
00122 return kmeans->usedMemory()+kdtree->usedMemory();
00123 }
00124
00125 void buildIndex()
00126 {
00127 logger().info("Building kmeans tree...\n");
00128 kmeans->buildIndex();
00129 logger().info("Building kdtree tree...\n");
00130 kdtree->buildIndex();
00131 }
00132
00133
00134 void saveIndex(FILE* stream)
00135 {
00136 kmeans->saveIndex(stream);
00137 kdtree->saveIndex(stream);
00138 }
00139
00140
00141 void loadIndex(FILE* stream)
00142 {
00143 kmeans->loadIndex(stream);
00144 kdtree->loadIndex(stream);
00145 }
00146
00147 void findNeighbors(ResultSet<ELEM_TYPE>& result, const ELEM_TYPE* vec, const SearchParams& searchParams)
00148 {
00149 kmeans->findNeighbors(result,vec,searchParams);
00150 kdtree->findNeighbors(result,vec,searchParams);
00151 }
00152
00153 const IndexParams* getParameters() const
00154 {
00155 return &index_params;
00156 }
00157
00158
00159 };
00160
00161 }
00162
00163 #endif //_OPENCV_COMPOSITETREE_H_