00001 /*M/////////////////////////////////////////////////////////////////////////////////////// 00002 // 00003 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 00004 // 00005 // By downloading, copying, installing or using the software you agree to this license. 00006 // If you do not agree to this license, do not download, install, 00007 // copy or use the software. 00008 // 00009 // 00010 // License Agreement 00011 // For Open Source Computer Vision Library 00012 // 00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. 00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved. 00015 // Third party copyrights are property of their respective owners. 00016 // 00017 // Redistribution and use in source and binary forms, with or without modification, 00018 // are permitted provided that the following conditions are met: 00019 // 00020 // * Redistribution's of source code must retain the above copyright notice, 00021 // this list of conditions and the following disclaimer. 00022 // 00023 // * Redistribution's in binary form must reproduce the above copyright notice, 00024 // this list of conditions and the following disclaimer in the documentation 00025 // and/or other materials provided with the distribution. 00026 // 00027 // * The name of the copyright holders may not be used to endorse or promote products 00028 // derived from this software without specific prior written permission. 00029 // 00030 // This software is provided by the copyright holders and contributors "as is" and 00031 // any express or implied warranties, including, but not limited to, the implied 00032 // warranties of merchantability and fitness for a particular purpose are disclaimed. 00033 // In no event shall the Intel Corporation or contributors be liable for any direct, 00034 // indirect, incidental, special, exemplary, or consequential damages 00035 // (including, but not limited to, procurement of substitute goods or services; 00036 // loss of use, data, or profits; or business interruption) however caused 00037 // and on any theory of liability, whether in contract, strict liability, 00038 // or tort (including negligence or otherwise) arising in any way out of 00039 // the use of this software, even if advised of the possibility of such damage. 00040 // 00041 //M*/ 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 } } // namespace cv::flann 00193 00194 #endif // __cplusplus 00195 00196 #endif