OpenCV  4.1.1-pre
Open Source Computer Vision
Looking for a C++ dev who knows OpenCV?
I'm looking for work. Hire me!
cv::FlannBasedMatcher Class Reference

Flann-based descriptor matcher. More...

#include <opencv2/features2d.hpp>

Inheritance diagram for cv::FlannBasedMatcher:
Collaboration diagram for cv::FlannBasedMatcher:

Public Types

enum  MatcherType {
  FLANNBASED = 1,
  BRUTEFORCE = 2,
  BRUTEFORCE_L1 = 3,
  BRUTEFORCE_HAMMING = 4,
  BRUTEFORCE_HAMMINGLUT = 5,
  BRUTEFORCE_SL2 = 6
}
 

Public Member Functions

 FlannBasedMatcher (const Ptr< flann::IndexParams > &indexParams=makePtr< flann::KDTreeIndexParams >(), const Ptr< flann::SearchParams > &searchParams=makePtr< flann::SearchParams >())
 
virtual void add (InputArrayOfArrays descriptors) CV_OVERRIDE
 Adds descriptors to train a CPU(trainDescCollectionis) or GPU(utrainDescCollectionis) descriptor collection. More...
 
virtual void clear () CV_OVERRIDE
 Clears the train descriptor collections. More...
 
virtual Ptr< DescriptorMatcherclone (bool emptyTrainData=false) const CV_OVERRIDE
 Clones the matcher. More...
 
virtual bool empty () const CV_OVERRIDE
 Returns true if there are no train descriptors in the both collections. More...
 
virtual String getDefaultName () const
 Returns the algorithm string identifier. More...
 
const std::vector< Mat > & getTrainDescriptors () const
 Returns a constant link to the train descriptor collection trainDescCollection . More...
 
virtual bool isMaskSupported () const CV_OVERRIDE
 Returns true if the descriptor matcher supports masking permissible matches. More...
 
void knnMatch (InputArray queryDescriptors, InputArray trainDescriptors, std::vector< std::vector< DMatch > > &matches, int k, InputArray mask=noArray(), bool compactResult=false) const
 Finds the k best matches for each descriptor from a query set. More...
 
void knnMatch (InputArray queryDescriptors, std::vector< std::vector< DMatch > > &matches, int k, InputArrayOfArrays masks=noArray(), bool compactResult=false)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
void match (InputArray queryDescriptors, InputArray trainDescriptors, std::vector< DMatch > &matches, InputArray mask=noArray()) const
 Finds the best match for each descriptor from a query set. More...
 
void match (InputArray queryDescriptors, std::vector< DMatch > &matches, InputArrayOfArrays masks=noArray())
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
void radiusMatch (InputArray queryDescriptors, InputArray trainDescriptors, std::vector< std::vector< DMatch > > &matches, float maxDistance, InputArray mask=noArray(), bool compactResult=false) const
 For each query descriptor, finds the training descriptors not farther than the specified distance. More...
 
void radiusMatch (InputArray queryDescriptors, std::vector< std::vector< DMatch > > &matches, float maxDistance, InputArrayOfArrays masks=noArray(), bool compactResult=false)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
void read (const String &fileName)
 
virtual void read (const FileNode &) CV_OVERRIDE
 Reads algorithm parameters from a file storage. More...
 
virtual void save (const String &filename) const
 Saves the algorithm to a file. More...
 
virtual void train () CV_OVERRIDE
 Trains a descriptor matcher. More...
 
void write (const String &fileName) const
 
void write (const Ptr< FileStorage > &fs, const String &name=String()) const
 
virtual void write (FileStorage &) const CV_OVERRIDE
 Stores algorithm parameters in a file storage. More...
 

Static Public Member Functions

static Ptr< DescriptorMatchercreate (const String &descriptorMatcherType)
 Creates a descriptor matcher of a given type with the default parameters (using default constructor). More...
 
static Ptr< DescriptorMatchercreate (const DescriptorMatcher::MatcherType &matcherType)
 
static Ptr< FlannBasedMatchercreate ()
 
template<typename _Tp >
static Ptr< _Tp > load (const String &filename, const String &objname=String())
 Loads algorithm from the file. More...
 
