• Roman Donchenko's avatar
    Merge remote-tracking branch 'origin/2.4' into merge-2.4 · 6b5ac42a
    Roman Donchenko authored
    Conflicts:
    	modules/calib3d/doc/camera_calibration_and_3d_reconstruction.rst
    	modules/features2d/doc/common_interfaces_of_descriptor_extractors.rst
    	modules/features2d/doc/object_categorization.rst
    	modules/gpu/doc/camera_calibration_and_3d_reconstruction.rst
    	modules/gpu/doc/image_filtering.rst
    	modules/gpu/doc/image_processing.rst
    	modules/gpu/doc/video.rst
    	modules/imgproc/doc/miscellaneous_transformations.rst
    	modules/imgproc/doc/object_detection.rst
    	modules/imgproc/doc/structural_analysis_and_shape_descriptors.rst
    	modules/imgproc/src/samplers.cpp
    	modules/ml/doc/k_nearest_neighbors.rst
    	modules/nonfree/doc/feature_detection.rst
    	modules/ocl/include/opencv2/ocl/ocl.hpp
    	modules/photo/doc/inpainting.rst
    	modules/ts/include/opencv2/ts.hpp
    	platforms/scripts/camera_build.conf
    	samples/android/camera-calibration/AndroidManifest.xml
    6b5ac42a
common_interfaces_of_descriptor_matchers.rst 13.6 KB

Common Interfaces of Descriptor Matchers

Matchers of keypoint descriptors in OpenCV have wrappers with a common interface that enables you to easily switch between different algorithms solving the same problem. This section is devoted to matching descriptors that are represented as vectors in a multidimensional space. All objects that implement vector descriptor matchers inherit the :ocv:class:`DescriptorMatcher` interface.

Note

  • An example explaining keypoint matching can be found at opencv_source_code/samples/cpp/descriptor_extractor_matcher.cpp
  • An example on descriptor matching evaluation can be found at opencv_source_code/samples/cpp/detector_descriptor_matcher_evaluation.cpp
  • An example on one to many image matching can be found at opencv_source_code/samples/cpp/matching_to_many_images.cpp

DescriptorMatcher

Abstract base class for matching keypoint descriptors. It has two groups of match methods: for matching descriptors of an image with another image or with an image set.

class DescriptorMatcher
{
public:
    virtual ~DescriptorMatcher();

    virtual void add( const vector<Mat>& descriptors );

    const vector<Mat>& getTrainDescriptors() const;
    virtual void clear();
    bool empty() const;
    virtual bool isMaskSupported() const = 0;

    virtual void train();

    /*
     * 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;
    /*
     * 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>() );
    void knnMatch( const Mat& queryDescriptors, vector<vector<DMatch> >& matches,
                   int k, const vector<Mat>& masks=vector<Mat>(),
                   bool compactResult=false );
    void radiusMatch( const Mat& queryDescriptors, vector<vector<DMatch> >& matches,
                      float maxDistance, const vector<Mat>& masks=vector<Mat>(),
                      bool compactResult=false );

    virtual void read( const FileNode& );
    virtual void write( FileStorage& ) const;

    virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const = 0;

    static Ptr<DescriptorMatcher> create( const String& descriptorMatcherType );

protected:
    vector<Mat> trainDescCollection;
    ...
};

DescriptorMatcher::add

Adds descriptors to train a descriptor collection. If the collection trainDescCollectionis is not empty, the new descriptors are added to existing train descriptors.

DescriptorMatcher::getTrainDescriptors

Returns a constant link to the train descriptor collection trainDescCollection .

DescriptorMatcher::clear

Clears the train descriptor collection.

DescriptorMatcher::empty

Returns true if there are no train descriptors in the collection.

DescriptorMatcher::isMaskSupported

Returns true if the descriptor matcher supports masking permissible matches.

DescriptorMatcher::train

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 ).

DescriptorMatcher::match

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

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.

DescriptorMatcher::knnMatch

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

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

DescriptorMatcher::radiusMatch

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

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.

DescriptorMatcher::clone

Clones the matcher.

DescriptorMatcher::create

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

BFMatcher

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 of descriptor sets.

BFMatcher::BFMatcher

Brute-force matcher constructor.

FlannBasedMatcher

Flann-based descriptor matcher. This matcher trains :ocv:class:`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.

class FlannBasedMatcher : public DescriptorMatcher
{
public:
    FlannBasedMatcher(
      const Ptr<flann::IndexParams>& indexParams=new flann::KDTreeIndexParams(),
      const Ptr<flann::SearchParams>& searchParams=new flann::SearchParams() );

    virtual void add( const vector<Mat>& descriptors );
    virtual void clear();

    virtual void train();
    virtual bool isMaskSupported() const;

    virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const;
protected:
    ...
};