2011-07-14 07:04:39 +08:00
|
|
|
#include "precomp.hpp"
|
|
|
|
|
|
|
|
#define MINIFLANN_SUPPORT_EXOTIC_DISTANCE_TYPES 0
|
|
|
|
|
|
|
|
static cvflann::IndexParams& get_params(const cv::flann::IndexParams& p)
|
|
|
|
{
|
|
|
|
return *(cvflann::IndexParams*)(p.params);
|
|
|
|
}
|
|
|
|
|
2012-04-30 22:33:52 +08:00
|
|
|
cv::flann::IndexParams::~IndexParams()
|
|
|
|
{
|
|
|
|
delete &get_params(*this);
|
|
|
|
}
|
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
namespace cv
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
namespace flann
|
|
|
|
{
|
|
|
|
|
2011-10-05 09:20:52 +08:00
|
|
|
using namespace cvflann;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
IndexParams::IndexParams()
|
|
|
|
{
|
|
|
|
params = new ::cvflann::IndexParams();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2013-03-23 00:37:49 +08:00
|
|
|
T getParam(const IndexParams& _p, const String& key, const T& defaultVal=T())
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
::cvflann::IndexParams& p = get_params(_p);
|
|
|
|
::cvflann::IndexParams::const_iterator it = p.find(key);
|
|
|
|
if( it == p.end() )
|
|
|
|
return defaultVal;
|
|
|
|
return it->second.cast<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2013-03-23 00:37:49 +08:00
|
|
|
void setParam(IndexParams& _p, const String& key, const T& value)
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
::cvflann::IndexParams& p = get_params(_p);
|
|
|
|
p[key] = value;
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
String IndexParams::getString(const String& key, const String& defaultVal) const
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
return getParam(*this, key, defaultVal);
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
int IndexParams::getInt(const String& key, int defaultVal) const
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
return getParam(*this, key, defaultVal);
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
double IndexParams::getDouble(const String& key, double defaultVal) const
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
return getParam(*this, key, defaultVal);
|
|
|
|
}
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
void IndexParams::setString(const String& key, const String& value)
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
setParam(*this, key, value);
|
|
|
|
}
|
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
void IndexParams::setInt(const String& key, int value)
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
setParam(*this, key, value);
|
|
|
|
}
|
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
void IndexParams::setDouble(const String& key, double value)
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
setParam(*this, key, value);
|
|
|
|
}
|
2011-08-04 22:13:07 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
void IndexParams::setFloat(const String& key, float value)
|
2011-08-04 22:13:07 +08:00
|
|
|
{
|
|
|
|
setParam(*this, key, value);
|
|
|
|
}
|
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
void IndexParams::setBool(const String& key, bool value)
|
2011-08-04 22:13:07 +08:00
|
|
|
{
|
|
|
|
setParam(*this, key, value);
|
|
|
|
}
|
|
|
|
|
2011-08-15 03:46:39 +08:00
|
|
|
void IndexParams::setAlgorithm(int value)
|
2011-08-04 22:13:07 +08:00
|
|
|
{
|
2011-08-15 03:46:39 +08:00
|
|
|
setParam(*this, "algorithm", (cvflann::flann_algorithm_t)value);
|
2011-08-04 22:13:07 +08:00
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
void IndexParams::getAll(std::vector<String>& names,
|
2018-09-19 18:55:26 +08:00
|
|
|
std::vector<FlannIndexType>& types,
|
2013-03-23 00:37:49 +08:00
|
|
|
std::vector<String>& strValues,
|
2011-07-14 07:04:39 +08:00
|
|
|
std::vector<double>& numValues) const
|
|
|
|
{
|
|
|
|
names.clear();
|
|
|
|
types.clear();
|
|
|
|
strValues.clear();
|
|
|
|
numValues.clear();
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
::cvflann::IndexParams& p = get_params(*this);
|
|
|
|
::cvflann::IndexParams::const_iterator it = p.begin(), it_end = p.end();
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
for( ; it != it_end; ++it )
|
|
|
|
{
|
|
|
|
names.push_back(it->first);
|
|
|
|
try
|
|
|
|
{
|
2013-03-23 00:37:49 +08:00
|
|
|
String val = it->second.cast<String>();
|
2018-09-19 18:55:26 +08:00
|
|
|
types.push_back(FLANN_INDEX_TYPE_STRING);
|
2011-07-14 07:04:39 +08:00
|
|
|
strValues.push_back(val);
|
|
|
|
numValues.push_back(-1);
|
2012-10-17 15:12:04 +08:00
|
|
|
continue;
|
2011-08-04 22:13:07 +08:00
|
|
|
}
|
|
|
|
catch (...) {}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-08-04 22:13:07 +08:00
|
|
|
strValues.push_back(it->second.type().name());
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-08-04 22:13:07 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
double val = it->second.cast<double>();
|
2018-09-19 18:55:26 +08:00
|
|
|
types.push_back(FLANN_INDEX_TYPE_64F);
|
2011-08-04 22:13:07 +08:00
|
|
|
numValues.push_back(val);
|
2012-10-17 15:12:04 +08:00
|
|
|
continue;
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
2011-08-04 22:13:07 +08:00
|
|
|
catch (...) {}
|
|
|
|
try
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
2011-08-04 22:13:07 +08:00
|
|
|
float val = it->second.cast<float>();
|
2018-09-19 18:55:26 +08:00
|
|
|
types.push_back(FLANN_INDEX_TYPE_32F);
|
2011-08-04 22:13:07 +08:00
|
|
|
numValues.push_back(val);
|
2012-10-17 15:12:04 +08:00
|
|
|
continue;
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
2011-08-04 22:13:07 +08:00
|
|
|
catch (...) {}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int val = it->second.cast<int>();
|
2018-09-19 18:55:26 +08:00
|
|
|
types.push_back(FLANN_INDEX_TYPE_32S);
|
2011-08-04 22:13:07 +08:00
|
|
|
numValues.push_back(val);
|
2012-10-17 15:12:04 +08:00
|
|
|
continue;
|
2011-08-04 22:13:07 +08:00
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
short val = it->second.cast<short>();
|
2018-09-19 18:55:26 +08:00
|
|
|
types.push_back(FLANN_INDEX_TYPE_16S);
|
2011-08-04 22:13:07 +08:00
|
|
|
numValues.push_back(val);
|
2012-10-17 15:12:04 +08:00
|
|
|
continue;
|
2011-08-04 22:13:07 +08:00
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ushort val = it->second.cast<ushort>();
|
2018-09-19 18:55:26 +08:00
|
|
|
types.push_back(FLANN_INDEX_TYPE_16U);
|
2011-08-04 22:13:07 +08:00
|
|
|
numValues.push_back(val);
|
2012-10-17 15:12:04 +08:00
|
|
|
continue;
|
2011-08-04 22:13:07 +08:00
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
char val = it->second.cast<char>();
|
2018-09-19 18:55:26 +08:00
|
|
|
types.push_back(FLANN_INDEX_TYPE_8S);
|
2011-08-04 22:13:07 +08:00
|
|
|
numValues.push_back(val);
|
2012-10-17 15:12:04 +08:00
|
|
|
continue;
|
2011-08-04 22:13:07 +08:00
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
uchar val = it->second.cast<uchar>();
|
2018-09-19 18:55:26 +08:00
|
|
|
types.push_back(FLANN_INDEX_TYPE_8U);
|
2011-08-04 22:13:07 +08:00
|
|
|
numValues.push_back(val);
|
2012-10-17 15:12:04 +08:00
|
|
|
continue;
|
2011-08-04 22:13:07 +08:00
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
bool val = it->second.cast<bool>();
|
2018-09-19 18:55:26 +08:00
|
|
|
types.push_back(FLANN_INDEX_TYPE_BOOL);
|
2011-08-04 22:13:07 +08:00
|
|
|
numValues.push_back(val);
|
2012-10-17 15:12:04 +08:00
|
|
|
continue;
|
2011-08-04 22:13:07 +08:00
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
cvflann::flann_algorithm_t val = it->second.cast<cvflann::flann_algorithm_t>();
|
2018-09-19 18:55:26 +08:00
|
|
|
types.push_back(FLANN_INDEX_TYPE_ALGORITHM);
|
2011-08-04 22:13:07 +08:00
|
|
|
numValues.push_back(val);
|
2012-10-17 15:12:04 +08:00
|
|
|
continue;
|
2011-08-04 22:13:07 +08:00
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
|
|
|
|
|
2018-09-19 18:55:26 +08:00
|
|
|
types.push_back((FlannIndexType)-1); // unknown type
|
2011-08-04 22:13:07 +08:00
|
|
|
numValues.push_back(-1);
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
KDTreeIndexParams::KDTreeIndexParams(int trees)
|
|
|
|
{
|
|
|
|
::cvflann::IndexParams& p = get_params(*this);
|
|
|
|
p["algorithm"] = FLANN_INDEX_KDTREE;
|
|
|
|
p["trees"] = trees;
|
|
|
|
}
|
|
|
|
|
|
|
|
LinearIndexParams::LinearIndexParams()
|
|
|
|
{
|
|
|
|
::cvflann::IndexParams& p = get_params(*this);
|
|
|
|
p["algorithm"] = FLANN_INDEX_LINEAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompositeIndexParams::CompositeIndexParams(int trees, int branching, int iterations,
|
|
|
|
flann_centers_init_t centers_init, float cb_index )
|
|
|
|
{
|
|
|
|
::cvflann::IndexParams& p = get_params(*this);
|
|
|
|
p["algorithm"] = FLANN_INDEX_KMEANS;
|
|
|
|
// number of randomized trees to use (for kdtree)
|
|
|
|
p["trees"] = trees;
|
|
|
|
// branching factor
|
|
|
|
p["branching"] = branching;
|
|
|
|
// max iterations to perform in one kmeans clustering (kmeans tree)
|
|
|
|
p["iterations"] = iterations;
|
|
|
|
// algorithm used for picking the initial cluster centers for kmeans tree
|
|
|
|
p["centers_init"] = centers_init;
|
|
|
|
// cluster boundary index. Used when searching the kmeans tree
|
|
|
|
p["cb_index"] = cb_index;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
AutotunedIndexParams::AutotunedIndexParams(float target_precision, float build_weight,
|
|
|
|
float memory_weight, float sample_fraction)
|
|
|
|
{
|
|
|
|
::cvflann::IndexParams& p = get_params(*this);
|
|
|
|
p["algorithm"] = FLANN_INDEX_AUTOTUNED;
|
|
|
|
// precision desired (used for autotuning, -1 otherwise)
|
|
|
|
p["target_precision"] = target_precision;
|
|
|
|
// build tree time weighting factor
|
|
|
|
p["build_weight"] = build_weight;
|
|
|
|
// index memory weighting factor
|
|
|
|
p["memory_weight"] = memory_weight;
|
|
|
|
// what fraction of the dataset to use for autotuning
|
|
|
|
p["sample_fraction"] = sample_fraction;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
|
|
|
|
KMeansIndexParams::KMeansIndexParams(int branching, int iterations,
|
|
|
|
flann_centers_init_t centers_init, float cb_index )
|
|
|
|
{
|
|
|
|
::cvflann::IndexParams& p = get_params(*this);
|
|
|
|
p["algorithm"] = FLANN_INDEX_KMEANS;
|
|
|
|
// branching factor
|
|
|
|
p["branching"] = branching;
|
|
|
|
// max iterations to perform in one kmeans clustering (kmeans tree)
|
|
|
|
p["iterations"] = iterations;
|
|
|
|
// algorithm used for picking the initial cluster centers for kmeans tree
|
|
|
|
p["centers_init"] = centers_init;
|
|
|
|
// cluster boundary index. Used when searching the kmeans tree
|
|
|
|
p["cb_index"] = cb_index;
|
|
|
|
}
|
2012-09-27 18:58:17 +08:00
|
|
|
|
|
|
|
HierarchicalClusteringIndexParams::HierarchicalClusteringIndexParams(int branching ,
|
|
|
|
flann_centers_init_t centers_init,
|
|
|
|
int trees, int leaf_size)
|
|
|
|
{
|
|
|
|
::cvflann::IndexParams& p = get_params(*this);
|
|
|
|
p["algorithm"] = FLANN_INDEX_HIERARCHICAL;
|
|
|
|
// The branching factor used in the hierarchical clustering
|
|
|
|
p["branching"] = branching;
|
|
|
|
// Algorithm used for picking the initial cluster centers
|
|
|
|
p["centers_init"] = centers_init;
|
|
|
|
// number of parallel trees to build
|
|
|
|
p["trees"] = trees;
|
|
|
|
// maximum leaf size
|
|
|
|
p["leaf_size"] = leaf_size;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
LshIndexParams::LshIndexParams(int table_number, int key_size, int multi_probe_level)
|
|
|
|
{
|
|
|
|
::cvflann::IndexParams& p = get_params(*this);
|
|
|
|
p["algorithm"] = FLANN_INDEX_LSH;
|
|
|
|
// The number of hash tables to use
|
2012-09-27 18:58:17 +08:00
|
|
|
p["table_number"] = table_number;
|
2011-07-14 07:04:39 +08:00
|
|
|
// The length of the key in the hash tables
|
2012-09-27 18:58:17 +08:00
|
|
|
p["key_size"] = key_size;
|
2011-07-14 07:04:39 +08:00
|
|
|
// Number of levels to use in multi-probe (0 for standard LSH)
|
2012-09-27 18:58:17 +08:00
|
|
|
p["multi_probe_level"] = multi_probe_level;
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
SavedIndexParams::SavedIndexParams(const String& _filename)
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
2013-03-23 00:37:49 +08:00
|
|
|
String filename = _filename;
|
2011-07-14 07:04:39 +08:00
|
|
|
::cvflann::IndexParams& p = get_params(*this);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
p["algorithm"] = FLANN_INDEX_SAVED;
|
|
|
|
p["filename"] = filename;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2020-08-04 02:00:59 +08:00
|
|
|
SearchParams::SearchParams( int checks, float eps, bool sorted, bool explore_all_trees )
|
|
|
|
{
|
|
|
|
::cvflann::IndexParams& p = get_params(*this);
|
|
|
|
|
|
|
|
// how many leafs to visit when searching for neighbours (-1 for unlimited)
|
|
|
|
p["checks"] = checks;
|
|
|
|
// search for eps-approximate neighbours (default: 0)
|
|
|
|
p["eps"] = eps;
|
|
|
|
// only for radius search, require neighbours sorted by distance (default: true)
|
|
|
|
p["sorted"] = sorted;
|
|
|
|
// if false, search stops at the tree reaching the number of max checks (original behavior).
|
|
|
|
// When true, we do a descent in each tree and. Like before the alternative paths
|
|
|
|
// stored in the heap are not be processed further when max checks is reached.
|
|
|
|
p["explore_all_trees"] = explore_all_trees;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
SearchParams::SearchParams( int checks, float eps, bool sorted )
|
|
|
|
{
|
|
|
|
::cvflann::IndexParams& p = get_params(*this);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
// how many leafs to visit when searching for neighbours (-1 for unlimited)
|
|
|
|
p["checks"] = checks;
|
|
|
|
// search for eps-approximate neighbours (default: 0)
|
|
|
|
p["eps"] = eps;
|
|
|
|
// only for radius search, require neighbours sorted by distance (default: true)
|
|
|
|
p["sorted"] = sorted;
|
2020-08-04 02:00:59 +08:00
|
|
|
// if false, search stops at the tree reaching the number of max checks (original behavior).
|
|
|
|
// When true, we do a descent in each tree and. Like before the alternative paths
|
|
|
|
// stored in the heap are not be processed further when max checks is reached.
|
|
|
|
p["explore_all_trees"] = false;
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
|
|
|
|
template<typename Distance, typename IndexType> void
|
|
|
|
buildIndex_(void*& index, const Mat& data, const IndexParams& params, const Distance& dist = Distance())
|
|
|
|
{
|
|
|
|
typedef typename Distance::ElementType ElementType;
|
2011-08-15 03:46:39 +08:00
|
|
|
if(DataType<ElementType>::type != data.type())
|
2013-04-03 00:59:43 +08:00
|
|
|
CV_Error_(Error::StsUnsupportedFormat, ("type=%d\n", data.type()));
|
2011-08-15 03:46:39 +08:00
|
|
|
if(!data.isContinuous())
|
2013-04-03 00:59:43 +08:00
|
|
|
CV_Error(Error::StsBadArg, "Only continuous arrays are supported");
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
::cvflann::Matrix<ElementType> dataset((ElementType*)data.data, data.rows, data.cols);
|
|
|
|
IndexType* _index = new IndexType(dataset, get_params(params), dist);
|
2016-07-12 16:53:51 +08:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_index->buildIndex();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
delete _index;
|
|
|
|
_index = NULL;
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
index = _index;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Distance> void
|
|
|
|
buildIndex(void*& index, const Mat& data, const IndexParams& params, const Distance& dist = Distance())
|
|
|
|
{
|
|
|
|
buildIndex_<Distance, ::cvflann::Index<Distance> >(index, data, params, dist);
|
|
|
|
}
|
|
|
|
|
2011-10-26 21:04:53 +08:00
|
|
|
#if CV_NEON
|
|
|
|
typedef ::cvflann::Hamming<uchar> HammingDistance;
|
|
|
|
#else
|
2011-07-14 07:04:39 +08:00
|
|
|
typedef ::cvflann::HammingLUT HammingDistance;
|
2011-10-26 21:04:53 +08:00
|
|
|
#endif
|
2020-09-02 04:38:21 +08:00
|
|
|
typedef ::cvflann::DNAmming2<uchar> DNAmmingDistance;
|
2011-07-14 07:04:39 +08:00
|
|
|
|
|
|
|
Index::Index()
|
|
|
|
{
|
|
|
|
index = 0;
|
|
|
|
featureType = CV_32F;
|
|
|
|
algo = FLANN_INDEX_LINEAR;
|
|
|
|
distType = FLANN_DIST_L2;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
Index::Index(InputArray _data, const IndexParams& params, flann_distance_t _distType)
|
|
|
|
{
|
|
|
|
index = 0;
|
|
|
|
featureType = CV_32F;
|
|
|
|
algo = FLANN_INDEX_LINEAR;
|
|
|
|
distType = FLANN_DIST_L2;
|
|
|
|
build(_data, params, _distType);
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
void Index::build(InputArray _data, const IndexParams& params, flann_distance_t _distType)
|
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
release();
|
2020-12-12 03:09:35 +08:00
|
|
|
|
|
|
|
// Index may reuse 'data' during search, need to keep it alive
|
|
|
|
features_clone = _data.getMat().clone();
|
|
|
|
Mat data = features_clone;
|
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
algo = getParam<flann_algorithm_t>(params, "algorithm", FLANN_INDEX_LINEAR);
|
|
|
|
if( algo == FLANN_INDEX_SAVED )
|
|
|
|
{
|
2020-12-12 03:09:35 +08:00
|
|
|
load_(getParam<String>(params, "filename", String()));
|
2011-07-14 07:04:39 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
index = 0;
|
|
|
|
featureType = data.type();
|
|
|
|
distType = _distType;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
if ( algo == FLANN_INDEX_LSH)
|
2012-09-29 01:40:38 +08:00
|
|
|
{
|
|
|
|
distType = FLANN_DIST_HAMMING;
|
|
|
|
}
|
2011-07-14 07:04:39 +08:00
|
|
|
|
|
|
|
switch( distType )
|
|
|
|
{
|
2012-09-27 18:58:17 +08:00
|
|
|
case FLANN_DIST_HAMMING:
|
|
|
|
buildIndex< HammingDistance >(index, data, params);
|
|
|
|
break;
|
2011-07-14 07:04:39 +08:00
|
|
|
case FLANN_DIST_L2:
|
|
|
|
buildIndex< ::cvflann::L2<float> >(index, data, params);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_L1:
|
|
|
|
buildIndex< ::cvflann::L1<float> >(index, data, params);
|
|
|
|
break;
|
|
|
|
#if MINIFLANN_SUPPORT_EXOTIC_DISTANCE_TYPES
|
2020-09-02 04:38:21 +08:00
|
|
|
case FLANN_DIST_DNAMMING:
|
|
|
|
buildIndex< DNAmmingDistance >(index, data, params);
|
|
|
|
break;
|
2011-07-14 07:04:39 +08:00
|
|
|
case FLANN_DIST_MAX:
|
|
|
|
buildIndex< ::cvflann::MaxDistance<float> >(index, data, params);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_HIST_INTERSECT:
|
|
|
|
buildIndex< ::cvflann::HistIntersectionDistance<float> >(index, data, params);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_HELLINGER:
|
|
|
|
buildIndex< ::cvflann::HellingerDistance<float> >(index, data, params);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_CHI_SQUARE:
|
|
|
|
buildIndex< ::cvflann::ChiSquareDistance<float> >(index, data, params);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_KL:
|
|
|
|
buildIndex< ::cvflann::KL_Divergence<float> >(index, data, params);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2013-04-03 00:59:43 +08:00
|
|
|
CV_Error(Error::StsBadArg, "Unknown/unsupported distance type");
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename IndexType> void deleteIndex_(void* index)
|
|
|
|
{
|
|
|
|
delete (IndexType*)index;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Distance> void deleteIndex(void* index)
|
|
|
|
{
|
|
|
|
deleteIndex_< ::cvflann::Index<Distance> >(index);
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
Index::~Index()
|
|
|
|
{
|
|
|
|
release();
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
void Index::release()
|
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2020-12-12 03:09:35 +08:00
|
|
|
features_clone.release();
|
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
if( !index )
|
|
|
|
return;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-09-27 18:58:17 +08:00
|
|
|
switch( distType )
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
2012-09-27 18:58:17 +08:00
|
|
|
case FLANN_DIST_HAMMING:
|
|
|
|
deleteIndex< HammingDistance >(index);
|
|
|
|
break;
|
2011-07-14 07:04:39 +08:00
|
|
|
case FLANN_DIST_L2:
|
|
|
|
deleteIndex< ::cvflann::L2<float> >(index);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_L1:
|
|
|
|
deleteIndex< ::cvflann::L1<float> >(index);
|
|
|
|
break;
|
|
|
|
#if MINIFLANN_SUPPORT_EXOTIC_DISTANCE_TYPES
|
2020-09-02 04:38:21 +08:00
|
|
|
case FLANN_DIST_DNAMMING:
|
|
|
|
deleteIndex< DNAmmingDistance >(index);
|
|
|
|
break;
|
2011-07-14 07:04:39 +08:00
|
|
|
case FLANN_DIST_MAX:
|
|
|
|
deleteIndex< ::cvflann::MaxDistance<float> >(index);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_HIST_INTERSECT:
|
|
|
|
deleteIndex< ::cvflann::HistIntersectionDistance<float> >(index);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_HELLINGER:
|
|
|
|
deleteIndex< ::cvflann::HellingerDistance<float> >(index);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_CHI_SQUARE:
|
|
|
|
deleteIndex< ::cvflann::ChiSquareDistance<float> >(index);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_KL:
|
|
|
|
deleteIndex< ::cvflann::KL_Divergence<float> >(index);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2013-04-03 00:59:43 +08:00
|
|
|
CV_Error(Error::StsBadArg, "Unknown/unsupported distance type");
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Distance, typename IndexType>
|
|
|
|
void runKnnSearch_(void* index, const Mat& query, Mat& indices, Mat& dists,
|
|
|
|
int knn, const SearchParams& params)
|
|
|
|
{
|
|
|
|
typedef typename Distance::ElementType ElementType;
|
|
|
|
typedef typename Distance::ResultType DistanceType;
|
|
|
|
int type = DataType<ElementType>::type;
|
|
|
|
int dtype = DataType<DistanceType>::type;
|
2018-01-08 17:26:10 +08:00
|
|
|
IndexType* index_ = (IndexType*)index;
|
|
|
|
|
|
|
|
CV_Assert((size_t)knn <= index_->size());
|
2011-07-14 07:04:39 +08:00
|
|
|
CV_Assert(query.type() == type && indices.type() == CV_32S && dists.type() == dtype);
|
|
|
|
CV_Assert(query.isContinuous() && indices.isContinuous() && dists.isContinuous());
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
::cvflann::Matrix<ElementType> _query((ElementType*)query.data, query.rows, query.cols);
|
2014-08-13 19:08:27 +08:00
|
|
|
::cvflann::Matrix<int> _indices(indices.ptr<int>(), indices.rows, indices.cols);
|
|
|
|
::cvflann::Matrix<DistanceType> _dists(dists.ptr<DistanceType>(), dists.rows, dists.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2018-01-08 17:26:10 +08:00
|
|
|
index_->knnSearch(_query, _indices, _dists, knn,
|
|
|
|
(const ::cvflann::SearchParams&)get_params(params));
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
template<typename Distance>
|
|
|
|
void runKnnSearch(void* index, const Mat& query, Mat& indices, Mat& dists,
|
|
|
|
int knn, const SearchParams& params)
|
|
|
|
{
|
|
|
|
runKnnSearch_<Distance, ::cvflann::Index<Distance> >(index, query, indices, dists, knn, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Distance, typename IndexType>
|
|
|
|
int runRadiusSearch_(void* index, const Mat& query, Mat& indices, Mat& dists,
|
|
|
|
double radius, const SearchParams& params)
|
|
|
|
{
|
|
|
|
typedef typename Distance::ElementType ElementType;
|
|
|
|
typedef typename Distance::ResultType DistanceType;
|
|
|
|
int type = DataType<ElementType>::type;
|
|
|
|
int dtype = DataType<DistanceType>::type;
|
|
|
|
CV_Assert(query.type() == type && indices.type() == CV_32S && dists.type() == dtype);
|
|
|
|
CV_Assert(query.isContinuous() && indices.isContinuous() && dists.isContinuous());
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
::cvflann::Matrix<ElementType> _query((ElementType*)query.data, query.rows, query.cols);
|
2014-08-13 19:08:27 +08:00
|
|
|
::cvflann::Matrix<int> _indices(indices.ptr<int>(), indices.rows, indices.cols);
|
|
|
|
::cvflann::Matrix<DistanceType> _dists(dists.ptr<DistanceType>(), dists.rows, dists.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
return ((IndexType*)index)->radiusSearch(_query, _indices, _dists,
|
2012-10-22 22:46:26 +08:00
|
|
|
saturate_cast<float>(radius),
|
2011-07-14 07:04:39 +08:00
|
|
|
(const ::cvflann::SearchParams&)get_params(params));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Distance>
|
|
|
|
int runRadiusSearch(void* index, const Mat& query, Mat& indices, Mat& dists,
|
|
|
|
double radius, const SearchParams& params)
|
|
|
|
{
|
|
|
|
return runRadiusSearch_<Distance, ::cvflann::Index<Distance> >(index, query, indices, dists, radius, params);
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
static void createIndicesDists(OutputArray _indices, OutputArray _dists,
|
|
|
|
Mat& indices, Mat& dists, int rows,
|
|
|
|
int minCols, int maxCols, int dtype)
|
|
|
|
{
|
|
|
|
if( _indices.needed() )
|
|
|
|
{
|
|
|
|
indices = _indices.getMat();
|
|
|
|
if( !indices.isContinuous() || indices.type() != CV_32S ||
|
|
|
|
indices.rows != rows || indices.cols < minCols || indices.cols > maxCols )
|
|
|
|
{
|
|
|
|
if( !indices.isContinuous() )
|
|
|
|
_indices.release();
|
|
|
|
_indices.create( rows, minCols, CV_32S );
|
|
|
|
indices = _indices.getMat();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
indices.create( rows, minCols, CV_32S );
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
if( _dists.needed() )
|
|
|
|
{
|
|
|
|
dists = _dists.getMat();
|
|
|
|
if( !dists.isContinuous() || dists.type() != dtype ||
|
|
|
|
dists.rows != rows || dists.cols < minCols || dists.cols > maxCols )
|
|
|
|
{
|
2017-09-03 08:37:30 +08:00
|
|
|
if( !_dists.isContinuous() )
|
2011-07-14 07:04:39 +08:00
|
|
|
_dists.release();
|
|
|
|
_dists.create( rows, minCols, dtype );
|
|
|
|
dists = _dists.getMat();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dists.create( rows, minCols, dtype );
|
|
|
|
}
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
void Index::knnSearch(InputArray _query, OutputArray _indices,
|
2011-07-14 07:04:39 +08:00
|
|
|
OutputArray _dists, int knn, const SearchParams& params)
|
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
Mat query = _query.getMat(), indices, dists;
|
2020-09-02 04:38:21 +08:00
|
|
|
int dtype = (distType == FLANN_DIST_HAMMING)
|
|
|
|
|| (distType == FLANN_DIST_DNAMMING) ? CV_32S : CV_32F;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
createIndicesDists( _indices, _dists, indices, dists, query.rows, knn, knn, dtype );
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
switch( distType )
|
|
|
|
{
|
2012-09-27 18:58:17 +08:00
|
|
|
case FLANN_DIST_HAMMING:
|
|
|
|
runKnnSearch<HammingDistance>(index, query, indices, dists, knn, params);
|
|
|
|
break;
|
2011-07-14 07:04:39 +08:00
|
|
|
case FLANN_DIST_L2:
|
|
|
|
runKnnSearch< ::cvflann::L2<float> >(index, query, indices, dists, knn, params);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_L1:
|
|
|
|
runKnnSearch< ::cvflann::L1<float> >(index, query, indices, dists, knn, params);
|
|
|
|
break;
|
|
|
|
#if MINIFLANN_SUPPORT_EXOTIC_DISTANCE_TYPES
|
2020-09-02 04:38:21 +08:00
|
|
|
case FLANN_DIST_DNAMMING:
|
|
|
|
runKnnSearch<DNAmmingDistance>(index, query, indices, dists, knn, params);
|
|
|
|
break;
|
2011-07-14 07:04:39 +08:00
|
|
|
case FLANN_DIST_MAX:
|
|
|
|
runKnnSearch< ::cvflann::MaxDistance<float> >(index, query, indices, dists, knn, params);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_HIST_INTERSECT:
|
|
|
|
runKnnSearch< ::cvflann::HistIntersectionDistance<float> >(index, query, indices, dists, knn, params);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_HELLINGER:
|
|
|
|
runKnnSearch< ::cvflann::HellingerDistance<float> >(index, query, indices, dists, knn, params);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_CHI_SQUARE:
|
|
|
|
runKnnSearch< ::cvflann::ChiSquareDistance<float> >(index, query, indices, dists, knn, params);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_KL:
|
|
|
|
runKnnSearch< ::cvflann::KL_Divergence<float> >(index, query, indices, dists, knn, params);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2013-04-03 00:59:43 +08:00
|
|
|
CV_Error(Error::StsBadArg, "Unknown/unsupported distance type");
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
int Index::radiusSearch(InputArray _query, OutputArray _indices,
|
|
|
|
OutputArray _dists, double radius, int maxResults,
|
|
|
|
const SearchParams& params)
|
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
Mat query = _query.getMat(), indices, dists;
|
2020-09-02 04:38:21 +08:00
|
|
|
int dtype = (distType == FLANN_DIST_HAMMING)
|
|
|
|
|| (distType == FLANN_DIST_DNAMMING) ? CV_32S : CV_32F;
|
2011-07-14 07:04:39 +08:00
|
|
|
CV_Assert( maxResults > 0 );
|
|
|
|
createIndicesDists( _indices, _dists, indices, dists, query.rows, maxResults, INT_MAX, dtype );
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
if( algo == FLANN_INDEX_LSH )
|
2013-04-03 00:59:43 +08:00
|
|
|
CV_Error( Error::StsNotImplemented, "LSH index does not support radiusSearch operation" );
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
switch( distType )
|
|
|
|
{
|
2012-09-27 18:58:17 +08:00
|
|
|
case FLANN_DIST_HAMMING:
|
|
|
|
return runRadiusSearch< HammingDistance >(index, query, indices, dists, radius, params);
|
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
case FLANN_DIST_L2:
|
|
|
|
return runRadiusSearch< ::cvflann::L2<float> >(index, query, indices, dists, radius, params);
|
|
|
|
case FLANN_DIST_L1:
|
|
|
|
return runRadiusSearch< ::cvflann::L1<float> >(index, query, indices, dists, radius, params);
|
|
|
|
#if MINIFLANN_SUPPORT_EXOTIC_DISTANCE_TYPES
|
2020-09-02 04:38:21 +08:00
|
|
|
case FLANN_DIST_DNAMMING:
|
|
|
|
return runRadiusSearch< DNAmmingDistance >(index, query, indices, dists, radius, params);
|
2011-07-14 07:04:39 +08:00
|
|
|
case FLANN_DIST_MAX:
|
|
|
|
return runRadiusSearch< ::cvflann::MaxDistance<float> >(index, query, indices, dists, radius, params);
|
|
|
|
case FLANN_DIST_HIST_INTERSECT:
|
|
|
|
return runRadiusSearch< ::cvflann::HistIntersectionDistance<float> >(index, query, indices, dists, radius, params);
|
|
|
|
case FLANN_DIST_HELLINGER:
|
|
|
|
return runRadiusSearch< ::cvflann::HellingerDistance<float> >(index, query, indices, dists, radius, params);
|
|
|
|
case FLANN_DIST_CHI_SQUARE:
|
|
|
|
return runRadiusSearch< ::cvflann::ChiSquareDistance<float> >(index, query, indices, dists, radius, params);
|
|
|
|
case FLANN_DIST_KL:
|
|
|
|
return runRadiusSearch< ::cvflann::KL_Divergence<float> >(index, query, indices, dists, radius, params);
|
|
|
|
#endif
|
|
|
|
default:
|
2013-04-03 00:59:43 +08:00
|
|
|
CV_Error(Error::StsBadArg, "Unknown/unsupported distance type");
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
flann_distance_t Index::getDistance() const
|
|
|
|
{
|
|
|
|
return distType;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
flann_algorithm_t Index::getAlgorithm() const
|
|
|
|
{
|
|
|
|
return algo;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename IndexType> void saveIndex_(const Index* index0, const void* index, FILE* fout)
|
|
|
|
{
|
|
|
|
IndexType* _index = (IndexType*)index;
|
|
|
|
::cvflann::save_header(fout, *_index);
|
|
|
|
// some compilers may store short enumerations as bytes,
|
|
|
|
// so make sure we always write integers (which are 4-byte values in any modern C compiler)
|
|
|
|
int idistType = (int)index0->getDistance();
|
|
|
|
::cvflann::save_value<int>(fout, idistType);
|
|
|
|
_index->saveIndex(fout);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Distance> void saveIndex(const Index* index0, const void* index, FILE* fout)
|
|
|
|
{
|
|
|
|
saveIndex_< ::cvflann::Index<Distance> >(index0, index, fout);
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
void Index::save(const String& filename) const
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
FILE* fout = fopen(filename.c_str(), "wb");
|
|
|
|
if (fout == NULL)
|
2013-04-03 00:59:43 +08:00
|
|
|
CV_Error_( Error::StsError, ("Can not open file %s for writing FLANN index\n", filename.c_str()) );
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
switch( distType )
|
|
|
|
{
|
2012-09-27 18:58:17 +08:00
|
|
|
case FLANN_DIST_HAMMING:
|
|
|
|
saveIndex< HammingDistance >(this, index, fout);
|
|
|
|
break;
|
2011-07-14 07:04:39 +08:00
|
|
|
case FLANN_DIST_L2:
|
|
|
|
saveIndex< ::cvflann::L2<float> >(this, index, fout);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_L1:
|
|
|
|
saveIndex< ::cvflann::L1<float> >(this, index, fout);
|
|
|
|
break;
|
|
|
|
#if MINIFLANN_SUPPORT_EXOTIC_DISTANCE_TYPES
|
2020-09-02 04:38:21 +08:00
|
|
|
case FLANN_DIST_DNAMMING:
|
|
|
|
saveIndex< DNAmmingDistance >(this, index, fout);
|
|
|
|
break;
|
2011-07-14 07:04:39 +08:00
|
|
|
case FLANN_DIST_MAX:
|
|
|
|
saveIndex< ::cvflann::MaxDistance<float> >(this, index, fout);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_HIST_INTERSECT:
|
|
|
|
saveIndex< ::cvflann::HistIntersectionDistance<float> >(this, index, fout);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_HELLINGER:
|
|
|
|
saveIndex< ::cvflann::HellingerDistance<float> >(this, index, fout);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_CHI_SQUARE:
|
|
|
|
saveIndex< ::cvflann::ChiSquareDistance<float> >(this, index, fout);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_KL:
|
|
|
|
saveIndex< ::cvflann::KL_Divergence<float> >(this, index, fout);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
fclose(fout);
|
|
|
|
fout = 0;
|
2013-04-03 00:59:43 +08:00
|
|
|
CV_Error(Error::StsBadArg, "Unknown/unsupported distance type");
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
|
|
|
if( fout )
|
|
|
|
fclose(fout);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Distance, typename IndexType>
|
|
|
|
bool loadIndex_(Index* index0, void*& index, const Mat& data, FILE* fin, const Distance& dist=Distance())
|
|
|
|
{
|
|
|
|
typedef typename Distance::ElementType ElementType;
|
|
|
|
CV_Assert(DataType<ElementType>::type == data.type() && data.isContinuous());
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
::cvflann::Matrix<ElementType> dataset((ElementType*)data.data, data.rows, data.cols);
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
::cvflann::IndexParams params;
|
|
|
|
params["algorithm"] = index0->getAlgorithm();
|
|
|
|
IndexType* _index = new IndexType(dataset, params, dist);
|
|
|
|
_index->loadIndex(fin);
|
|
|
|
index = _index;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Distance>
|
|
|
|
bool loadIndex(Index* index0, void*& index, const Mat& data, FILE* fin, const Distance& dist=Distance())
|
|
|
|
{
|
|
|
|
return loadIndex_<Distance, ::cvflann::Index<Distance> >(index0, index, data, fin, dist);
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
bool Index::load(InputArray _data, const String& filename)
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
release();
|
2020-12-12 03:09:35 +08:00
|
|
|
|
|
|
|
// Index may reuse 'data' during search, need to keep it alive
|
|
|
|
features_clone = _data.getMat().clone();
|
|
|
|
Mat data = features_clone;
|
|
|
|
|
|
|
|
return load_(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Index::load_(const String& filename)
|
|
|
|
{
|
|
|
|
Mat data = features_clone;
|
|
|
|
bool ok = true;
|
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
FILE* fin = fopen(filename.c_str(), "rb");
|
|
|
|
if (fin == NULL)
|
|
|
|
return false;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
::cvflann::IndexHeader header = ::cvflann::load_header(fin);
|
|
|
|
algo = header.index_type;
|
|
|
|
featureType = header.data_type == FLANN_UINT8 ? CV_8U :
|
|
|
|
header.data_type == FLANN_INT8 ? CV_8S :
|
|
|
|
header.data_type == FLANN_UINT16 ? CV_16U :
|
|
|
|
header.data_type == FLANN_INT16 ? CV_16S :
|
|
|
|
header.data_type == FLANN_INT32 ? CV_32S :
|
|
|
|
header.data_type == FLANN_FLOAT32 ? CV_32F :
|
|
|
|
header.data_type == FLANN_FLOAT64 ? CV_64F : -1;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
if( (int)header.rows != data.rows || (int)header.cols != data.cols ||
|
|
|
|
featureType != data.type() )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Reading FLANN index error: the saved data size (%d, %d) or type (%d) is different from the passed one (%d, %d), %d\n",
|
|
|
|
(int)header.rows, (int)header.cols, featureType, data.rows, data.cols, data.type());
|
|
|
|
fclose(fin);
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-09-27 18:58:17 +08:00
|
|
|
int idistType = 0;
|
|
|
|
::cvflann::load_value(fin, idistType);
|
|
|
|
distType = (flann_distance_t)idistType;
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
if( !((distType == FLANN_DIST_HAMMING && featureType == CV_8U) ||
|
2020-09-02 04:38:21 +08:00
|
|
|
(distType == FLANN_DIST_DNAMMING && featureType == CV_8U) ||
|
2012-09-27 18:58:17 +08:00
|
|
|
(distType != FLANN_DIST_HAMMING && featureType == CV_32F)) )
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Reading FLANN index error: unsupported feature type %d for the index type %d\n", featureType, algo);
|
|
|
|
fclose(fin);
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-09-27 18:58:17 +08:00
|
|
|
switch( distType )
|
2011-07-14 07:04:39 +08:00
|
|
|
{
|
2012-09-27 18:58:17 +08:00
|
|
|
case FLANN_DIST_HAMMING:
|
|
|
|
loadIndex< HammingDistance >(this, index, data, fin);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_L2:
|
|
|
|
loadIndex< ::cvflann::L2<float> >(this, index, data, fin);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_L1:
|
|
|
|
loadIndex< ::cvflann::L1<float> >(this, index, data, fin);
|
|
|
|
break;
|
|
|
|
#if MINIFLANN_SUPPORT_EXOTIC_DISTANCE_TYPES
|
2020-09-02 04:38:21 +08:00
|
|
|
case FLANN_DIST_DNAMMING:
|
|
|
|
loadIndex< DNAmmingDistance >(this, index, data, fin);
|
|
|
|
break;
|
2012-09-27 18:58:17 +08:00
|
|
|
case FLANN_DIST_MAX:
|
|
|
|
loadIndex< ::cvflann::MaxDistance<float> >(this, index, data, fin);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_HIST_INTERSECT:
|
2020-07-02 00:20:02 +08:00
|
|
|
loadIndex< ::cvflann::HistIntersectionDistance<float> >(this, index, data, fin);
|
2012-09-27 18:58:17 +08:00
|
|
|
break;
|
|
|
|
case FLANN_DIST_HELLINGER:
|
|
|
|
loadIndex< ::cvflann::HellingerDistance<float> >(this, index, data, fin);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_CHI_SQUARE:
|
|
|
|
loadIndex< ::cvflann::ChiSquareDistance<float> >(this, index, data, fin);
|
|
|
|
break;
|
|
|
|
case FLANN_DIST_KL:
|
|
|
|
loadIndex< ::cvflann::KL_Divergence<float> >(this, index, data, fin);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Reading FLANN index error: unsupported distance type %d\n", distType);
|
|
|
|
ok = false;
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
2012-09-27 18:58:17 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
if( fin )
|
|
|
|
fclose(fin);
|
|
|
|
return ok;
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-07-14 07:04:39 +08:00
|
|
|
}
|