template<typename _Tp >
static Ptr< _Tp > loadFromString (const String &strModel, const String &objname=String())
 Loads algorithm from a String. More...
 

Protected Member Functions

void checkMasks (InputArrayOfArrays masks, int queryDescriptorsCount) const
 
virtual void knnMatchImpl (InputArray queryDescriptors, std::vector< std::vector< DMatch > > &matches, int k, InputArrayOfArrays masks=noArray(), bool compactResult=false) CV_OVERRIDE
 In fact the matching is implemented only by the following two methods. More...
 
virtual void radiusMatchImpl (InputArray queryDescriptors, std::vector< std::vector< DMatch > > &matches, float maxDistance, InputArrayOfArrays masks=noArray(), bool compactResult=false) CV_OVERRIDE
 
void writeFormat (FileStorage &fs) const
 

Static Protected Member Functions

static Mat clone_op (Mat m)
 
static void convertToDMatches (const DescriptorCollection &descriptors, const Mat &indices, const Mat &distances, std::vector< std::vector< DMatch > > &matches)
 
static bool isMaskedOut (InputArrayOfArrays masks, int queryIdx)
 
static bool isPossibleMatch (InputArray mask, int queryIdx, int trainIdx)
 

Protected Attributes

int addedDescCount
 
Ptr< flann::IndexflannIndex
 
Ptr< flann::IndexParamsindexParams
 
DescriptorCollection mergedDescriptors
 
Ptr< flann::SearchParamssearchParams
 
std::vector< MattrainDescCollection
 Collection of descriptors from train images. More...
 
std::vector< UMatutrainDescCollection
 

Detailed Description

Flann-based descriptor matcher.

This matcher trains cv::flann::Index on a train descriptor collection and calls its nearest search methods to find the best matches. So, this matcher may be faster when matching a large train collection than the brute force matcher. FlannBasedMatcher does not support masking permissible matches of descriptor sets because flann::Index does not support this. :

Member Enumeration Documentation

◆ MatcherType

Enumerator
FLANNBASED 
BRUTEFORCE 
BRUTEFORCE_L1 
BRUTEFORCE_HAMMING 
BRUTEFORCE_HAMMINGLUT 
BRUTEFORCE_SL2 

Constructor & Destructor Documentation

◆ FlannBasedMatcher()

cv::FlannBasedMatcher::FlannBasedMatcher ( const Ptr< flann::IndexParams > &  indexParams = makePtrflann::KDTreeIndexParams >(),
const Ptr< flann::SearchParams > &  searchParams = makePtrflann::SearchParams >() 
)

Member Function Documentation

◆ add()

virtual void cv::FlannBasedMatcher::add ( InputArrayOfArrays  descriptors)
virtual

Adds descriptors to train a CPU(trainDescCollectionis) or GPU(utrainDescCollectionis) descriptor collection.

If the collection is not empty, the new descriptors are added to existing train descriptors.

Parameters
descriptorsDescriptors to add. Each descriptors[i] is a set of descriptors from the same train image.

Reimplemented from cv::DescriptorMatcher.

◆ checkMasks()

void cv::DescriptorMatcher::checkMasks ( InputArrayOfArrays  masks,
int  queryDescriptorsCount 
) const
protectedinherited

◆ clear()

virtual void cv::FlannBasedMatcher::clear ( )
virtual

Clears the train descriptor collections.

Reimplemented from cv::DescriptorMatcher.

◆ clone()

virtual Ptr<DescriptorMatcher> cv::FlannBasedMatcher::clone ( bool  emptyTrainData = false) const
virtual

Clones the matcher.

Parameters
emptyTrainDataIf emptyTrainData is false, the method creates a deep copy of the object, that is, copies both parameters and train data. If emptyTrainData is true, the method creates an object copy with the current parameters but with empty train data.

Implements cv::DescriptorMatcher.

◆ clone_op()

static Mat cv::DescriptorMatcher::clone_op ( Mat  m)
inlinestaticprotectedinherited

References cv::Mat::clone().

Here is the call graph for this function:

◆ convertToDMatches()

static void cv::FlannBasedMatcher::convertToDMatches ( const DescriptorCollection &  descriptors,
const Mat indices,
const Mat distances,
std::vector< std::vector< DMatch > > &  matches 
)
staticprotected

◆ create() [1/3]

