1 Common Interfaces of Generic Descriptor Matchers
2 ================================================
8 .. ocv:class:: OneWayDescriptorBase
10 Class encapsulates functionality for training/loading a set of one way descriptors
11 and finding the nearest closest descriptor to an input feature. ::
13 class CV_EXPORTS OneWayDescriptorBase
17 // creates an instance of OneWayDescriptor from a set of training files
18 // - patch_size: size of the input (large) patch
19 // - pose_count: the number of poses to generate for each descriptor
20 // - train_path: path to training files
21 // - pca_config: the name of the file that contains PCA for small patches (2 times smaller
22 // than patch_size each dimension
23 // - pca_hr_config: the name of the file that contains PCA for large patches (of patch_size size)
24 // - pca_desc_config: the name of the file that contains descriptors of PCA components
25 OneWayDescriptorBase(CvSize patch_size, int pose_count, const char* train_path = 0, const char* pca_config = 0,
26 const char* pca_hr_config = 0, const char* pca_desc_config = 0, int pyr_levels = 1,
27 int pca_dim_high = 100, int pca_dim_low = 100);
29 OneWayDescriptorBase(CvSize patch_size, int pose_count, const String &pca_filename, const String &train_path = String(), const String &images_list = String(),
30 float _scale_min = 0.7f, float _scale_max=1.5f, float _scale_step=1.2f, int pyr_levels = 1,
31 int pca_dim_high = 100, int pca_dim_low = 100);
34 virtual ~OneWayDescriptorBase();
38 // Allocate: allocates memory for a given number of descriptors
39 void Allocate(int train_feature_count);
41 // AllocatePCADescriptors: allocates memory for pca descriptors
42 void AllocatePCADescriptors();
45 CvSize GetPatchSize() const {return m_patch_size;};
46 // returns the number of poses for each descriptor
47 int GetPoseCount() const {return m_pose_count;};
49 // returns the number of pyramid levels
50 int GetPyrLevels() const {return m_pyr_levels;};
52 // returns the number of descriptors
53 int GetDescriptorCount() const {return m_train_feature_count;};
55 // CreateDescriptorsFromImage: creates descriptors for each of the input features
57 // - features: input features
58 // - pyr_levels: the number of pyramid levels
59 void CreateDescriptorsFromImage(IplImage* src, const vector<KeyPoint>& features);
61 // CreatePCADescriptors: generates descriptors for PCA components, needed for fast generation of feature descriptors
62 void CreatePCADescriptors();
64 // returns a feature descriptor by feature index
65 const OneWayDescriptor* GetDescriptor(int desc_idx) const {return &m_descriptors[desc_idx];};
67 // FindDescriptor: finds the closest descriptor
68 // - patch: input image patch
69 // - desc_idx: output index of the closest descriptor to the input patch
70 // - pose_idx: output index of the closest pose of the closest descriptor to the input patch
71 // - distance: distance from the input patch to the closest feature pose
72 // - _scales: scales of the input patch for each descriptor
73 // - scale_ranges: input scales variation (float[2])
74 void FindDescriptor(IplImage* patch, int& desc_idx, int& pose_idx, float& distance, float* _scale = 0, float* scale_ranges = 0) const;
76 // - patch: input image patch
77 // - n: number of the closest indexes
78 // - desc_idxs: output indexes of the closest descriptor to the input patch (n)
79 // - pose_idx: output indexes of the closest pose of the closest descriptor to the input patch (n)
80 // - distances: distance from the input patch to the closest feature pose (n)
81 // - _scales: scales of the input patch
82 // - scale_ranges: input scales variation (float[2])
83 void FindDescriptor(IplImage* patch, int n, vector<int>& desc_idxs, vector<int>& pose_idxs,
84 vector<float>& distances, vector<float>& _scales, float* scale_ranges = 0) const;
86 // FindDescriptor: finds the closest descriptor
88 // - pt: center of the feature
89 // - desc_idx: output index of the closest descriptor to the input patch
90 // - pose_idx: output index of the closest pose of the closest descriptor to the input patch
91 // - distance: distance from the input patch to the closest feature pose
92 void FindDescriptor(IplImage* src, cv::Point2f pt, int& desc_idx, int& pose_idx, float& distance) const;
94 // InitializePoses: generates random poses
95 void InitializePoses();
97 // InitializeTransformsFromPoses: generates 2x3 affine matrices from poses (initializes m_transforms)
98 void InitializeTransformsFromPoses();
100 // InitializePoseTransforms: subsequently calls InitializePoses and InitializeTransformsFromPoses
101 void InitializePoseTransforms();
103 // InitializeDescriptor: initializes a descriptor
104 // - desc_idx: descriptor index
105 // - train_image: image patch (ROI is supported)
106 // - feature_label: feature textual label
107 void InitializeDescriptor(int desc_idx, IplImage* train_image, const char* feature_label);
109 void InitializeDescriptor(int desc_idx, IplImage* train_image, const KeyPoint& keypoint, const char* feature_label);
111 // InitializeDescriptors: load features from an image and create descriptors for each of them
112 void InitializeDescriptors(IplImage* train_image, const vector<KeyPoint>& features,
113 const char* feature_label = "", int desc_start_idx = 0);
115 // Write: writes this object to a file storage
116 // - fs: output filestorage
117 void Write (FileStorage &fs) const;
119 // Read: reads OneWayDescriptorBase object from a file node
120 // - fn: input file node
121 void Read (const FileNode &fn);
123 // LoadPCADescriptors: loads PCA descriptors from a file
124 // - filename: input filename
125 int LoadPCADescriptors(const char* filename);
127 // LoadPCADescriptors: loads PCA descriptors from a file node
128 // - fn: input file node
129 int LoadPCADescriptors(const FileNode &fn);
131 // SavePCADescriptors: saves PCA descriptors to a file
132 // - filename: output filename
133 void SavePCADescriptors(const char* filename);
135 // SavePCADescriptors: saves PCA descriptors to a file storage
136 // - fs: output file storage
137 void SavePCADescriptors(CvFileStorage* fs) const;
139 // GeneratePCA: calculate and save PCA components and descriptors
140 // - img_path: path to training PCA images directory
141 // - images_list: filename with filenames of training PCA images
142 void GeneratePCA(const char* img_path, const char* images_list, int pose_count=500);
144 // SetPCAHigh: sets the high resolution pca matrices (copied to internal structures)
145 void SetPCAHigh(CvMat* avg, CvMat* eigenvectors);
147 // SetPCALow: sets the low resolution pca matrices (copied to internal structures)
148 void SetPCALow(CvMat* avg, CvMat* eigenvectors);
150 int GetLowPCA(CvMat** avg, CvMat** eigenvectors)
153 *eigenvectors = m_pca_eigenvectors;
154 return m_pca_dim_low;
157 int GetPCADimLow() const {return m_pca_dim_low;};
158 int GetPCADimHigh() const {return m_pca_dim_high;};
160 void ConvertDescriptorsArrayToTree(); // Converting pca_descriptors array to KD tree
162 // GetPCAFilename: get default PCA filename
163 static String GetPCAFilename () { return "pca.yml"; }
165 virtual bool empty() const { return m_train_feature_count <= 0 ? true : false; }
171 OneWayDescriptorMatcher
172 -----------------------
173 .. ocv:class:: OneWayDescriptorMatcher : public GenericDescriptorMatcher
175 Wrapping class for computing, matching, and classifying descriptors using the
176 :ocv:class:`OneWayDescriptorBase` class. ::
178 class OneWayDescriptorMatcher : public GenericDescriptorMatcher
184 static const int POSE_COUNT = 500;
185 static const int PATCH_WIDTH = 24;
186 static const int PATCH_HEIGHT = 24;
187 static float GET_MIN_SCALE() { return 0.7f; }
188 static float GET_MAX_SCALE() { return 1.5f; }
189 static float GET_STEP_SCALE() { return 1.2f; }
191 Params( int poseCount = POSE_COUNT,
192 Size patchSize = Size(PATCH_WIDTH, PATCH_HEIGHT),
193 String pcaFilename = String(),
194 String trainPath = String(), String trainImagesList = String(),
195 float minScale = GET_MIN_SCALE(), float maxScale = GET_MAX_SCALE(),
196 float stepScale = GET_STEP_SCALE() );
202 String trainImagesList;
204 float minScale, maxScale, stepScale;
207 OneWayDescriptorMatcher( const Params& params=Params() );
208 virtual ~OneWayDescriptorMatcher();
210 void initialize( const Params& params, const Ptr<OneWayDescriptorBase>& base=Ptr<OneWayDescriptorBase>() );
212 // Clears keypoints stored in collection and OneWayDescriptorBase
213 virtual void clear();
215 virtual void train();
217 virtual bool isMaskSupported();
219 virtual void read( const FileNode &fn );
220 virtual void write( FileStorage& fs ) const;
222 virtual Ptr<GenericDescriptorMatcher> clone( bool emptyTrainData=false ) const;
229 .. ocv:class:: FernClassifier
233 class CV_EXPORTS FernClassifier
237 FernClassifier(const FileNode& node);
238 FernClassifier(const vector<vector<Point2f> >& points,
239 const vector<Mat>& refimgs,
240 const vector<vector<int> >& labels=vector<vector<int> >(),
241 int _nclasses=0, int _patchSize=PATCH_SIZE,
242 int _signatureSize=DEFAULT_SIGNATURE_SIZE,
243 int _nstructs=DEFAULT_STRUCTS,
244 int _structSize=DEFAULT_STRUCT_SIZE,
245 int _nviews=DEFAULT_VIEWS,
246 int _compressionMethod=COMPRESSION_NONE,
247 const PatchGenerator& patchGenerator=PatchGenerator());
248 virtual ~FernClassifier();
249 virtual void read(const FileNode& n);
250 virtual void write(FileStorage& fs, const String& name=String()) const;
251 virtual void trainFromSingleView(const Mat& image,
252 const vector<KeyPoint>& keypoints,
253 int _patchSize=PATCH_SIZE,
254 int _signatureSize=DEFAULT_SIGNATURE_SIZE,
255 int _nstructs=DEFAULT_STRUCTS,
256 int _structSize=DEFAULT_STRUCT_SIZE,
257 int _nviews=DEFAULT_VIEWS,
258 int _compressionMethod=COMPRESSION_NONE,
259 const PatchGenerator& patchGenerator=PatchGenerator());
260 virtual void train(const vector<vector<Point2f> >& points,
261 const vector<Mat>& refimgs,
262 const vector<vector<int> >& labels=vector<vector<int> >(),
263 int _nclasses=0, int _patchSize=PATCH_SIZE,
264 int _signatureSize=DEFAULT_SIGNATURE_SIZE,
265 int _nstructs=DEFAULT_STRUCTS,
266 int _structSize=DEFAULT_STRUCT_SIZE,
267 int _nviews=DEFAULT_VIEWS,
268 int _compressionMethod=COMPRESSION_NONE,
269 const PatchGenerator& patchGenerator=PatchGenerator());
270 virtual int operator()(const Mat& img, Point2f kpt, vector<float>& signature) const;
271 virtual int operator()(const Mat& patch, vector<float>& signature) const;
272 virtual void clear();
273 virtual bool empty() const;
274 void setVerbose(bool verbose);
276 int getClassCount() const;
277 int getStructCount() const;
278 int getStructSize() const;
279 int getSignatureSize() const;
280 int getCompressionMethod() const;
281 Size getPatchSize() const;
285 uchar x1, y1, x2, y2;
286 Feature() : x1(0), y1(0), x2(0), y2(0) {}
287 Feature(int _x1, int _y1, int _x2, int _y2)
288 : x1((uchar)_x1), y1((uchar)_y1), x2((uchar)_x2), y2((uchar)_y2)
290 template<typename _Tp> bool operator ()(const Mat_<_Tp>& patch) const
291 { return patch(y1,x1) > patch(y2, x2); }
297 DEFAULT_STRUCTS = 50,
298 DEFAULT_STRUCT_SIZE = 9,
299 DEFAULT_VIEWS = 5000,
300 DEFAULT_SIGNATURE_SIZE = 176,
301 COMPRESSION_NONE = 0,
302 COMPRESSION_RANDOM_PROJ = 1,
304 DEFAULT_COMPRESSION_METHOD = COMPRESSION_NONE
311 FernDescriptorMatcher
312 ---------------------
313 .. ocv:class:: FernDescriptorMatcher : public GenericDescriptorMatcher
315 Wrapping class for computing, matching, and classifying descriptors using the
316 :ocv:class:`FernClassifier` class. ::
318 class FernDescriptorMatcher : public GenericDescriptorMatcher
324 Params( int nclasses=0,
325 int patchSize=FernClassifier::PATCH_SIZE,
326 int signatureSize=FernClassifier::DEFAULT_SIGNATURE_SIZE,
327 int nstructs=FernClassifier::DEFAULT_STRUCTS,
328 int structSize=FernClassifier::DEFAULT_STRUCT_SIZE,
329 int nviews=FernClassifier::DEFAULT_VIEWS,
330 int compressionMethod=FernClassifier::COMPRESSION_NONE,
331 const PatchGenerator& patchGenerator=PatchGenerator() );
333 Params( const String& filename );
341 int compressionMethod;
342 PatchGenerator patchGenerator;
347 FernDescriptorMatcher( const Params& params=Params() );
348 virtual ~FernDescriptorMatcher();
350 virtual void clear();
352 virtual void train();
354 virtual bool isMaskSupported();
356 virtual void read( const FileNode &fn );
357 virtual void write( FileStorage& fs ) const;
359 virtual Ptr<GenericDescriptorMatcher> clone( bool emptyTrainData=false ) const;