2010-10-13 03:47:50 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
|
|
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
2016-09-30 17:46:29 +08:00
|
|
|
#ifndef OPENCV_FLANN_HPP
|
|
|
|
#define OPENCV_FLANN_HPP
|
2010-10-13 03:47:50 +08:00
|
|
|
|
2013-03-13 20:22:44 +08:00
|
|
|
#include "opencv2/core.hpp"
|
2011-07-14 07:04:39 +08:00
|
|
|
#include "opencv2/flann/miniflann.hpp"
|
2013-03-26 18:58:09 +08:00
|
|
|
#include "opencv2/flann/flann_base.hpp"
|
2011-07-14 07:04:39 +08:00
|
|
|
|
2014-11-20 00:08:03 +08:00
|
|
|
/**
|
|
|
|
@defgroup flann Clustering and Search in Multi-Dimensional Spaces
|
|
|
|
|
|
|
|
This section documents OpenCV's interface to the FLANN library. FLANN (Fast Library for Approximate
|
|
|
|
Nearest Neighbors) is a library that contains a collection of algorithms optimized for fast nearest
|
|
|
|
neighbor search in large datasets and for high dimensional features. More information about FLANN
|
|
|
|
can be found in @cite Muja2009 .
|
|
|
|
*/
|
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
namespace cvflann
|
|
|
|
{
|
|
|
|
CV_EXPORTS flann_distance_t flann_distance_type();
|
2017-01-24 18:37:41 +08:00
|
|
|
CV_DEPRECATED CV_EXPORTS void set_distance_type(flann_distance_t distance_type, int order);
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
|
|
|
|
2010-10-13 03:47:50 +08:00
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
namespace flann
|
|
|
|
{
|
|
|
|
|
2014-11-20 00:08:03 +08:00
|
|
|
|
|
|
|
//! @addtogroup flann
|
|
|
|
//! @{
|
|
|
|
|
2010-10-13 03:47:50 +08:00
|
|
|
template <typename T> struct CvType {};
|
|
|
|
template <> struct CvType<unsigned char> { static int type() { return CV_8U; } };
|
|
|
|
template <> struct CvType<char> { static int type() { return CV_8S; } };
|
|
|
|
template <> struct CvType<unsigned short> { static int type() { return CV_16U; } };
|
|
|
|
template <> struct CvType<short> { static int type() { return CV_16S; } };
|
|
|
|
template <> struct CvType<int> { static int type() { return CV_32S; } };
|
|
|
|
template <> struct CvType<float> { static int type() { return CV_32F; } };
|
|
|
|
template <> struct CvType<double> { static int type() { return CV_64F; } };
|
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
|
|
|
|
// bring the flann parameters into this namespace
|
|
|
|
using ::cvflann::get_param;
|
|
|
|
using ::cvflann::print_params;
|
|
|
|
|
|
|
|
// bring the flann distances into this namespace
|
|
|
|
using ::cvflann::L2_Simple;
|
|
|
|
using ::cvflann::L2;
|
|
|
|
using ::cvflann::L1;
|
|
|
|
using ::cvflann::MinkowskiDistance;
|
|
|
|
using ::cvflann::MaxDistance;
|
|
|
|
using ::cvflann::HammingLUT;
|
|
|
|
using ::cvflann::Hamming;
|
|
|
|
using ::cvflann::Hamming2;
|
|
|
|
using ::cvflann::HistIntersectionDistance;
|
|
|
|
using ::cvflann::HellingerDistance;
|
|
|
|
using ::cvflann::ChiSquareDistance;
|
|
|
|
using ::cvflann::KL_Divergence;
|
|
|
|
|
|
|
|
|
2014-11-20 00:08:03 +08:00
|
|
|
/** @brief The FLANN nearest neighbor index class. This class is templated with the type of elements for which
|
|
|
|
the index is built.
|
|
|
|
*/
|
2011-07-14 07:04:39 +08:00
|
|
|
template <typename Distance>
|
2012-10-17 15:12:04 +08:00
|
|
|
class GenericIndex
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename Distance::ElementType ElementType;
|
|
|
|
typedef typename Distance::ResultType DistanceType;
|
|
|
|
|
2014-11-20 00:08:03 +08:00
|
|
|
/** @brief Constructs a nearest neighbor search index for a given dataset.
|
|
|
|
|
|
|
|
@param features Matrix of containing the features(points) to index. The size of the matrix is
|
2014-11-21 16:28:14 +08:00
|
|
|
num_features x feature_dimensionality and the data type of the elements in the matrix must
|
2014-11-20 00:08:03 +08:00
|
|
|
coincide with the type of the index.
|
|
|
|
@param params Structure containing the index parameters. The type of index that will be
|
|
|
|
constructed depends on the type of this parameter. See the description.
|
|
|
|
@param distance
|
|
|
|
|
|
|
|
The method constructs a fast search structure from a set of features using the specified algorithm
|
|
|
|
with specified parameters, as defined by params. params is a reference to one of the following class
|
|
|
|
IndexParams descendants:
|
|
|
|
|
|
|
|
- **LinearIndexParams** When passing an object of this type, the index will perform a linear,
|
|
|
|
brute-force search. :
|
|
|
|
@code
|
|
|
|
struct LinearIndexParams : public IndexParams
|
|
|
|
{
|
|
|
|
};
|
|
|
|
@endcode
|
|
|
|
- **KDTreeIndexParams** When passing an object of this type the index constructed will consist of
|
|
|
|
a set of randomized kd-trees which will be searched in parallel. :
|
|
|
|
@code
|
|
|
|
struct KDTreeIndexParams : public IndexParams
|
|
|
|
{
|
|
|
|
KDTreeIndexParams( int trees = 4 );
|
|
|
|
};
|
|
|
|
@endcode
|
|
|
|
- **KMeansIndexParams** When passing an object of this type the index constructed will be a
|
|
|
|
hierarchical k-means tree. :
|
|
|
|
@code
|
|
|
|
struct KMeansIndexParams : public IndexParams
|
|
|
|
{
|
|
|
|
KMeansIndexParams(
|
|
|
|
int branching = 32,
|
|
|
|
int iterations = 11,
|
|
|
|
flann_centers_init_t centers_init = CENTERS_RANDOM,
|
|
|
|
float cb_index = 0.2 );
|
|
|
|
};
|
|
|
|
@endcode
|
|
|
|
- **CompositeIndexParams** When using a parameters object of this type the index created
|
|
|
|
combines the randomized kd-trees and the hierarchical k-means tree. :
|
|
|
|
@code
|
|
|
|
struct CompositeIndexParams : public IndexParams
|
|
|
|
{
|
|
|
|
CompositeIndexParams(
|
|
|
|
int trees = 4,
|
|
|
|
int branching = 32,
|
|
|
|
int iterations = 11,
|
|
|
|
flann_centers_init_t centers_init = CENTERS_RANDOM,
|
|
|
|
float cb_index = 0.2 );
|
|
|
|
};
|
|
|
|
@endcode
|
|
|
|
- **LshIndexParams** When using a parameters object of this type the index created uses
|
|
|
|
multi-probe LSH (by Multi-Probe LSH: Efficient Indexing for High-Dimensional Similarity Search
|
|
|
|
by Qin Lv, William Josephson, Zhe Wang, Moses Charikar, Kai Li., Proceedings of the 33rd
|
|
|
|
International Conference on Very Large Data Bases (VLDB). Vienna, Austria. September 2007) :
|
|
|
|
@code
|
|
|
|
struct LshIndexParams : public IndexParams
|
|
|
|
{
|
|
|
|
LshIndexParams(
|
|
|
|
unsigned int table_number,
|
|
|
|
unsigned int key_size,
|
|
|
|
unsigned int multi_probe_level );
|
|
|
|
};
|
|
|
|
@endcode
|
|
|
|
- **AutotunedIndexParams** When passing an object of this type the index created is
|
|
|
|
automatically tuned to offer the best performance, by choosing the optimal index type
|
|
|
|
(randomized kd-trees, hierarchical kmeans, linear) and parameters for the dataset provided. :
|
|
|
|
@code
|
|
|
|
struct AutotunedIndexParams : public IndexParams
|
|
|
|
{
|
|
|
|
AutotunedIndexParams(
|
|
|
|
float target_precision = 0.9,
|
|
|
|
float build_weight = 0.01,
|
|
|
|
float memory_weight = 0,
|
|
|
|
float sample_fraction = 0.1 );
|
|
|
|
};
|
|
|
|
@endcode
|
|
|
|
- **SavedIndexParams** This object type is used for loading a previously saved index from the
|
|
|
|
disk. :
|
|
|
|
@code
|
|
|
|
struct SavedIndexParams : public IndexParams
|
|
|
|
{
|
|
|
|
SavedIndexParams( String filename );
|
|
|
|
};
|
|
|
|
@endcode
|
|
|
|
*/
|
2011-07-26 07:58:54 +08:00
|
|
|
GenericIndex(const Mat& features, const ::cvflann::IndexParams& params, Distance distance = Distance());
|
2011-07-14 07:04:39 +08:00
|
|
|
|
|
|
|
~GenericIndex();
|
|
|
|
|
2014-11-20 00:08:03 +08:00
|
|
|
/** @brief Performs a K-nearest neighbor search for a given query point using the index.
|
|
|
|
|
|
|
|
@param query The query point
|
|
|
|
@param indices Vector that will contain the indices of the K-nearest neighbors found. It must have
|
|
|
|
at least knn size.
|
|
|
|
@param dists Vector that will contain the distances to the K-nearest neighbors found. It must have
|
|
|
|
at least knn size.
|
|
|
|
@param knn Number of nearest neighbors to search for.
|
|
|
|
@param params SearchParams
|
|
|
|
*/
|
2013-02-25 00:14:01 +08:00
|
|
|
void knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices,
|
|
|
|
std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& params);
|
2011-07-26 07:58:54 +08:00
|
|
|
void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& params);
|
2011-07-14 07:04:39 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
int radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices,
|
|
|
|
std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& params);
|
2012-10-17 15:12:04 +08:00
|
|
|
int radiusSearch(const Mat& query, Mat& indices, Mat& dists,
|
2011-07-26 07:58:54 +08:00
|
|
|
DistanceType radius, const ::cvflann::SearchParams& params);
|
2011-07-14 07:04:39 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
void save(String filename) { nnIndex->save(filename); }
|
2011-07-14 07:04:39 +08:00
|
|
|
|
|
|
|
int veclen() const { return nnIndex->veclen(); }
|
|
|
|
|
|
|
|
int size() const { return nnIndex->size(); }
|
|
|
|
|
2011-07-26 07:58:54 +08:00
|
|
|
::cvflann::IndexParams getParameters() { return nnIndex->getParameters(); }
|
2011-07-14 07:04:39 +08:00
|
|
|
|
2017-01-24 18:37:41 +08:00
|
|
|
CV_DEPRECATED const ::cvflann::IndexParams* getIndexParameters() { return nnIndex->getIndexParameters(); }
|
2011-07-14 07:04:39 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
::cvflann::Index<Distance>* nnIndex;
|
|
|
|
};
|
|
|
|
|
2014-11-20 00:08:03 +08:00
|
|
|
//! @cond IGNORED
|
2011-07-14 07:04:39 +08:00
|
|
|
|
|
|
|
#define FLANN_DISTANCE_CHECK \
|
2011-07-19 06:01:42 +08:00
|
|
|
if ( ::cvflann::flann_distance_type() != cvflann::FLANN_DIST_L2) { \
|
2011-07-14 07:04:39 +08:00
|
|
|
printf("[WARNING] You are using cv::flann::Index (or cv::flann::GenericIndex) and have also changed "\
|
|
|
|
"the distance using cvflann::set_distance_type. This is no longer working as expected "\
|
|
|
|
"(cv::flann::Index always uses L2). You should create the index templated on the distance, "\
|
|
|
|
"for example for L1 distance use: GenericIndex< L1<float> > \n"); \
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-06-20 07:46:23 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
template <typename Distance>
|
2011-07-26 07:58:54 +08:00
|
|
|
GenericIndex<Distance>::GenericIndex(const Mat& dataset, const ::cvflann::IndexParams& params, Distance distance)
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
CV_Assert(dataset.type() == CvType<ElementType>::type());
|
|
|
|
CV_Assert(dataset.isContinuous());
|
|
|
|
::cvflann::Matrix<ElementType> m_dataset((ElementType*)dataset.ptr<ElementType>(0), dataset.rows, dataset.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
nnIndex = new ::cvflann::Index<Distance>(m_dataset, params, distance);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
FLANN_DISTANCE_CHECK
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
nnIndex->buildIndex();
|
|
|
|
}
|
2011-06-20 07:46:23 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
template <typename Distance>
|
|
|
|
GenericIndex<Distance>::~GenericIndex()
|
|
|
|
{
|
|
|
|
delete nnIndex;
|
|
|
|
}
|
2011-06-20 07:46:23 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
template <typename Distance>
|
2013-02-25 00:14:01 +08:00
|
|
|
void GenericIndex<Distance>::knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& searchParams)
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
|
|
|
|
::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
|
|
|
|
::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
|
|
|
|
|
|
|
|
FLANN_DISTANCE_CHECK
|
|
|
|
|
|
|
|
nnIndex->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Distance>
|
2011-07-26 07:58:54 +08:00
|
|
|
void GenericIndex<Distance>::knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& searchParams)
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
CV_Assert(queries.type() == CvType<ElementType>::type());
|
|
|
|
CV_Assert(queries.isContinuous());
|
|
|
|
::cvflann::Matrix<ElementType> m_queries((ElementType*)queries.ptr<ElementType>(0), queries.rows, queries.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
CV_Assert(indices.type() == CV_32S);
|
|
|
|
CV_Assert(indices.isContinuous());
|
|
|
|
::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
CV_Assert(dists.type() == CvType<DistanceType>::type());
|
|
|
|
CV_Assert(dists.isContinuous());
|
|
|
|
::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
|
|
|
|
|
|
|
|
FLANN_DISTANCE_CHECK
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
nnIndex->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Distance>
|
2013-02-25 00:14:01 +08:00
|
|
|
int GenericIndex<Distance>::radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
|
|
|
|
::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
|
|
|
|
::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
|
|
|
|
|
|
|
|
FLANN_DISTANCE_CHECK
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
return nnIndex->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
|
|
|
|
}
|
2011-06-20 07:46:23 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
template <typename Distance>
|
2011-07-26 07:58:54 +08:00
|
|
|
int GenericIndex<Distance>::radiusSearch(const Mat& query, Mat& indices, Mat& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
CV_Assert(query.type() == CvType<ElementType>::type());
|
|
|
|
CV_Assert(query.isContinuous());
|
|
|
|
::cvflann::Matrix<ElementType> m_query((ElementType*)query.ptr<ElementType>(0), query.rows, query.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
CV_Assert(indices.type() == CV_32S);
|
|
|
|
CV_Assert(indices.isContinuous());
|
|
|
|
::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
CV_Assert(dists.type() == CvType<DistanceType>::type());
|
|
|
|
CV_Assert(dists.isContinuous());
|
|
|
|
::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
FLANN_DISTANCE_CHECK
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
return nnIndex->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
|
|
|
|
}
|
|
|
|
|
2014-11-20 00:08:03 +08:00
|
|
|
//! @endcond
|
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
/**
|
|
|
|
* @deprecated Use GenericIndex class instead
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-09-01 04:12:52 +08:00
|
|
|
class Index_
|
|
|
|
{
|
2011-06-20 17:20:17 +08:00
|
|
|
public:
|
2016-09-01 04:12:52 +08:00
|
|
|
typedef typename L2<T>::ElementType ElementType;
|
|
|
|
typedef typename L2<T>::ResultType DistanceType;
|
2010-10-13 03:47:50 +08:00
|
|
|
|
2017-01-24 18:37:41 +08:00
|
|
|
CV_DEPRECATED Index_(const Mat& dataset, const ::cvflann::IndexParams& params)
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2016-09-01 04:12:52 +08:00
|
|
|
printf("[WARNING] The cv::flann::Index_<T> class is deperecated, use cv::flann::GenericIndex<Distance> instead\n");
|
2010-10-13 03:47:50 +08:00
|
|
|
|
2016-09-01 04:12:52 +08:00
|
|
|
CV_Assert(dataset.type() == CvType<ElementType>::type());
|
|
|
|
CV_Assert(dataset.isContinuous());
|
|
|
|
::cvflann::Matrix<ElementType> m_dataset((ElementType*)dataset.ptr<ElementType>(0), dataset.rows, dataset.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2016-09-01 04:12:52 +08:00
|
|
|
if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L2 ) {
|
|
|
|
nnIndex_L1 = NULL;
|
|
|
|
nnIndex_L2 = new ::cvflann::Index< L2<ElementType> >(m_dataset, params);
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
2016-09-01 04:12:52 +08:00
|
|
|
else if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L1 ) {
|
|
|
|
nnIndex_L1 = new ::cvflann::Index< L1<ElementType> >(m_dataset, params);
|
|
|
|
nnIndex_L2 = NULL;
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
2016-09-01 04:12:52 +08:00
|
|
|
else {
|
|
|
|
printf("[ERROR] cv::flann::Index_<T> only provides backwards compatibility for the L1 and L2 distances. "
|
|
|
|
"For other distance types you must use cv::flann::GenericIndex<Distance>\n");
|
|
|
|
CV_Assert(0);
|
|
|
|
}
|
|
|
|
if (nnIndex_L1) nnIndex_L1->buildIndex();
|
|
|
|
if (nnIndex_L2) nnIndex_L2->buildIndex();
|
|
|
|
}
|
2017-01-24 18:37:41 +08:00
|
|
|
CV_DEPRECATED ~Index_()
|
2016-09-01 04:12:52 +08:00
|
|
|
{
|
|
|
|
if (nnIndex_L1) delete nnIndex_L1;
|
|
|
|
if (nnIndex_L2) delete nnIndex_L2;
|
|
|
|
}
|
2010-10-13 03:47:50 +08:00
|
|
|
|
2017-01-24 18:37:41 +08:00
|
|
|
CV_DEPRECATED void knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& searchParams)
|
2016-09-01 04:12:52 +08:00
|
|
|
{
|
|
|
|
::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
|
|
|
|
::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
|
|
|
|
::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
|
2010-10-13 03:47:50 +08:00
|
|
|
|
2016-09-01 04:12:52 +08:00
|
|
|
if (nnIndex_L1) nnIndex_L1->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
|
|
|
|
if (nnIndex_L2) nnIndex_L2->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
|
|
|
|
}
|
2017-01-24 18:37:41 +08:00
|
|
|
CV_DEPRECATED void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& searchParams)
|
2016-09-01 04:12:52 +08:00
|
|
|
{
|
|
|
|
CV_Assert(queries.type() == CvType<ElementType>::type());
|
|
|
|
CV_Assert(queries.isContinuous());
|
|
|
|
::cvflann::Matrix<ElementType> m_queries((ElementType*)queries.ptr<ElementType>(0), queries.rows, queries.cols);
|
2014-11-20 00:08:03 +08:00
|
|
|
|
2016-09-01 04:12:52 +08:00
|
|
|
CV_Assert(indices.type() == CV_32S);
|
|
|
|
CV_Assert(indices.isContinuous());
|
|
|
|
::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2016-09-01 04:12:52 +08:00
|
|
|
CV_Assert(dists.type() == CvType<DistanceType>::type());
|
|
|
|
CV_Assert(dists.isContinuous());
|
|
|
|
::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2016-09-01 04:12:52 +08:00
|
|
|
if (nnIndex_L1) nnIndex_L1->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
|
|
|
|
if (nnIndex_L2) nnIndex_L2->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
2010-10-13 03:47:50 +08:00
|
|
|
|
2017-01-24 18:37:41 +08:00
|
|
|
CV_DEPRECATED int radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
|
2016-09-01 04:12:52 +08:00
|
|
|
{
|
|
|
|
::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
|
|
|
|
::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
|
|
|
|
::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2016-09-01 04:12:52 +08:00
|
|
|
if (nnIndex_L1) return nnIndex_L1->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
|
|
|
|
if (nnIndex_L2) return nnIndex_L2->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
|
|
|
|
}
|
2010-10-13 03:47:50 +08:00
|
|
|
|
2017-01-24 18:37:41 +08:00
|
|
|
CV_DEPRECATED int radiusSearch(const Mat& query, Mat& indices, Mat& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
|
2016-09-01 04:12:52 +08:00
|
|
|
{
|
|
|
|
CV_Assert(query.type() == CvType<ElementType>::type());
|
|
|
|
CV_Assert(query.isContinuous());
|
|
|
|
::cvflann::Matrix<ElementType> m_query((ElementType*)query.ptr<ElementType>(0), query.rows, query.cols);
|
2010-10-13 03:47:50 +08:00
|
|
|
|
2016-09-01 04:12:52 +08:00
|
|
|
CV_Assert(indices.type() == CV_32S);
|
|
|
|
CV_Assert(indices.isContinuous());
|
|
|
|
::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2016-09-01 04:12:52 +08:00
|
|
|
CV_Assert(dists.type() == CvType<DistanceType>::type());
|
|
|
|
CV_Assert(dists.isContinuous());
|
|
|
|
::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2016-09-01 04:12:52 +08:00
|
|
|
if (nnIndex_L1) return nnIndex_L1->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
|
|
|
|
if (nnIndex_L2) return nnIndex_L2->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
|
|
|
|
}
|
2011-07-14 07:04:39 +08:00
|
|
|
|
2017-01-24 18:37:41 +08:00
|
|
|
CV_DEPRECATED void save(String filename)
|
2016-09-01 04:12:52 +08:00
|
|
|
{
|
|
|
|
if (nnIndex_L1) nnIndex_L1->save(filename);
|
|
|
|
if (nnIndex_L2) nnIndex_L2->save(filename);
|
|
|
|
}
|
2010-10-13 03:47:50 +08:00
|
|
|
|
2017-01-24 18:37:41 +08:00
|
|
|
CV_DEPRECATED int veclen() const
|
2016-09-01 04:12:52 +08:00
|
|
|
{
|
|
|
|
if (nnIndex_L1) return nnIndex_L1->veclen();
|
|
|
|
if (nnIndex_L2) return nnIndex_L2->veclen();
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2017-01-24 18:37:41 +08:00
|
|
|
CV_DEPRECATED int size() const
|
2016-09-01 04:12:52 +08:00
|
|
|
{
|
|
|
|
if (nnIndex_L1) return nnIndex_L1->size();
|
|
|
|
if (nnIndex_L2) return nnIndex_L2->size();
|
|
|
|
}
|
2010-10-13 03:47:50 +08:00
|
|
|
|
2017-01-24 18:37:41 +08:00
|
|
|
CV_DEPRECATED ::cvflann::IndexParams getParameters()
|
2016-09-01 04:12:52 +08:00
|
|
|
{
|
|
|
|
if (nnIndex_L1) return nnIndex_L1->getParameters();
|
|
|
|
if (nnIndex_L2) return nnIndex_L2->getParameters();
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2016-09-01 04:12:52 +08:00
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2017-01-24 18:37:41 +08:00
|
|
|
CV_DEPRECATED const ::cvflann::IndexParams* getIndexParameters()
|
2016-09-01 04:12:52 +08:00
|
|
|
{
|
|
|
|
if (nnIndex_L1) return nnIndex_L1->getIndexParameters();
|
|
|
|
if (nnIndex_L2) return nnIndex_L2->getIndexParameters();
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2016-09-01 04:12:52 +08:00
|
|
|
private:
|
|
|
|
// providing backwards compatibility for L2 and L1 distances (most common)
|
|
|
|
::cvflann::Index< L2<ElementType> >* nnIndex_L2;
|
|
|
|
::cvflann::Index< L1<ElementType> >* nnIndex_L1;
|
|
|
|
};
|
2010-10-13 03:47:50 +08:00
|
|
|
|
2014-11-20 00:08:03 +08:00
|
|
|
|
|
|
|
/** @brief Clusters features using hierarchical k-means algorithm.
|
|
|
|
|
|
|
|
@param features The points to be clustered. The matrix must have elements of type
|
|
|
|
Distance::ElementType.
|
|
|
|
@param centers The centers of the clusters obtained. The matrix must have type
|
|
|
|
Distance::ResultType. The number of rows in this matrix represents the number of clusters desired,
|
|
|
|
however, because of the way the cut in the hierarchical tree is chosen, the number of clusters
|
|
|
|
computed will be the highest number of the form (branching-1)\*k+1 that's lower than the number of
|
|
|
|
clusters desired, where branching is the tree's branching factor (see description of the
|
|
|
|
KMeansIndexParams).
|
|
|
|
@param params Parameters used in the construction of the hierarchical k-means tree.
|
|
|
|
@param d Distance to be used for clustering.
|
2010-10-13 03:47:50 +08:00
|
|
|
|
2014-11-20 00:08:03 +08:00
|
|
|
The method clusters the given feature vectors by constructing a hierarchical k-means tree and
|
|
|
|
choosing a cut in the tree that minimizes the cluster's variance. It returns the number of clusters
|
|
|
|
found.
|
|
|
|
*/
|
2011-07-14 07:04:39 +08:00
|
|
|
template <typename Distance>
|
2011-11-24 06:01:06 +08:00
|
|
|
int hierarchicalClustering(const Mat& features, Mat& centers, const ::cvflann::KMeansIndexParams& params,
|
2011-07-14 07:04:39 +08:00
|
|
|
Distance d = Distance())
|
2010-10-13 03:47:50 +08:00
|
|
|
{
|
2011-07-14 07:04:39 +08:00
|
|
|
typedef typename Distance::ElementType ElementType;
|
|
|
|
typedef typename Distance::ResultType DistanceType;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
CV_Assert(features.type() == CvType<ElementType>::type());
|
2010-10-13 03:47:50 +08:00
|
|
|
CV_Assert(features.isContinuous());
|
2011-07-14 07:04:39 +08:00
|
|
|
::cvflann::Matrix<ElementType> m_features((ElementType*)features.ptr<ElementType>(0), features.rows, features.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
CV_Assert(centers.type() == CvType<DistanceType>::type());
|
2010-10-13 03:47:50 +08:00
|
|
|
CV_Assert(centers.isContinuous());
|
2011-07-14 07:04:39 +08:00
|
|
|
::cvflann::Matrix<DistanceType> m_centers((DistanceType*)centers.ptr<DistanceType>(0), centers.rows, centers.cols);
|
|
|
|
|
|
|
|
return ::cvflann::hierarchicalClustering<Distance>(m_features, m_centers, params, d);
|
|
|
|
}
|
|
|
|
|
2014-11-20 00:08:03 +08:00
|
|
|
/** @deprecated
|
|
|
|
*/
|
2011-07-14 07:04:39 +08:00
|
|
|
template <typename ELEM_TYPE, typename DIST_TYPE>
|
2017-01-24 18:37:41 +08:00
|
|
|
CV_DEPRECATED int hierarchicalClustering(const Mat& features, Mat& centers, const ::cvflann::KMeansIndexParams& params)
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
printf("[WARNING] cv::flann::hierarchicalClustering<ELEM_TYPE,DIST_TYPE> is deprecated, use "
|
|
|
|
"cv::flann::hierarchicalClustering<Distance> instead\n");
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-19 06:01:42 +08:00
|
|
|
if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L2 ) {
|
2011-07-14 07:04:39 +08:00
|
|
|
return hierarchicalClustering< L2<ELEM_TYPE> >(features, centers, params);
|
|
|
|
}
|
2011-07-19 06:01:42 +08:00
|
|
|
else if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L1 ) {
|
2011-07-14 07:04:39 +08:00
|
|
|
return hierarchicalClustering< L1<ELEM_TYPE> >(features, centers, params);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("[ERROR] cv::flann::hierarchicalClustering<ELEM_TYPE,DIST_TYPE> only provides backwards "
|
|
|
|
"compatibility for the L1 and L2 distances. "
|
|
|
|
"For other distance types you must use cv::flann::hierarchicalClustering<Distance>\n");
|
|
|
|
CV_Assert(0);
|
|
|
|
}
|
2010-10-13 03:47:50 +08:00
|
|
|
}
|
|
|
|
|
2014-11-20 00:08:03 +08:00
|
|
|
//! @} flann
|
|
|
|
|
2010-10-13 03:47:50 +08:00
|
|
|
} } // namespace cv::flann
|
|
|
|
|
|
|
|
#endif
|