common_interfaces_of_generic_descriptor_matchers.rst 16 KB
Newer Older
1 2 3 4 5
Common Interfaces of Generic Descriptor Matchers
================================================

.. highlight:: cpp

6
Matchers of keypoint descriptors in OpenCV have wrappers with a common interface that enables you to easily switch 
7
between different algorithms solving the same problem. This section is devoted to matching descriptors
8 9
that cannot be represented as vectors in a multidimensional space. ``GenericDescriptorMatcher`` is a more generic interface for descriptors. It does not make any assumptions about descriptor representation.
Every descriptor with the
10 11
:ocv:class:`DescriptorExtractor` interface has a wrapper with the ``GenericDescriptorMatcher`` interface (see
:ocv:class:`VectorDescriptorMatcher` ).
12
There are descriptors such as the One-way descriptor and Ferns that have the ``GenericDescriptorMatcher`` interface implemented but do not support ``DescriptorExtractor``.
13

14

15 16 17

GenericDescriptorMatcher
------------------------
18
.. ocv:class:: GenericDescriptorMatcher
19

20
Abstract interface for extracting and matching a keypoint descriptor. There are also :ocv:class:`DescriptorExtractor` and :ocv:class:`DescriptorMatcher` for these purposes but their interfaces are intended for descriptors represented as vectors in a multidimensional space. ``GenericDescriptorMatcher`` is a more generic interface for descriptors. ``DescriptorMatcher`` and ``GenericDescriptorMatcher`` have two groups of match methods: for matching keypoints of an image with another image or with an image set. ::
21 22 23 24 25 26

    class GenericDescriptorMatcher
    {
    public:
        GenericDescriptorMatcher();
        virtual ~GenericDescriptorMatcher();
27

28 29
        virtual void add( const vector<Mat>& images,
                          vector<vector<KeyPoint> >& keypoints );
30

31 32 33
        const vector<Mat>& getTrainImages() const;
        const vector<vector<KeyPoint> >& getTrainKeypoints() const;
        virtual void clear();
34

35
        virtual void train() = 0;
36

37
        virtual bool isMaskSupported() = 0;
38 39

        void classify( const Mat& queryImage,
40
                       vector<KeyPoint>& queryKeypoints,
41
                       const Mat& trainImage,
42
                       vector<KeyPoint>& trainKeypoints ) const;
43
        void classify( const Mat& queryImage,
44
                       vector<KeyPoint>& queryKeypoints );
45

46
        /*
47
         * Group of methods to match keypoints from an image pair.
48 49 50 51 52 53
         */
        void match( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
                    const Mat& trainImage, vector<KeyPoint>& trainKeypoints,
                    vector<DMatch>& matches, const Mat& mask=Mat() ) const;
        void knnMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
                       const Mat& trainImage, vector<KeyPoint>& trainKeypoints,
54
                       vector<vector<DMatch> >& matches, int k,
55 56 57
                       const Mat& mask=Mat(), bool compactResult=false ) const;
        void radiusMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
                          const Mat& trainImage, vector<KeyPoint>& trainKeypoints,
58
                          vector<vector<DMatch> >& matches, float maxDistance,
59 60
                          const Mat& mask=Mat(), bool compactResult=false ) const;
        /*
61
         * Group of methods to match keypoints from one image to an image set.
62 63 64 65
         */
        void match( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
                    vector<DMatch>& matches, const vector<Mat>& masks=vector<Mat>() );
        void knnMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
66
                       vector<vector<DMatch> >& matches, int k,
67 68
                       const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
        void radiusMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
69
                          vector<vector<DMatch> >& matches, float maxDistance,
70
                          const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
71

72 73
        virtual void read( const FileNode& );
        virtual void write( FileStorage& ) const;
74

75
        virtual Ptr<GenericDescriptorMatcher> clone( bool emptyTrainData=false ) const = 0;
76

77 78 79
    protected:
        ...
    };
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
80

81

82

83

84
GenericDescriptorMatcher::add
85
---------------------------------
86
Adds images and their keypoints to the training collection stored in the class instance.
87

88
.. ocv:function:: void GenericDescriptorMatcher::add( const vector<Mat>& images,                        vector<vector<KeyPoint> >& keypoints )
89