static Ptr<DescriptorMatcher> cv::DescriptorMatcher::create ( const String descriptorMatcherType)
staticinherited

Creates a descriptor matcher of a given type with the default parameters (using default constructor).

Parameters
descriptorMatcherTypeDescriptor matcher type. Now the following matcher types are supported:
  • BruteForce (it uses L2 )
  • BruteForce-L1
  • BruteForce-Hamming
  • BruteForce-Hamming(2)
  • FlannBased

◆ create() [2/3]

static Ptr<DescriptorMatcher> cv::DescriptorMatcher::create ( const DescriptorMatcher::MatcherType matcherType)
staticinherited

◆ create() [3/3]

static Ptr<FlannBasedMatcher> cv::FlannBasedMatcher::create ( )
static

◆ empty()

virtual bool cv::DescriptorMatcher::empty ( ) const
virtualinherited

Returns true if there are no train descriptors in the both collections.

Reimplemented from cv::Algorithm.

◆ getDefaultName()

virtual String cv::Algorithm::getDefaultName ( ) const
virtualinherited

Returns the algorithm string identifier.

This string is used as top level xml/yml node tag when the object is saved to a file or string.

Reimplemented in cv::AKAZE, cv::KAZE, cv::SimpleBlobDetector, cv::GFTTDetector, cv::AgastFeatureDetector, cv::FastFeatureDetector, cv::MSER, cv::ORB, cv::BRISK, and cv::Feature2D.

◆ getTrainDescriptors()

const std::vector<Mat>& cv::DescriptorMatcher::getTrainDescriptors ( ) const
inherited

Returns a constant link to the train descriptor collection trainDescCollection .

◆ isMaskedOut()

static bool cv::DescriptorMatcher::isMaskedOut ( InputArrayOfArrays  masks,
int  queryIdx 
)
staticprotectedinherited

◆ isMaskSupported()

virtual bool cv::FlannBasedMatcher::isMaskSupported ( ) const
virtual

Returns true if the descriptor matcher supports masking permissible matches.

Implements cv::DescriptorMatcher.

◆ isPossibleMatch()

static bool cv::DescriptorMatcher::isPossibleMatch ( InputArray  mask,
int  queryIdx,
int  trainIdx 
)
staticprotectedinherited

◆ knnMatch() [1/2]

void cv::DescriptorMatcher::knnMatch ( InputArray  queryDescriptors,
InputArray  trainDescriptors,
std::vector< std::vector< DMatch > > &  matches,
int  k,
InputArray  mask = noArray(),
bool  compactResult = false 
) const
inherited

Finds the k best matches for each descriptor from a query set.

Parameters
queryDescriptorsQuery set of descriptors.
trainDescriptorsTrain set of descriptors. This set is not added to the train descriptors collection stored in the class object.
maskMask specifying permissible matches between an input query and train matrices of descriptors.
matchesMatches. Each matches[i] is k or less matches for the same query descriptor.
kCount of best matches found per each query descriptor or less if a query descriptor has less than k possible matches in total.
compactResultParameter used when the mask (or masks) is not empty. If compactResult is false, the matches vector has the same size as queryDescriptors rows. If compactResult is true, the matches vector does not contain matches for fully masked-out query descriptors.

These extended variants of DescriptorMatcher::match methods find several best matches for each query descriptor. The matches are returned in the distance increasing order. See DescriptorMatcher::match for the details about query and train descriptors.

◆ knnMatch() [2/2]

void cv::DescriptorMatcher::knnMatch ( InputArray  queryDescriptors,
std::vector< std::vector< DMatch > > &  matches,
int  k,
InputArrayOfArrays  masks = noArray(),
bool  compactResult = false 
)
inherited

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
queryDescriptorsQuery set of descriptors.
matchesMatches. Each matches[i] is k or less matches for the same query descriptor.
kCount of best matches found per each query descriptor or less if a query descriptor has less than k possible matches in total.
masksSet of masks. Each masks[i] specifies permissible matches between the input query descriptors and stored train descriptors from the i-th image trainDescCollection[i].
compactResultParameter used when the mask (or masks) is not empty. If compactResult is false, the matches vector has the same size as queryDescriptors rows. If compactResult is true, the matches vector does not contain matches for fully masked-out query descriptors.

