@ -308,7 +308,7 @@ SearchParams::SearchParams( int checks, float eps, bool sorted )
template < typename Distance , typename IndexType > void
template < typename Distance , typename IndexType > void
buildIndex_ ( void * & index , const Mat & data , const IndexParams & params , const Distance & dist = Distance ( ) )
buildIndex_ ( void * & index , const Mat & wholedata , const Mat & data , const IndexParams & params , const Distance & dist = Distance ( ) )
{
{
typedef typename Distance : : ElementType ElementType ;
typedef typename Distance : : ElementType ElementType ;
if ( DataType < ElementType > : : type ! = data . type ( ) )
if ( DataType < ElementType > : : type ! = data . type ( ) )
@ -317,15 +317,25 @@ buildIndex_(void*& index, const Mat& data, const IndexParams& params, const Dist
CV_Error ( CV_StsBadArg , " Only continuous arrays are supported " ) ;
CV_Error ( CV_StsBadArg , " Only continuous arrays are supported " ) ;
: : cvflann : : Matrix < ElementType > dataset ( ( ElementType * ) data . data , data . rows , data . cols ) ;
: : cvflann : : Matrix < ElementType > dataset ( ( ElementType * ) data . data , data . rows , data . cols ) ;
IndexType * _index = new IndexType ( dataset , get_params ( params ) , dist ) ;
IndexType * _index = NULL ;
if ( ! index | | getParam < flann_algorithm_t > ( params , " algorithm " , FLANN_INDEX_LINEAR ) ! = FLANN_INDEX_LSH ) // currently, additional index support is the lsh algorithm only.
{
_index = new IndexType ( dataset , get_params ( params ) , dist ) ;
_index - > buildIndex ( ) ;
_index - > buildIndex ( ) ;
index = _index ;
index = _index ;
}
else // build additional lsh index
{
: : cvflann : : Matrix < ElementType > wholedataset ( ( ElementType * ) wholedata . data , wholedata . rows , wholedata . cols ) ;
( ( IndexType * ) index ) - > addIndex ( wholedataset , dataset ) ;
}
}
}
template < typename Distance > void
template < typename Distance > void
buildIndex ( void * & index , const Mat & data , const IndexParams & params , const Distance & dist = Distance ( ) )
buildIndex ( void * & index , const Mat & wholedata , const Mat & data , const IndexParams & params , const Distance & dist = Distance ( ) )
{
{
buildIndex_ < Distance , : : cvflann : : Index < Distance > > ( index , data , params , dist ) ;
buildIndex_ < Distance , : : cvflann : : Index < Distance > > ( index , wholedata , data , params , dist ) ;
}
}
# if CV_NEON
# if CV_NEON
@ -348,21 +358,28 @@ Index::Index(InputArray _data, const IndexParams& params, flann_distance_t _dist
featureType = CV_32F ;
featureType = CV_32F ;
algo = FLANN_INDEX_LINEAR ;
algo = FLANN_INDEX_LINEAR ;
distType = FLANN_DIST_L2 ;
distType = FLANN_DIST_L2 ;
build ( _data , params , _distType ) ;
build ( _data , _data , params , _distType ) ;
}
}
void Index : : build ( InputArray _data , const IndexParams & params , flann_distance_t _distType )
void Index : : build ( InputArray & _wholedata , InputArray & _data , const IndexParams & params , flann_distance_t _distType )
{
{
release ( ) ;
algo = getParam < flann_algorithm_t > ( params , " algorithm " , FLANN_INDEX_LINEAR ) ;
algo = getParam < flann_algorithm_t > ( params , " algorithm " , FLANN_INDEX_LINEAR ) ;
if ( algo = = FLANN_INDEX_SAVED )
if ( algo ! = FLANN_INDEX_LSH ) // do not release if algo == FLANN_INDEX_LSH
{
release ( ) ;
}
if ( algo = = FLANN_INDEX_SAVED )
{
{
load ( _data , getParam < std : : string > ( params , " filename " , std : : string ( ) ) ) ;
load ( _data , getParam < std : : string > ( params , " filename " , std : : string ( ) ) ) ;
return ;
return ;
}
}
Mat data = _data . getMat ( ) ;
Mat data = _data . getMat ( ) ;
if ( algo ! = FLANN_INDEX_LSH ) // do not clear if algo == FLANN_INDEX_LSH
{
index = 0 ;
index = 0 ;
}
featureType = data . type ( ) ;
featureType = data . type ( ) ;
distType = _distType ;
distType = _distType ;
@ -374,29 +391,29 @@ void Index::build(InputArray _data, const IndexParams& params, flann_distance_t
switch ( distType )
switch ( distType )
{
{
case FLANN_DIST_HAMMING :
case FLANN_DIST_HAMMING :
buildIndex < HammingDistance > ( index , data , params ) ;
buildIndex < HammingDistance > ( index , _wholedata . getMat ( ) , data , params ) ;
break ;
break ;
case FLANN_DIST_L2 :
case FLANN_DIST_L2 :
buildIndex < : : cvflann : : L2 < float > > ( index , data , params ) ;
buildIndex < : : cvflann : : L2 < float > > ( index , _wholedata . getMat ( ) , data , params ) ;
break ;
break ;
case FLANN_DIST_L1 :
case FLANN_DIST_L1 :
buildIndex < : : cvflann : : L1 < float > > ( index , data , params ) ;
buildIndex < : : cvflann : : L1 < float > > ( index , _wholedata . getMat ( ) , data , params ) ;
break ;
break ;
# if MINIFLANN_SUPPORT_EXOTIC_DISTANCE_TYPES
# if MINIFLANN_SUPPORT_EXOTIC_DISTANCE_TYPES
case FLANN_DIST_MAX :
case FLANN_DIST_MAX :
buildIndex < : : cvflann : : MaxDistance < float > > ( index , data , params ) ;
buildIndex < : : cvflann : : MaxDistance < float > > ( index , _wholedata . getMat ( ) , data , params ) ;
break ;
break ;
case FLANN_DIST_HIST_INTERSECT :
case FLANN_DIST_HIST_INTERSECT :
buildIndex < : : cvflann : : HistIntersectionDistance < float > > ( index , data , params ) ;
buildIndex < : : cvflann : : HistIntersectionDistance < float > > ( index , _wholedata . getMat ( ) , data , params ) ;
break ;
break ;
case FLANN_DIST_HELLINGER :
case FLANN_DIST_HELLINGER :
buildIndex < : : cvflann : : HellingerDistance < float > > ( index , data , params ) ;
buildIndex < : : cvflann : : HellingerDistance < float > > ( index , _wholedata . getMat ( ) , data , params ) ;
break ;
break ;
case FLANN_DIST_CHI_SQUARE :
case FLANN_DIST_CHI_SQUARE :
buildIndex < : : cvflann : : ChiSquareDistance < float > > ( index , data , params ) ;
buildIndex < : : cvflann : : ChiSquareDistance < float > > ( index , _wholedata . getMat ( ) , data , params ) ;
break ;
break ;
case FLANN_DIST_KL :
case FLANN_DIST_KL :
buildIndex < : : cvflann : : KL_Divergence < float > > ( index , data , params ) ;
buildIndex < : : cvflann : : KL_Divergence < float > > ( index , _wholedata . getMat ( ) , data , params ) ;
break ;
break ;
# endif
# endif
default :
default :