90
    :param images: Image collection.
91

92
    :param keypoints: Point collection. It is assumed that ``keypoints[i]``  are keypoints detected in the image  ``images[i]`` .
93

94

95

96
GenericDescriptorMatcher::getTrainImages
97
--------------------------------------------
98 99
Returns a train image collection.

100
.. ocv:function:: const vector<Mat>& GenericDescriptorMatcher::getTrainImages() const
101 102 103



104
GenericDescriptorMatcher::getTrainKeypoints
105
-----------------------------------------------
106 107
Returns a train keypoints collection.

108
.. ocv:function:: const vector<vector<KeyPoint> >&  GenericDescriptorMatcher::getTrainKeypoints() const
109 110 111



112
GenericDescriptorMatcher::clear
113
-----------------------------------
114 115
Clears a train collection (images and keypoints).

116
.. ocv:function:: void GenericDescriptorMatcher::clear()
117 118 119



120
GenericDescriptorMatcher::train
121
-----------------------------------
122 123
Trains descriptor matcher

124
.. ocv:function:: void GenericDescriptorMatcher::train()
125

126
Prepares descriptor matcher, for example, creates a tree-based structure, to extract descriptors or to optimize descriptors matching.
127 128


129
GenericDescriptorMatcher::isMaskSupported
130
---------------------------------------------
131 132
Returns ``true`` if a generic descriptor matcher supports masking permissible matches.

133
.. ocv:function:: void GenericDescriptorMatcher::isMaskSupported()
134 135 136



137
GenericDescriptorMatcher::classify
138
--------------------------------------
139 140
Classifies keypoints from a query set.

141
.. ocv:function:: void GenericDescriptorMatcher::classify(  const Mat& queryImage,           vector<KeyPoint>& queryKeypoints,           const Mat& trainImage,           vector<KeyPoint>& trainKeypoints ) const
142

143
.. ocv:function:: void GenericDescriptorMatcher::classify( const Mat& queryImage,           vector<KeyPoint>& queryKeypoints )
144

145
    :param queryImage: Query image.
146

147
    :param queryKeypoints: Keypoints from a query image.
148

149
    :param trainImage: Train image.
150

151
    :param trainKeypoints: Keypoints from a train image.
152

153
The method classifies each keypoint from a query set. The first variant of the method takes a train image and its keypoints as an input argument. The second variant uses the internally stored training collection that can be built using the ``GenericDescriptorMatcher::add`` method.
154
    
155
The methods do the following:
156
    
157 158
#.
    Call the ``GenericDescriptorMatcher::match`` method to find correspondence between the query set and the training set.
159
        
160 161
#.
    Set the ``class_id`` field of each keypoint from the query set to ``class_id`` of the corresponding keypoint from the training set.
162

163

164

165
GenericDescriptorMatcher::match
166
-----------------------------------
167
Finds the best match in the training set for each keypoint from the query set.
168

169
.. ocv:function:: void GenericDescriptorMatcher::match( const Mat& queryImage, vector<KeyPoint>& queryKeypoints, const Mat& trainImage, vector<KeyPoint>& trainKeypoints, vector<DMatch>& matches, const Mat& mask=Mat() ) const
170

171
.. ocv:function:: void GenericDescriptorMatcher::match( const Mat& queryImage, vector<KeyPoint>& queryKeypoints, vector<DMatch>& matches, const vector<Mat>& masks=vector<Mat>() )
172

173
    :param queryImage: Query image.
174

175
    :param queryKeypoints: Keypoints detected in  ``queryImage`` .
176

177
    :param trainImage: Train image. It is not added to a train image collection  stored in the class object.
178

179
    :param trainKeypoints: Keypoints detected in  ``trainImage`` . They are not added to a train points collection stored in the class object.
180

181
    :param matches: Matches. If a query descriptor (keypoint) is masked out in  ``mask`` ,  match is added for this descriptor. So,  ``matches``  size may be smaller than the query keypoints count.
182

183
    :param mask: Mask specifying permissible matches between an input query and train keypoints.
184

185
    :param masks: Set of masks. Each  ``masks[i]``  specifies permissible matches between input query keypoints and stored train keypoints from the i-th image.
