opencv/modules/features2d/doc/common_interfaces_of_descriptor_matchers.rst

353 lines
14 KiB
ReStructuredText
Raw Normal View History

Common Interfaces of Descriptor Matchers
========================================
.. highlight:: cpp
2011-05-08 10:35:08 +02:00
Matchers of keypoint descriptors in OpenCV have wrappers with a common interface that enables you to easily switch
2011-02-26 12:05:10 +01:00
between different algorithms solving the same problem. This section is devoted to matching descriptors
2011-05-08 10:35:08 +02:00
that are represented as vectors in a multidimensional space. All objects that implement ``vector``
2011-02-26 12:05:10 +01:00
descriptor matchers inherit
2011-05-08 10:35:08 +02:00
:ref:`DescriptorMatcher` interface.
.. index:: DMatch
.. _DMatch:
DMatch
------
.. c:type:: DMatch
2011-05-08 10:35:08 +02:00
Class for matching keypoint descriptors: query descriptor index,
train descriptor index, train image index, and distance between descriptors ::??
struct DMatch
{
2011-02-26 12:05:10 +01:00
DMatch() : queryIdx(-1), trainIdx(-1), imgIdx(-1),
distance(std::numeric_limits<float>::max()) {}
DMatch( int _queryIdx, int _trainIdx, float _distance ) :
2011-02-26 12:05:10 +01:00
queryIdx(_queryIdx), trainIdx(_trainIdx), imgIdx(-1),
distance(_distance) {}
DMatch( int _queryIdx, int _trainIdx, int _imgIdx, float _distance ) :
2011-02-26 12:05:10 +01:00
queryIdx(_queryIdx), trainIdx(_trainIdx), imgIdx(_imgIdx),
distance(_distance) {}
2011-02-26 12:05:10 +01:00
int queryIdx; // query descriptor index
int trainIdx; // train descriptor index
int imgIdx; // train image index
2011-02-26 12:05:10 +01:00
float distance;
2011-02-26 12:05:10 +01:00
// less is better
bool operator<( const DMatch &m ) const;
};
2011-03-03 08:29:55 +01:00
.. index:: DescriptorMatcher
.. _DescriptorMatcher:
DescriptorMatcher
-----------------
.. c:type:: DescriptorMatcher
2011-02-26 12:05:10 +01:00
Abstract base class for matching keypoint descriptors. It has two groups
2011-05-08 10:35:08 +02:00
of match methods: for matching descriptors of an image with another image or
with an image set. ::
class DescriptorMatcher
{
public:
virtual ~DescriptorMatcher();
2011-02-26 12:05:10 +01:00
virtual void add( const vector<Mat>& descriptors );
2011-02-26 12:05:10 +01:00
const vector<Mat>& getTrainDescriptors() const;
virtual void clear();
bool empty() const;
virtual bool isMaskSupported() const = 0;
2011-02-26 12:05:10 +01:00
virtual void train();
2011-02-26 12:05:10 +01:00
/*
2011-05-08 10:35:08 +02:00
* Group of methods to match descriptors from an image pair.
*/
void match( const Mat& queryDescriptors, const Mat& trainDescriptors,
vector<DMatch>& matches, const Mat& mask=Mat() ) const;
void knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors,
vector<vector<DMatch> >& matches, int k,
const Mat& mask=Mat(), bool compactResult=false ) const;
void radiusMatch( const Mat& queryDescriptors, const Mat& trainDescriptors,
vector<vector<DMatch> >& matches, float maxDistance,
const Mat& mask=Mat(), bool compactResult=false ) const;
/*
2011-05-08 10:35:08 +02:00
* Group of methods to match descriptors from one image to an image set.
*/
void match( const Mat& queryDescriptors, vector<DMatch>& matches,
const vector<Mat>& masks=vector<Mat>() );
2011-02-26 12:05:10 +01:00
void knnMatch( const Mat& queryDescriptors, vector<vector<DMatch> >& matches,
int k, const vector<Mat>& masks=vector<Mat>(),
bool compactResult=false );
2011-02-26 12:05:10 +01:00
void radiusMatch( const Mat& queryDescriptors, vector<vector<DMatch> >& matches,
float maxDistance, const vector<Mat>& masks=vector<Mat>(),
bool compactResult=false );
2011-02-26 12:05:10 +01:00
virtual void read( const FileNode& );
virtual void write( FileStorage& ) const;
2011-02-26 12:05:10 +01:00
virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const = 0;
2011-02-26 12:05:10 +01:00
static Ptr<DescriptorMatcher> create( const string& descriptorMatcherType );
2011-02-26 12:05:10 +01:00
protected:
2011-02-26 12:05:10 +01:00
vector<Mat> trainDescCollection;
...
};
2011-03-03 08:29:55 +01:00
.. index:: DescriptorMatcher::add
DescriptorMatcher::add
2011-03-03 08:29:55 +01:00
--------------------------
.. c:function:: void add( const vector<Mat>\& descriptors )
2011-05-08 10:35:08 +02:00
Adds descriptors to train a descriptor collection. If the collection ``trainDescCollectionis`` is not empty, the new descriptors are added to existing train descriptors.
2011-05-08 10:35:08 +02:00
:param descriptors: Descriptors to add. Each ``descriptors[i]`` is a set of descriptors from the same train image.
.. index:: DescriptorMatcher::getTrainDescriptors
DescriptorMatcher::getTrainDescriptors
2011-03-03 08:29:55 +01:00
------------------------------------------
.. c:function:: const vector<Mat>\& getTrainDescriptors() const
2011-05-08 10:35:08 +02:00
Returns a constant link to the train descriptor collection ``trainDescCollection`` .
.. index:: DescriptorMatcher::clear
DescriptorMatcher::clear
----------------------------
.. c:function:: void DescriptorMatcher::clear()
2011-05-08 10:35:08 +02:00
Clears the train descriptor collection.
.. index:: DescriptorMatcher::empty
DescriptorMatcher::empty
----------------------------
.. c:function:: bool DescriptorMatcher::empty() const
2011-05-08 10:35:08 +02:00
Returns true if there are not train descriptors in the collection.
.. index:: DescriptorMatcher::isMaskSupported
DescriptorMatcher::isMaskSupported
--------------------------------------
.. c:function:: bool DescriptorMatcher::isMaskSupported()
2011-05-08 10:35:08 +02:00
Returns true if the descriptor matcher supports masking permissible matches.
.. index:: DescriptorMatcher::train
DescriptorMatcher::train
----------------------------
.. c:function:: void DescriptorMatcher::train()
2011-05-08 10:35:08 +02:00
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`` ).
.. index:: DescriptorMatcher::match
DescriptorMatcher::match
2011-03-03 08:29:55 +01:00
----------------------------
.. c:function:: void DescriptorMatcher::match( const Mat\& queryDescriptors, const Mat\& trainDescriptors, vector<DMatch>\& matches, const Mat\& mask=Mat() ) const
2011-05-08 10:35:08 +02:00
Finds the best match for each descriptor from a query set with train descriptors. Query descriptors are supposed to be of keypoints detected on the same query image. In the first variant of this method, train descriptors are set as an input argument and are supposed to be of keypoints detected on the same train image. In the second variant of the method, train descriptors collection that was set using ``addmethod`` is used. Optional mask (or masks) can be set to describe which descriptors can be matched. ``queryDescriptors[i]`` can be matched with ``trainDescriptors[j]`` only if ``mask.at<uchar>(i,j)`` is non-zero.?? what does "of keypoints" mean?
.. c:function:: void DescriptorMatcher::match( const Mat\& queryDescriptors, vector<DMatch>\& matches, const vector<Mat>\& masks=vector<Mat>() )
2011-02-26 12:05:10 +01:00
:param queryDescriptors: Query set of descriptors.
2011-05-08 10:35:08 +02:00
:param trainDescriptors: Train set of descriptors. This set is not added to the train descriptors collection stored in the class object.
2011-05-08 10:35:08 +02:00
:param matches: Matches. If a query descriptor masked out in ``mask`` , no match is added for this descriptor. So, ``matches`` size may be smaller than the query descriptors count.
2011-05-08 10:35:08 +02:00
:param mask: Mask specifying permissible matches between an input query and train matrices of descriptors.
2011-05-08 10:35:08 +02:00
:param masks: Set of masks. Each ``masks[i]`` specifies permissible matches between input query descriptors and stored train descriptors from the i-th image ``trainDescCollection[i]`` .
.. index:: DescriptorMatcher::knnMatch
DescriptorMatcher::knnMatch
-------------------------------
.. c:function:: void DescriptorMatcher::knnMatch( const Mat\& queryDescriptors, const Mat\& trainDescriptors, vector<vector<DMatch> >\& matches, int k, const Mat\& mask=Mat(), bool compactResult=false ) const
2011-05-08 10:35:08 +02:00
Finds the k best matches for each descriptor from a query set with train descriptors. Found k (or less if not possible) matches are returned in the distance increasing order. See the details about query and train descriptors in ??.
.. c:function:: void DescriptorMatcher::knnMatch( const Mat\& queryDescriptors, vector<vector<DMatch> >\& matches, int k, const vector<Mat>\& masks=vector<Mat>(), bool compactResult=false )
2011-05-08 10:35:08 +02:00
:param queryDescriptors, trainDescriptors, mask, masks: See :ref:`DescriptorMatcher::match` .
2011-02-26 12:05:10 +01:00
:param matches: Mathes. Each ``matches[i]`` is k or less matches for the same query descriptor.
2011-05-08 10:35:08 +02:00
:param k: Count of best matches found per each query descriptor (or less if it is not possible).
2011-05-08 10:35:08 +02:00
:param compactResult: Parameter that is 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.
.. index:: DescriptorMatcher::radiusMatch
DescriptorMatcher::radiusMatch
----------------------------------
:func:`DescriptorMatcher::match`
.. c:function:: void DescriptorMatcher::radiusMatch( const Mat\& queryDescriptors, const Mat\& trainDescriptors, vector<vector<DMatch> >\& matches, float maxDistance, const Mat\& mask=Mat(), bool compactResult=false ) const
2011-05-08 10:35:08 +02:00
Finds the best matches for each query descriptor that has a distance smaller than the given threshold. Found matches are returned in the distance increasing order. See the details about query and train descriptors in ??.
.. c:function:: void DescriptorMatcher::radiusMatch( const Mat\& queryDescriptors, vector<vector<DMatch> >\& matches, float maxDistance, const vector<Mat>\& masks=vector<Mat>(), bool compactResult=false )
2011-05-08 10:35:08 +02:00
:param queryDescriptors, trainDescriptors, mask, masks: See :ref:`DescriptorMatcher::match` .
2011-05-08 10:35:08 +02:00
:param matches, compactResult: See :ref:`DescriptorMatcher::knnMatch` .
2011-05-08 10:35:08 +02:00
:param maxDistance: Threshold for found match distances.
.. index:: DescriptorMatcher::clone
DescriptorMatcher::clone
----------------------------
.. c:function:: Ptr<DescriptorMatcher> \\DescriptorMatcher::clone( bool emptyTrainData ) const
2011-05-08 10:35:08 +02:00
Clones the matcher.
2011-05-08 10:35:08 +02:00
:param emptyTrainData: If ``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.
.. index:: DescriptorMatcher::create
DescriptorMatcher::create
-----------------------------
:func:`DescriptorMatcher`
.. c:function:: Ptr<DescriptorMatcher> DescriptorMatcher::create( const string\& descriptorMatcherType )
2011-05-08 10:35:08 +02:00
Creates a descriptor matcher of a given type with the default parameters (using default constructor).
2011-02-26 12:05:10 +01:00
:param descriptorMatcherType: Descriptor matcher type.
2011-05-08 10:35:08 +02:00
Now the following matcher types are supported: ``"BruteForce"`` (it uses ``L2`` ), ``"BruteForce-L1"`` ,``"BruteForce-Hamming"`` ,``"BruteForce-HammingLUT"`` , and ``"FlannBased"`` .
.. index:: BruteForceMatcher
.. _BruteForceMatcher:
BruteForceMatcher
-----------------
.. c:type:: BruteForceMatcher
2011-03-03 08:29:55 +01:00
Brute-force descriptor matcher. For each descriptor in the first set, this matcher finds the closest descriptor in the second set by trying each one. This descriptor matcher supports masking permissible matches between descriptor sets. ::
template<class Distance>
class BruteForceMatcher : public DescriptorMatcher
{
public:
BruteForceMatcher( Distance d = Distance() );
virtual ~BruteForceMatcher();
2011-02-26 12:05:10 +01:00
virtual bool isMaskSupported() const;
virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const;
protected:
...
}
2011-03-03 08:29:55 +01:00
2011-05-08 10:35:08 +02:00
For efficiency, ``BruteForceMatcher`` is used as a template for the distance metric??. For float descriptors, a common choice is ``L2<float>`` . The following class of supported distances is used: ::
template<typename T>
struct Accumulator
{
typedef T Type;
};
2011-02-26 12:05:10 +01:00
template<> struct Accumulator<unsigned char> { typedef unsigned int Type; };
template<> struct Accumulator<unsigned short> { typedef unsigned int Type; };
template<> struct Accumulator<char> { typedef int Type; };
template<> struct Accumulator<short> { typedef int Type; };
2011-02-26 12:05:10 +01:00
/*
* Squared Euclidean distance functor
*/
template<class T>
struct L2
{
typedef T ValueType;
typedef typename Accumulator<T>::Type ResultType;
2011-02-26 12:05:10 +01:00
ResultType operator()( const T* a, const T* b, int size ) const;
};
2011-02-26 12:05:10 +01:00
/*
* Manhattan distance (city block distance) functor
*/
template<class T>
struct CV_EXPORTS L1
{
typedef T ValueType;
typedef typename Accumulator<T>::Type ResultType;
2011-02-26 12:05:10 +01:00
ResultType operator()( const T* a, const T* b, int size ) const;
...
};
2011-02-26 12:05:10 +01:00
/*
* Hamming distance (city block distance) functor
*/
struct HammingLUT
{
typedef unsigned char ValueType;
typedef int ResultType;
2011-02-26 12:05:10 +01:00
ResultType operator()( const unsigned char* a, const unsigned char* b,
int size ) const;
...
};
2011-02-26 12:05:10 +01:00
struct Hamming
{
typedef unsigned char ValueType;
typedef int ResultType;
2011-02-26 12:05:10 +01:00
ResultType operator()( const unsigned char* a, const unsigned char* b,
int size ) const;
...
};
2011-03-03 08:29:55 +01:00
.. index:: FlannBasedMatcher
.. _FlannBasedMatcher:
FlannBasedMatcher
-----------------
.. c:type:: FlannBasedMatcher
2011-05-08 10:35:08 +02:00
Flann-based descriptor matcher. This matcher trains :ref:`flann::Index` on a train descriptor collection and calls its nearest search methods to find the best matches. So, this matcher may be faster in cases of matching a large train collection than the brute force matcher. ``FlannBasedMatcher`` does not support masking permissible matches between descriptor sets because :ref:`flann::Index` does not support this. ::
class FlannBasedMatcher : public DescriptorMatcher
{
public:
2011-02-26 12:05:10 +01:00
FlannBasedMatcher(
const Ptr<flann::IndexParams>& indexParams=new flann::KDTreeIndexParams(),
const Ptr<flann::SearchParams>& searchParams=new flann::SearchParams() );
2011-02-26 12:05:10 +01:00
virtual void add( const vector<Mat>& descriptors );
virtual void clear();
2011-02-26 12:05:10 +01:00
virtual void train();
virtual bool isMaskSupported() const;
2011-02-26 12:05:10 +01:00
virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const;
protected:
...
};
2011-03-03 08:29:55 +01:00
..