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
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef _OPENCV_FLANN_HPP_
00044 #define _OPENCV_FLANN_HPP_
00045
00046 #ifdef __cplusplus
00047
00048 #include "opencv2/flann/flann_base.hpp"
00049
00050 namespace cv
00051 {
00052 namespace flann
00053 {
00054
00055 template <typename T> struct CvType {};
00056 template <> struct CvType<unsigned char> { static int type() { return CV_8U; } };
00057 template <> struct CvType<char> { static int type() { return CV_8S; } };
00058 template <> struct CvType<unsigned short> { static int type() { return CV_16U; } };
00059 template <> struct CvType<short> { static int type() { return CV_16S; } };
00060 template <> struct CvType<int> { static int type() { return CV_32S; } };
00061 template <> struct CvType<float> { static int type() { return CV_32F; } };
00062 template <> struct CvType<double> { static int type() { return CV_64F; } };
00063
00064
00065 using ::cvflann::IndexParams;
00066 using ::cvflann::LinearIndexParams;
00067 using ::cvflann::KDTreeIndexParams;
00068 using ::cvflann::KMeansIndexParams;
00069 using ::cvflann::CompositeIndexParams;
00070 using ::cvflann::AutotunedIndexParams;
00071 using ::cvflann::SavedIndexParams;
00072
00073 using ::cvflann::SearchParams;
00074
00075
00076 template <typename T>
00077 class CV_EXPORTS Index_ {
00078 ::cvflann::Index<T>* nnIndex;
00079
00080 public:
00081 Index_(const Mat& features, const IndexParams& params);
00082
00083 ~Index_();
00084
00085 void knnSearch(const vector<T>& query, vector<int>& indices, vector<float>& dists, int knn, const SearchParams& params);
00086 void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const SearchParams& params);
00087
00088 int radiusSearch(const vector<T>& query, vector<int>& indices, vector<float>& dists, float radius, const SearchParams& params);
00089 int radiusSearch(const Mat& query, Mat& indices, Mat& dists, float radius, const SearchParams& params);
00090
00091 void save(std::string filename) { nnIndex->save(filename); }
00092
00093 int veclen() const { return nnIndex->veclen(); }
00094
00095 int size() const { return nnIndex->size(); }
00096
00097 const IndexParams* getIndexParameters() { return nnIndex->getIndexParameters(); }
00098
00099 };
00100
00101
00102 template <typename T>
00103 Index_<T>::Index_(const Mat& dataset, const IndexParams& params)
00104 {
00105 CV_Assert(dataset.type() == CvType<T>::type());
00106 CV_Assert(dataset.isContinuous());
00107 ::cvflann::Matrix<T> m_dataset((T*)dataset.ptr<T>(0), dataset.rows, dataset.cols);
00108
00109 nnIndex = new ::cvflann::Index<T>(m_dataset, params);
00110 nnIndex->buildIndex();
00111 }
00112
00113 template <typename T>
00114 Index_<T>::~Index_()
00115 {
00116 delete nnIndex;
00117 }
00118
00119 template <typename T>
00120 void Index_<T>::knnSearch(const vector<T>& query, vector<int>& indices, vector<float>& dists, int knn, const SearchParams& searchParams)
00121 {
00122 ::cvflann::Matrix<T> m_query((T*)&query[0], 1, (int)query.size());
00123 ::cvflann::Matrix<int> m_indices(&indices[0], 1, (int)indices.size());
00124 ::cvflann::Matrix<float> m_dists(&dists[0], 1, (int)dists.size());
00125
00126 nnIndex->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
00127 }
00128
00129
00130 template <typename T>
00131 void Index_<T>::knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const SearchParams& searchParams)
00132 {
00133 CV_Assert(queries.type() == CvType<T>::type());
00134 CV_Assert(queries.isContinuous());
00135 ::cvflann::Matrix<T> m_queries((T*)queries.ptr<T>(0), queries.rows, queries.cols);
00136
00137 CV_Assert(indices.type() == CV_32S);
00138 CV_Assert(indices.isContinuous());
00139 ::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
00140
00141 CV_Assert(dists.type() == CV_32F);
00142 CV_Assert(dists.isContinuous());
00143 ::cvflann::Matrix<float> m_dists((float*)dists.ptr<float>(0), dists.rows, dists.cols);
00144
00145 nnIndex->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
00146 }
00147
00148 template <typename T>
00149 int Index_<T>::radiusSearch(const vector<T>& query, vector<int>& indices, vector<float>& dists, float radius, const SearchParams& searchParams)
00150 {
00151 ::cvflann::Matrix<T> m_query((T*)&query[0], 1, (int)query.size());
00152 ::cvflann::Matrix<int> m_indices(&indices[0], 1, (int)indices.size());
00153 ::cvflann::Matrix<float> m_dists(&dists[0], 1, (int)dists.size());
00154
00155 return nnIndex->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
00156 }
00157
00158 template <typename T>
00159 int Index_<T>::radiusSearch(const Mat& query, Mat& indices, Mat& dists, float radius, const SearchParams& searchParams)
00160 {
00161 CV_Assert(query.type() == CvType<T>::type());
00162 CV_Assert(query.isContinuous());
00163 ::cvflann::Matrix<T> m_query((T*)query.ptr<T>(0), query.rows, query.cols);
00164
00165 CV_Assert(indices.type() == CV_32S);
00166 CV_Assert(indices.isContinuous());
00167 ::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
00168
00169 CV_Assert(dists.type() == CV_32F);
00170 CV_Assert(dists.isContinuous());
00171 ::cvflann::Matrix<float> m_dists((float*)dists.ptr<float>(0), dists.rows, dists.cols);
00172
00173 return nnIndex->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
00174 }
00175
00176 typedef Index_<float> Index;
00177
00178 template <typename ELEM_TYPE, typename DIST_TYPE>
00179 int hierarchicalClustering(const Mat& features, Mat& centers, const KMeansIndexParams& params)
00180 {
00181 CV_Assert(features.type() == CvType<ELEM_TYPE>::type());
00182 CV_Assert(features.isContinuous());
00183 ::cvflann::Matrix<ELEM_TYPE> m_features((ELEM_TYPE*)features.ptr<ELEM_TYPE>(0), features.rows, features.cols);
00184
00185 CV_Assert(centers.type() == CvType<DIST_TYPE>::type());
00186 CV_Assert(centers.isContinuous());
00187 ::cvflann::Matrix<DIST_TYPE> m_centers((DIST_TYPE*)centers.ptr<DIST_TYPE>(0), centers.rows, centers.cols);
00188
00189 return ::cvflann::hierarchicalClustering<ELEM_TYPE,DIST_TYPE>(m_features, m_centers, params);
00190 }
00191
00192 } }
00193
00194 #endif // __cplusplus
00195
00196 #endif