186

187
The methods find the best match for each query keypoint. In the first variant of the method, a train image and its keypoints are the input arguments. In the second variant, query keypoints are matched to the internally stored training collection that can be built using the ``GenericDescriptorMatcher::add`` method.     Optional mask (or masks) can be passed to specify which query and training descriptors can be matched. Namely, ``queryKeypoints[i]`` can be matched with ``trainKeypoints[j]`` only if ``mask.at<uchar>(i,j)`` is non-zero.
188

189

190

191
GenericDescriptorMatcher::knnMatch
192
--------------------------------------
193 194
Finds the ``k`` best matches for each query keypoint.

195
.. ocv:function:: void GenericDescriptorMatcher::knnMatch(           const Mat& queryImage, vector<KeyPoint>& queryKeypoints,      const Mat& trainImage, vector<KeyPoint>& trainKeypoints,      vector<vector<DMatch> >& matches, int k,       const Mat& mask=Mat(), bool compactResult=false ) const
196

197
.. ocv:function:: void GenericDescriptorMatcher::knnMatch(           const Mat& queryImage, vector<KeyPoint>& queryKeypoints,      vector<vector<DMatch> >& matches, int k,       const vector<Mat>& masks=vector<Mat>(),       bool compactResult=false )
198
    
199
The methods are extended variants of ``GenericDescriptorMatch::match``. The parameters are similar, and the  the semantics is similar to ``DescriptorMatcher::knnMatch``. But this class does not require explicitly computed keypoint descriptors.
200

201

202

203
GenericDescriptorMatcher::radiusMatch
204
-----------------------------------------
205 206
For each query keypoint, finds the training keypoints not farther than the specified distance.

207
.. ocv:function:: void GenericDescriptorMatcher::radiusMatch(           const Mat& queryImage, vector<KeyPoint>& queryKeypoints,      const Mat& trainImage, vector<KeyPoint>& trainKeypoints,      vector<vector<DMatch> >& matches, float maxDistance,       const Mat& mask=Mat(), bool compactResult=false ) const
208

209
.. ocv:function:: void GenericDescriptorMatcher::radiusMatch(           const Mat& queryImage, vector<KeyPoint>& queryKeypoints,      vector<vector<DMatch> >& matches, float maxDistance,       const vector<Mat>& masks=vector<Mat>(),       bool compactResult=false )
210

211
The methods are similar to ``DescriptorMatcher::radius``. But this class does not require explicitly computed keypoint descriptors.
212

213

214

215
GenericDescriptorMatcher::read
216
----------------------------------
217 218
Reads a matcher object from a file node.

219
.. ocv:function:: void GenericDescriptorMatcher::read( const FileNode& fn )
220 221 222



223
GenericDescriptorMatcher::write
224
-----------------------------------
225
Writes a match object to a file storage.
226

227
.. ocv:function:: void GenericDescriptorMatcher::write( FileStorage& fs ) const
228 229


230
GenericDescriptorMatcher::clone
231
-----------------------------------
232
Clones the matcher.
233

234
.. ocv:function:: Ptr<GenericDescriptorMatcher> GenericDescriptorMatcher::clone( bool emptyTrainData ) const
235

236 237
    :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
238
            but with empty train data.
239 240 241 242


OneWayDescriptorMatcher
-----------------------
243
.. ocv:class:: OneWayDescriptorMatcher
244