◆ knnMatchImpl()

virtual void cv::FlannBasedMatcher::knnMatchImpl ( InputArray  queryDescriptors,
std::vector< std::vector< DMatch > > &  matches,
int  k,
InputArrayOfArrays  masks = noArray(),
bool  compactResult = false 
)
protectedvirtual

In fact the matching is implemented only by the following two methods.

These methods suppose that the class object has been trained already. Public match methods call these methods after calling train().

Implements cv::DescriptorMatcher.

◆ load()

template<typename _Tp >
static Ptr<_Tp> cv::Algorithm::load ( const String filename,
const String objname = String() 
)
inlinestaticinherited

Loads algorithm from the file.

Parameters
filenameName of the file to read.
objnameThe optional name of the node to read (if empty, the first top-level node will be used)

This is static template method of Algorithm. It's usage is following (in the case of SVM):

Ptr<SVM> svm = Algorithm::load<SVM>("my_svm_model.xml");

In order to make this method work, the derived class must overwrite Algorithm::read(const FileNode& fn).

References CV_Assert, cv::FileNode::empty(), cv::FileStorage::getFirstTopLevelNode(), cv::FileStorage::isOpened(), and cv::FileStorage::READ.

Here is the call graph for this function:

◆ loadFromString()

template<typename _Tp >
static Ptr<_Tp> cv::Algorithm::loadFromString ( const String strModel,
const String objname = String() 
)
inlinestaticinherited

Loads algorithm from a String.

Parameters
strModelThe string variable containing the model you want to load.
objnameThe optional name of the node to read (if empty, the first top-level node will be used)

This is static template method of Algorithm. It's usage is following (in the case of SVM):

Ptr<SVM> svm = Algorithm::loadFromString<SVM>(myStringModel);

References CV_WRAP, cv::FileNode::empty(), cv::FileStorage::getFirstTopLevelNode(), cv::FileStorage::MEMORY, and cv::FileStorage::READ.

Here is the call graph for this function:

◆ match() [1/2]

void cv::DescriptorMatcher::match ( InputArray  queryDescriptors,
InputArray  trainDescriptors,
std::vector< DMatch > &  matches,
InputArray  mask = noArray() 
) const
inherited

Finds the best match for each descriptor from a query set.

Parameters
queryDescriptorsQuery set of descriptors.
trainDescriptorsTrain set of descriptors. This set is not added to the train descriptors collection stored in the class object.
matchesMatches. If a query descriptor is masked out in mask , no match is added for this descriptor. So, matches size may be smaller than the query descriptors count.
maskMask specifying permissible matches between an input query and train matrices of descriptors.

In the first variant of this method, the train descriptors are passed as an input argument. In the second variant of the method, train descriptors collection that was set by DescriptorMatcher::add is used. Optional mask (or masks) can be passed to specify which query and training descriptors can be matched. Namely, queryDescriptors[i] can be matched with trainDescriptors[j] only if mask.at<uchar>(i,j) is non-zero.

◆ match() [2/2]

void cv::DescriptorMatcher::match ( InputArray  queryDescriptors,
std::vector< DMatch > &  matches,
InputArrayOfArrays  masks = noArray() 
)
inherited

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
queryDescriptorsQuery set of descriptors.
matchesMatches. If a query descriptor is masked out in mask , no match is added for this descriptor. So, matches size may be smaller than the query descriptors count.
masksSet of masks. Each masks[i] specifies permissible matches between the input query descriptors and stored train descriptors from the i-th image trainDescCollection[i].

◆ radiusMatch() [1/2]

void cv::DescriptorMatcher::radiusMatch ( InputArray  queryDescriptors,
InputArray  trainDescriptors,
std::vector< std::vector< DMatch > > &  matches,
float  maxDistance,
InputArray  mask = noArray(),
bool  compactResult = false 
) const
inherited

For each query descriptor, finds the training descriptors not farther than the specified distance.

Parameters
queryDescriptorsQuery set of descriptors.
trainDescriptorsTrain set of descriptors. This set is not added to the train descriptors collection stored in the class object.
matchesFound matches.
compactResultParameter used when the mask (or masks) is not empty. If compactResult is false, the matches vector has the same size as queryDescriptors rows. If compactResult is true, the matches vector does not contain matches for fully masked-out query descriptors.
maxDistanceThreshold for the distance between matched descriptors. Distance means here metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured in Pixels)!
maskMask specifying permissible matches between an input query and train matrices of descriptors.

