File: common_interfaces_of_generic_descriptor_matchers.rst

package info (click to toggle)
opencv 2.4.9.1%2Bdfsg-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 126,800 kB
  • ctags: 62,729
  • sloc: xml: 509,055; cpp: 490,794; lisp: 23,208; python: 21,174; java: 19,317; ansic: 1,038; sh: 128; makefile: 72
file content (363 lines) | stat: -rw-r--r-- 15,389 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
Common Interfaces of Generic Descriptor Matchers
================================================

.. highlight:: cpp

OneWayDescriptorBase
--------------------
.. ocv:class:: OneWayDescriptorBase

Class encapsulates functionality for training/loading a set of one way descriptors
and finding the nearest closest descriptor to an input feature. ::

    class CV_EXPORTS OneWayDescriptorBase
    {
    public:

        // creates an instance of OneWayDescriptor from a set of training files
        // - patch_size: size of the input (large) patch
        // - pose_count: the number of poses to generate for each descriptor
        // - train_path: path to training files
        // - pca_config: the name of the file that contains PCA for small patches (2 times smaller
        // than patch_size each dimension
        // - pca_hr_config: the name of the file that contains PCA for large patches (of patch_size size)
        // - pca_desc_config: the name of the file that contains descriptors of PCA components
        OneWayDescriptorBase(CvSize patch_size, int pose_count, const char* train_path = 0, const char* pca_config = 0,
                            const char* pca_hr_config = 0, const char* pca_desc_config = 0, int pyr_levels = 1,
                            int pca_dim_high = 100, int pca_dim_low = 100);

        OneWayDescriptorBase(CvSize patch_size, int pose_count, const string &pca_filename, const string &train_path = string(), const string &images_list = string(),
                            float _scale_min = 0.7f, float _scale_max=1.5f, float _scale_step=1.2f, int pyr_levels = 1,
                            int pca_dim_high = 100, int pca_dim_low = 100);


        virtual ~OneWayDescriptorBase();
        void clear ();


        // Allocate: allocates memory for a given number of descriptors
        void Allocate(int train_feature_count);

        // AllocatePCADescriptors: allocates memory for pca descriptors
        void AllocatePCADescriptors();

        // returns patch size
        CvSize GetPatchSize() const {return m_patch_size;};
        // returns the number of poses for each descriptor
        int GetPoseCount() const {return m_pose_count;};

        // returns the number of pyramid levels
        int GetPyrLevels() const {return m_pyr_levels;};

        // returns the number of descriptors
        int GetDescriptorCount() const {return m_train_feature_count;};

        // CreateDescriptorsFromImage: creates descriptors for each of the input features
        // - src: input image
        // - features: input features
        // - pyr_levels: the number of pyramid levels
        void CreateDescriptorsFromImage(IplImage* src, const vector<KeyPoint>& features);

        // CreatePCADescriptors: generates descriptors for PCA components, needed for fast generation of feature descriptors
        void CreatePCADescriptors();

        // returns a feature descriptor by feature index
        const OneWayDescriptor* GetDescriptor(int desc_idx) const {return &m_descriptors[desc_idx];};

        // FindDescriptor: finds the closest descriptor
        // - patch: input image patch
        // - desc_idx: output index of the closest descriptor to the input patch
        // - pose_idx: output index of the closest pose of the closest descriptor to the input patch
        // - distance: distance from the input patch to the closest feature pose
        // - _scales: scales of the input patch for each descriptor
        // - scale_ranges: input scales variation (float[2])
        void FindDescriptor(IplImage* patch, int& desc_idx, int& pose_idx, float& distance, float* _scale = 0, float* scale_ranges = 0) const;

        // - patch: input image patch
        // - n: number of the closest indexes
        // - desc_idxs: output indexes of the closest descriptor to the input patch (n)
        // - pose_idx: output indexes of the closest pose of the closest descriptor to the input patch (n)
        // - distances: distance from the input patch to the closest feature pose (n)
        // - _scales: scales of the input patch
        // - scale_ranges: input scales variation (float[2])
        void FindDescriptor(IplImage* patch, int n, vector<int>& desc_idxs, vector<int>& pose_idxs,
                            vector<float>& distances, vector<float>& _scales, float* scale_ranges = 0) const;

        // FindDescriptor: finds the closest descriptor
        // - src: input image
        // - pt: center of the feature
        // - desc_idx: output index of the closest descriptor to the input patch
        // - pose_idx: output index of the closest pose of the closest descriptor to the input patch
        // - distance: distance from the input patch to the closest feature pose
        void FindDescriptor(IplImage* src, cv::Point2f pt, int& desc_idx, int& pose_idx, float& distance) const;

        // InitializePoses: generates random poses
        void InitializePoses();

        // InitializeTransformsFromPoses: generates 2x3 affine matrices from poses (initializes m_transforms)
        void InitializeTransformsFromPoses();

        // InitializePoseTransforms: subsequently calls InitializePoses and InitializeTransformsFromPoses
        void InitializePoseTransforms();

        // InitializeDescriptor: initializes a descriptor
        // - desc_idx: descriptor index
        // - train_image: image patch (ROI is supported)
        // - feature_label: feature textual label
        void InitializeDescriptor(int desc_idx, IplImage* train_image, const char* feature_label);

        void InitializeDescriptor(int desc_idx, IplImage* train_image, const KeyPoint& keypoint, const char* feature_label);

        // InitializeDescriptors: load features from an image and create descriptors for each of them
        void InitializeDescriptors(IplImage* train_image, const vector<KeyPoint>& features,
                                  const char* feature_label = "", int desc_start_idx = 0);

        // Write: writes this object to a file storage
        // - fs: output filestorage
        void Write (FileStorage &fs) const;

        // Read: reads OneWayDescriptorBase object from a file node
        // - fn: input file node
        void Read (const FileNode &fn);

        // LoadPCADescriptors: loads PCA descriptors from a file
        // - filename: input filename
        int LoadPCADescriptors(const char* filename);

        // LoadPCADescriptors: loads PCA descriptors from a file node
        // - fn: input file node
        int LoadPCADescriptors(const FileNode &fn);

        // SavePCADescriptors: saves PCA descriptors to a file
        // - filename: output filename
        void SavePCADescriptors(const char* filename);

        // SavePCADescriptors: saves PCA descriptors to a file storage
        // - fs: output file storage
        void SavePCADescriptors(CvFileStorage* fs) const;

        // GeneratePCA: calculate and save PCA components and descriptors
        // - img_path: path to training PCA images directory
        // - images_list: filename with filenames of training PCA images
        void GeneratePCA(const char* img_path, const char* images_list, int pose_count=500);

        // SetPCAHigh: sets the high resolution pca matrices (copied to internal structures)
        void SetPCAHigh(CvMat* avg, CvMat* eigenvectors);

        // SetPCALow: sets the low resolution pca matrices (copied to internal structures)
        void SetPCALow(CvMat* avg, CvMat* eigenvectors);

        int GetLowPCA(CvMat** avg, CvMat** eigenvectors)
        {
            *avg = m_pca_avg;
            *eigenvectors = m_pca_eigenvectors;
            return m_pca_dim_low;
        };

        int GetPCADimLow() const {return m_pca_dim_low;};
        int GetPCADimHigh() const {return m_pca_dim_high;};

        void ConvertDescriptorsArrayToTree(); // Converting pca_descriptors array to KD tree

        // GetPCAFilename: get default PCA filename
        static string GetPCAFilename () { return "pca.yml"; }

        virtual bool empty() const { return m_train_feature_count <= 0 ? true : false; }

    protected:
        ...
    };

OneWayDescriptorMatcher
-----------------------
.. ocv:class:: OneWayDescriptorMatcher : public GenericDescriptorMatcher

Wrapping class for computing, matching, and classifying descriptors using the
:ocv:class:`OneWayDescriptorBase` class. ::

    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; }

            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() );

            int poseCount;
            Size patchSize;
            string pcaFilename;
            string trainPath;
            string trainImagesList;

            float minScale, maxScale, stepScale;
        };

        OneWayDescriptorMatcher( const Params& params=Params() );
        virtual ~OneWayDescriptorMatcher();

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

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

        virtual void train();

        virtual bool isMaskSupported();

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

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

FernClassifier
--------------
.. ocv:class:: FernClassifier

::

    class CV_EXPORTS FernClassifier
    {
    public:
        FernClassifier();
        FernClassifier(const FileNode& node);
        FernClassifier(const vector<vector<Point2f> >& points,
                      const vector<Mat>& refimgs,
                      const vector<vector<int> >& labels=vector<vector<int> >(),
                      int _nclasses=0, int _patchSize=PATCH_SIZE,
                      int _signatureSize=DEFAULT_SIGNATURE_SIZE,
                      int _nstructs=DEFAULT_STRUCTS,
                      int _structSize=DEFAULT_STRUCT_SIZE,
                      int _nviews=DEFAULT_VIEWS,
                      int _compressionMethod=COMPRESSION_NONE,
                      const PatchGenerator& patchGenerator=PatchGenerator());
        virtual ~FernClassifier();
        virtual void read(const FileNode& n);
        virtual void write(FileStorage& fs, const String& name=String()) const;
        virtual void trainFromSingleView(const Mat& image,
                                        const vector<KeyPoint>& keypoints,
                                        int _patchSize=PATCH_SIZE,
                                        int _signatureSize=DEFAULT_SIGNATURE_SIZE,
                                        int _nstructs=DEFAULT_STRUCTS,
                                        int _structSize=DEFAULT_STRUCT_SIZE,
                                        int _nviews=DEFAULT_VIEWS,
                                        int _compressionMethod=COMPRESSION_NONE,
                                        const PatchGenerator& patchGenerator=PatchGenerator());
        virtual void train(const vector<vector<Point2f> >& points,
                          const vector<Mat>& refimgs,
                          const vector<vector<int> >& labels=vector<vector<int> >(),
                          int _nclasses=0, int _patchSize=PATCH_SIZE,
                          int _signatureSize=DEFAULT_SIGNATURE_SIZE,
                          int _nstructs=DEFAULT_STRUCTS,
                          int _structSize=DEFAULT_STRUCT_SIZE,
                          int _nviews=DEFAULT_VIEWS,
                          int _compressionMethod=COMPRESSION_NONE,
                          const PatchGenerator& patchGenerator=PatchGenerator());
        virtual int operator()(const Mat& img, Point2f kpt, vector<float>& signature) const;
        virtual int operator()(const Mat& patch, vector<float>& signature) const;
        virtual void clear();
        virtual bool empty() const;
        void setVerbose(bool verbose);

        int getClassCount() const;
        int getStructCount() const;
        int getStructSize() const;
        int getSignatureSize() const;
        int getCompressionMethod() const;
        Size getPatchSize() const;

        struct Feature
        {
            uchar x1, y1, x2, y2;
            Feature() : x1(0), y1(0), x2(0), y2(0) {}
            Feature(int _x1, int _y1, int _x2, int _y2)
            : x1((uchar)_x1), y1((uchar)_y1), x2((uchar)_x2), y2((uchar)_y2)
            {}
            template<typename _Tp> bool operator ()(const Mat_<_Tp>& patch) const
            { return patch(y1,x1) > patch(y2, x2); }
        };

        enum
        {
            PATCH_SIZE = 31,
            DEFAULT_STRUCTS = 50,
            DEFAULT_STRUCT_SIZE = 9,
            DEFAULT_VIEWS = 5000,
            DEFAULT_SIGNATURE_SIZE = 176,
            COMPRESSION_NONE = 0,
            COMPRESSION_RANDOM_PROJ = 1,
            COMPRESSION_PCA = 2,
            DEFAULT_COMPRESSION_METHOD = COMPRESSION_NONE
        };

    protected:
        ...
    };

FernDescriptorMatcher
---------------------
.. ocv:class:: FernDescriptorMatcher : public GenericDescriptorMatcher

Wrapping class for computing, matching, and classifying descriptors using the
:ocv:class:`FernClassifier` class. ::

    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() );

            Params( const string& filename );

            int nclasses;
            int patchSize;
            int signatureSize;
            int nstructs;
            int structSize;
            int nviews;
            int compressionMethod;
            PatchGenerator patchGenerator;

            string filename;
        };

        FernDescriptorMatcher( const Params& params=Params() );
        virtual ~FernDescriptorMatcher();

        virtual void clear();

        virtual void train();

        virtual bool isMaskSupported();

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

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

    protected:
            ...
    };