245
Wrapping class for computing, matching, and classifying descriptors using the
246
:ocv:class:`OneWayDescriptorBase` class. ::
247 248 249 250 251 252 253 254 255 256 257 258 259

    class OneWayDescriptorMatcher : public GenericDescriptorMatcher
    {
    public:
        class Params
        {
        public:
            static const int POSE_COUNT = 500;
            static const int PATCH_WIDTH = 24;
            static const int PATCH_HEIGHT = 24;
            static float GET_MIN_SCALE() { return 0.7f; }
            static float GET_MAX_SCALE() { return 1.5f; }
            static float GET_STEP_SCALE() { return 1.2f; }
260

261 262 263 264 265 266
            Params( int poseCount = POSE_COUNT,
                    Size patchSize = Size(PATCH_WIDTH, PATCH_HEIGHT),
                    string pcaFilename = string(),
                    string trainPath = string(), string trainImagesList = string(),
                    float minScale = GET_MIN_SCALE(), float maxScale = GET_MAX_SCALE(),
                    float stepScale = GET_STEP_SCALE() );
267

268 269 270 271 272
            int poseCount;
            Size patchSize;
            string pcaFilename;
            string trainPath;
            string trainImagesList;
273

274 275
            float minScale, maxScale, stepScale;
        };
276

277 278
        OneWayDescriptorMatcher( const Params& params=Params() );
        virtual ~OneWayDescriptorMatcher();
279

280
        void initialize( const Params& params, const Ptr<OneWayDescriptorBase>& base=Ptr<OneWayDescriptorBase>() );
281

282
        // Clears keypoints stored in collection and OneWayDescriptorBase
283
        virtual void clear();
284

285
        virtual void train();
286

287
        virtual bool isMaskSupported();
288

289 290
        virtual void read( const FileNode &fn );
        virtual void write( FileStorage& fs ) const;
291

292 293 294 295 296 297
        virtual Ptr<GenericDescriptorMatcher> clone( bool emptyTrainData=false ) const;
    protected:
        ...
    };


298

299 300 301

FernDescriptorMatcher
---------------------
302
.. ocv:class:: FernDescriptorMatcher
303

304
Wrapping class for computing, matching, and classifying descriptors using the
305
:ocv:class:`FernClassifier` class. ::
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

    class FernDescriptorMatcher : public GenericDescriptorMatcher
    {
    public:
        class Params
        {
        public:
            Params( int nclasses=0,
                    int patchSize=FernClassifier::PATCH_SIZE,
                    int signatureSize=FernClassifier::DEFAULT_SIGNATURE_SIZE,
                    int nstructs=FernClassifier::DEFAULT_STRUCTS,
                    int structSize=FernClassifier::DEFAULT_STRUCT_SIZE,
                    int nviews=FernClassifier::DEFAULT_VIEWS,
                    int compressionMethod=FernClassifier::COMPRESSION_NONE,
                    const PatchGenerator& patchGenerator=PatchGenerator() );
321

322
            Params( const string& filename );
323

324 325 326 327 328 329 330 331
            int nclasses;
            int patchSize;
            int signatureSize;
            int nstructs;
            int structSize;
            int nviews;
            int compressionMethod;
            PatchGenerator patchGenerator;
332

333 334
            string filename;
        };
335

336 337
        FernDescriptorMatcher( const Params& params=Params() );
        virtual ~FernDescriptorMatcher();
338

339
        virtual void clear();
340

341
        virtual void train();
342

343
        virtual bool isMaskSupported();
344

345 346
        virtual void read( const FileNode &fn );
        virtual void write( FileStorage& fs ) const;
347

348
        virtual Ptr<GenericDescriptorMatcher> clone( bool emptyTrainData=false ) const;
349

350 351 352
    protected:
            ...
    };
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
353

354 355 356 357 358



VectorDescriptorMatcher
-----------------------
359
.. ocv:class:: VectorDescriptorMatcher
360

361
Class used for matching descriptors that can be described as vectors in a finite-dimensional space. ::
362 363 364 365 366 367

    class CV_EXPORTS VectorDescriptorMatcher : public GenericDescriptorMatcher
    {
    public:
        VectorDescriptorMatcher( const Ptr<DescriptorExtractor>& extractor, const Ptr<DescriptorMatcher>& matcher );
        virtual ~VectorDescriptorMatcher();
368

369 370 371 372 373
        virtual void add( const vector<Mat>& imgCollection,
                          vector<vector<KeyPoint> >& pointCollection );
        virtual void clear();
        virtual void train();
        virtual bool isMaskSupported();
374

375 376
        virtual void read( const FileNode& fn );
        virtual void write( FileStorage& fs ) const;
377

378
        virtual Ptr<GenericDescriptorMatcher> clone( bool emptyTrainData=false ) const;
379

380 381 382
    protected:
        ...
    };
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
383

384

385
Example: ::
386

387
    VectorDescriptorMatcher matcher( new SurfDescriptorExtractor,
388
                                     new BruteForceMatcher<L2<float> > );
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
389

390