For each query descriptor, the methods find such training descriptors that the distance between the query descriptor and the training descriptor is equal or smaller than maxDistance. Found matches are returned in the distance increasing order.

◆ radiusMatch() [2/2]

void cv::DescriptorMatcher::radiusMatch ( InputArray  queryDescriptors,
std::vector< std::vector< DMatch > > &  matches,
float  maxDistance,
InputArrayOfArrays  masks = noArray(),
bool  compactResult = false 
)
inherited

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
queryDescriptorsQuery set of descriptors.
matchesFound matches.
maxDistanceThreshold for the distance between matched descriptors. Distance means here metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured in Pixels)!
masksSet of masks. Each masks[i] specifies permissible matches between the input query descriptors and stored train descriptors from the i-th image trainDescCollection[i].
compactResultParameter used when the mask (or masks) is not empty. If compactResult is false, the matches vector has the same size as queryDescriptors rows. If compactResult is true, the matches vector does not contain matches for fully masked-out query descriptors.

◆ radiusMatchImpl()

virtual void cv::FlannBasedMatcher::radiusMatchImpl ( InputArray  queryDescriptors,
std::vector< std::vector< DMatch > > &  matches,
float  maxDistance,
InputArrayOfArrays  masks = noArray(),
bool  compactResult = false 
)
protectedvirtual

Implements cv::DescriptorMatcher.

◆ read() [1/2]

void cv::DescriptorMatcher::read ( const String fileName)
inlineinherited

References CV_OVERRIDE, CV_WRAP, cv::FileStorage::READ, and cv::FileStorage::root().

Here is the call graph for this function:

◆ read() [2/2]

virtual void cv::FlannBasedMatcher::read ( const FileNode fn)
virtual

Reads algorithm parameters from a file storage.

Reimplemented from cv::DescriptorMatcher.

◆ save()

virtual void cv::Algorithm::save ( const String filename) const
virtualinherited

Saves the algorithm to a file.

In order to make this method work, the derived class must implement Algorithm::write(FileStorage& fs).

◆ train()

virtual void cv::FlannBasedMatcher::train ( )
virtual

Trains a descriptor matcher.

Trains a descriptor matcher (for example, the flann index). In all methods to match, the method train() is run every time before matching. Some descriptor matchers (for example, BruteForceMatcher) have an empty implementation of this method. Other matchers really train their inner structures (for example, FlannBasedMatcher trains flann::Index ).

Reimplemented from cv::DescriptorMatcher.

◆ write() [1/3]

void cv::DescriptorMatcher::write ( const String fileName) const
inlineinherited

◆ write() [2/3]

void cv::DescriptorMatcher::write ( const Ptr< FileStorage > &  fs,
const String name = String() 
) const
inlineinherited

References cv::Algorithm::write().

Here is the call graph for this function:

◆ write() [3/3]

virtual void cv::FlannBasedMatcher::write ( FileStorage fs) const
virtual

Stores algorithm parameters in a file storage.

Reimplemented from cv::DescriptorMatcher.

◆ writeFormat()

void cv::Algorithm::writeFormat ( FileStorage fs) const
protectedinherited

Member Data Documentation

◆ addedDescCount

int cv::FlannBasedMatcher::addedDescCount
protected

◆ flannIndex

Ptr<flann::Index> cv::FlannBasedMatcher::flannIndex
protected

◆ indexParams

Ptr<flann::IndexParams> cv::FlannBasedMatcher::indexParams
protected

◆ mergedDescriptors

DescriptorCollection cv::FlannBasedMatcher::mergedDescriptors
protected

◆ searchParams

Ptr<flann::SearchParams> cv::FlannBasedMatcher::searchParams
protected

◆ trainDescCollection

std::vector<Mat> cv::DescriptorMatcher::trainDescCollection
protectedinherited

Collection of descriptors from train images.

◆ utrainDescCollection

std::vector<UMat> cv::DescriptorMatcher::utrainDescCollection
protectedinherited

The documentation for this class was generated from the following file: