]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/include/opencv/cvaux.hpp
fixed some more GCC & MSVC warnings. Now samples on Windows build well.
[opencv.git] / opencv / include / opencv / cvaux.hpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
4 //\r
5 //  By downloading, copying, installing or using the software you agree to this license.\r
6 //  If you do not agree to this license, do not download, install,\r
7 //  copy or use the software.\r
8 //\r
9 //\r
10 //                        Intel License Agreement\r
11 //                For Open Source Computer Vision Library\r
12 //\r
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.\r
14 // Third party copyrights are property of their respective owners.\r
15 //\r
16 // Redistribution and use in source and binary forms, with or without modification,\r
17 // are permitted provided that the following conditions are met:\r
18 //\r
19 //   * Redistribution's of source code must retain the above copyright notice,\r
20 //     this list of conditions and the following disclaimer.\r
21 //\r
22 //   * Redistribution's in binary form must reproduce the above copyright notice,\r
23 //     this list of conditions and the following disclaimer in the documentation\r
24 //     and/or other materials provided with the distribution.\r
25 //\r
26 //   * The name of Intel Corporation may not be used to endorse or promote products\r
27 //     derived from this software without specific prior written permission.\r
28 //\r
29 // This software is provided by the copyright holders and contributors "as is" and\r
30 // any express or implied warranties, including, but not limited to, the implied\r
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.\r
32 // In no event shall the Intel Corporation or contributors be liable for any direct,\r
33 // indirect, incidental, special, exemplary, or consequential damages\r
34 // (including, but not limited to, procurement of substitute goods or services;\r
35 // loss of use, data, or profits; or business interruption) however caused\r
36 // and on any theory of liability, whether in contract, strict liability,\r
37 // or tort (including negligence or otherwise) arising in any way out of\r
38 // the use of this software, even if advised of the possibility of such damage.\r
39 //\r
40 //M*/\r
41 \r
42 #ifndef __CVAUX_HPP__\r
43 #define __CVAUX_HPP__\r
44 \r
45 #ifdef __cplusplus\r
46 \r
47 #include <iosfwd>\r
48 \r
49 /****************************************************************************************\\r
50 *                                       CamShiftTracker                                  *\r
51 \****************************************************************************************/\r
52 \r
53 class CV_EXPORTS CvCamShiftTracker\r
54 {\r
55 public:\r
56 \r
57     CvCamShiftTracker();\r
58     virtual ~CvCamShiftTracker();\r
59 \r
60     /**** Characteristics of the object that are calculated by track_object method *****/\r
61     float   get_orientation() const // orientation of the object in degrees\r
62     { return m_box.angle; }\r
63     float   get_length() const // the larger linear size of the object\r
64     { return m_box.size.height; }\r
65     float   get_width() const // the smaller linear size of the object\r
66     { return m_box.size.width; }\r
67     CvPoint2D32f get_center() const // center of the object\r
68     { return m_box.center; }\r
69     CvRect get_window() const // bounding rectangle for the object\r
70     { return m_comp.rect; }\r
71 \r
72     /*********************** Tracking parameters ************************/\r
73     int     get_threshold() const // thresholding value that applied to back project\r
74     { return m_threshold; }\r
75 \r
76     int     get_hist_dims( int* dims = 0 ) const // returns number of histogram dimensions and sets\r
77     { return m_hist ? cvGetDims( m_hist->bins, dims ) : 0; }\r
78 \r
79     int     get_min_ch_val( int channel ) const // get the minimum allowed value of the specified channel\r
80     { return m_min_ch_val[channel]; }\r
81 \r
82     int     get_max_ch_val( int channel ) const // get the maximum allowed value of the specified channel\r
83     { return m_max_ch_val[channel]; }\r
84 \r
85     // set initial object rectangle (must be called before initial calculation of the histogram)\r
86     bool    set_window( CvRect window)\r
87     { m_comp.rect = window; return true; }\r
88 \r
89     bool    set_threshold( int threshold ) // threshold applied to the histogram bins\r
90     { m_threshold = threshold; return true; }\r
91 \r
92     bool    set_hist_bin_range( int dim, int min_val, int max_val );\r
93 \r
94     bool    set_hist_dims( int c_dims, int* dims );// set the histogram parameters\r
95 \r
96     bool    set_min_ch_val( int channel, int val ) // set the minimum allowed value of the specified channel\r
97     { m_min_ch_val[channel] = val; return true; }\r
98     bool    set_max_ch_val( int channel, int val ) // set the maximum allowed value of the specified channel\r
99     { m_max_ch_val[channel] = val; return true; }\r
100 \r
101     /************************ The processing methods *********************************/\r
102     // update object position\r
103     virtual bool  track_object( const IplImage* cur_frame );\r
104 \r
105     // update object histogram\r
106     virtual bool  update_histogram( const IplImage* cur_frame );\r
107 \r
108     // reset histogram\r
109     virtual void  reset_histogram();\r
110 \r
111     /************************ Retrieving internal data *******************************/\r
112     // get back project image\r
113     virtual IplImage* get_back_project()\r
114     { return m_back_project; }\r
115 \r
116     float query( int* bin ) const\r
117     { return m_hist ? (float)cvGetRealND(m_hist->bins, bin) : 0.f; }\r
118 \r
119 protected:\r
120 \r
121     // internal method for color conversion: fills m_color_planes group\r
122     virtual void color_transform( const IplImage* img );\r
123 \r
124     CvHistogram* m_hist;\r
125 \r
126     CvBox2D    m_box;\r
127     CvConnectedComp m_comp;\r
128 \r
129     float      m_hist_ranges_data[CV_MAX_DIM][2];\r
130     float*     m_hist_ranges[CV_MAX_DIM];\r
131 \r
132     int        m_min_ch_val[CV_MAX_DIM];\r
133     int        m_max_ch_val[CV_MAX_DIM];\r
134     int        m_threshold;\r
135 \r
136     IplImage*  m_color_planes[CV_MAX_DIM];\r
137     IplImage*  m_back_project;\r
138     IplImage*  m_temp;\r
139     IplImage*  m_mask;\r
140 };\r
141 \r
142 /****************************************************************************************\\r
143 *                                   Adaptive Skin Detector                               *\r
144 \****************************************************************************************/\r
145 \r
146 class CV_EXPORTS CvAdaptiveSkinDetector\r
147 {\r
148 private:\r
149         enum {\r
150                 GSD_HUE_LT = 3,\r
151                 GSD_HUE_UT = 33,\r
152                 GSD_INTENSITY_LT = 15,\r
153                 GSD_INTENSITY_UT = 250\r
154         };\r
155 \r
156         class CV_EXPORTS Histogram\r
157         {\r
158         private:\r
159                 enum {\r
160                         HistogramSize = (GSD_HUE_UT - GSD_HUE_LT + 1)\r
161                 };\r
162 \r
163         protected:\r
164                 int findCoverageIndex(double surfaceToCover, int defaultValue = 0);\r
165 \r
166         public:\r
167                 CvHistogram *fHistogram;\r
168                 Histogram();\r
169                 virtual ~Histogram();\r
170 \r
171                 void findCurveThresholds(int &x1, int &x2, double percent = 0.05);\r
172                 void mergeWith(Histogram *source, double weight);\r
173         };\r
174 \r
175         int nStartCounter, nFrameCount, nSkinHueLowerBound, nSkinHueUpperBound, nMorphingMethod, nSamplingDivider;\r
176         double fHistogramMergeFactor, fHuePercentCovered;\r
177         Histogram histogramHueMotion, skinHueHistogram;\r
178         IplImage *imgHueFrame, *imgSaturationFrame, *imgLastGrayFrame, *imgMotionFrame, *imgFilteredFrame;\r
179         IplImage *imgShrinked, *imgTemp, *imgGrayFrame, *imgHSVFrame;\r
180 \r
181 protected:\r
182         void initData(IplImage *src, int widthDivider, int heightDivider);\r
183         void adaptiveFilter();\r
184 \r
185 public:\r
186 \r
187         enum {\r
188                 MORPHING_METHOD_NONE = 0,\r
189                 MORPHING_METHOD_ERODE = 1,\r
190                 MORPHING_METHOD_ERODE_ERODE     = 2,\r
191                 MORPHING_METHOD_ERODE_DILATE = 3\r
192         };\r
193 \r
194         CvAdaptiveSkinDetector(int samplingDivider = 1, int morphingMethod = MORPHING_METHOD_NONE);\r
195         virtual ~CvAdaptiveSkinDetector();\r
196 \r
197         virtual void process(IplImage *inputBGRImage, IplImage *outputHueMask);\r
198 };\r
199 \r
200 \r
201 /****************************************************************************************\\r
202 *                                  Fuzzy MeanShift Tracker                               *\r
203 \****************************************************************************************/\r
204 \r
205 class CV_EXPORTS CvFuzzyPoint {\r
206 public:\r
207         double x, y, value;\r
208 \r
209         CvFuzzyPoint(double _x, double _y);\r
210 };\r
211 \r
212 class CV_EXPORTS CvFuzzyCurve {\r
213 private:\r
214     std::vector<CvFuzzyPoint> points;\r
215         double value, centre;\r
216 \r
217         bool between(double x, double x1, double x2);\r
218 \r
219 public:\r
220         CvFuzzyCurve();\r
221         ~CvFuzzyCurve();\r
222 \r
223         void setCentre(double _centre);\r
224         double getCentre();\r
225         void clear();\r
226         void addPoint(double x, double y);\r
227         double calcValue(double param);\r
228         double getValue();\r
229         void setValue(double _value);\r
230 };\r
231 \r
232 class CV_EXPORTS CvFuzzyFunction {\r
233 public:\r
234     std::vector<CvFuzzyCurve> curves;\r
235 \r
236         CvFuzzyFunction();\r
237         ~CvFuzzyFunction();\r
238         void addCurve(CvFuzzyCurve *curve, double value = 0);\r
239         void resetValues();\r
240         double calcValue();\r
241         CvFuzzyCurve *newCurve();\r
242 };\r
243 \r
244 class CV_EXPORTS CvFuzzyRule {\r
245 private:\r
246         CvFuzzyCurve *fuzzyInput1, *fuzzyInput2;\r
247         CvFuzzyCurve *fuzzyOutput;\r
248 public:\r
249         CvFuzzyRule();\r
250         ~CvFuzzyRule();\r
251         void setRule(CvFuzzyCurve *c1, CvFuzzyCurve *c2, CvFuzzyCurve *o1);\r
252         double calcValue(double param1, double param2);\r
253         CvFuzzyCurve *getOutputCurve();\r
254 };\r
255 \r
256 class CV_EXPORTS CvFuzzyController {\r
257 private:\r
258     std::vector<CvFuzzyRule*> rules;\r
259 public:\r
260         CvFuzzyController();\r
261         ~CvFuzzyController();\r
262         void addRule(CvFuzzyCurve *c1, CvFuzzyCurve *c2, CvFuzzyCurve *o1);\r
263         double calcOutput(double param1, double param2);\r
264 };\r
265 \r
266 class CV_EXPORTS CvFuzzyMeanShiftTracker\r
267 {\r
268 private:\r
269         class FuzzyResizer\r
270         {\r
271         private:\r
272                 CvFuzzyFunction iInput, iOutput;\r
273                 CvFuzzyController fuzzyController;\r
274         public:\r
275                 FuzzyResizer();\r
276                 int calcOutput(double edgeDensity, double density);\r
277         };\r
278 \r
279         class SearchWindow\r
280         {\r
281         public:\r
282                 FuzzyResizer *fuzzyResizer;\r
283                 int x, y;\r
284                 int width, height, maxWidth, maxHeight, ellipseHeight, ellipseWidth;\r
285                 int ldx, ldy, ldw, ldh, numShifts, numIters;\r
286                 int xGc, yGc;\r
287                 long m00, m01, m10, m11, m02, m20;\r
288                 double ellipseAngle;\r
289                 double density;\r
290                 unsigned int depthLow, depthHigh;\r
291                 int verticalEdgeLeft, verticalEdgeRight, horizontalEdgeTop, horizontalEdgeBottom;\r
292 \r
293                 SearchWindow();\r
294                 ~SearchWindow();\r
295                 void setSize(int _x, int _y, int _width, int _height);\r
296                 void initDepthValues(IplImage *maskImage, IplImage *depthMap);\r
297                 bool shift();\r
298                 void extractInfo(IplImage *maskImage, IplImage *depthMap, bool initDepth);\r
299                 void getResizeAttribsEdgeDensityLinear(int &resizeDx, int &resizeDy, int &resizeDw, int &resizeDh);\r
300                 void getResizeAttribsInnerDensity(int &resizeDx, int &resizeDy, int &resizeDw, int &resizeDh);\r
301                 void getResizeAttribsEdgeDensityFuzzy(int &resizeDx, int &resizeDy, int &resizeDw, int &resizeDh);\r
302                 bool meanShift(IplImage *maskImage, IplImage *depthMap, int maxIteration, bool initDepth);\r
303         };\r
304 \r
305 public:\r
306         enum TrackingState\r
307         {\r
308                 tsNone                  = 0,\r
309                 tsSearching     = 1,\r
310                 tsTracking              = 2,\r
311                 tsSetWindow     = 3,\r
312                 tsDisabled              = 10\r
313         };\r
314 \r
315         enum ResizeMethod {\r
316                 rmEdgeDensityLinear             = 0,\r
317                 rmEdgeDensityFuzzy              = 1,\r
318                 rmInnerDensity                  = 2\r
319         };\r
320 \r
321         enum {\r
322                 MinKernelMass                   = 1000\r
323         };\r
324 \r
325         SearchWindow kernel;\r
326         int searchMode;\r
327 \r
328 private:\r
329         enum\r
330         {\r
331                 MaxMeanShiftIteration   = 5,\r
332                 MaxSetSizeIteration     = 5\r
333         };\r
334 \r
335         void findOptimumSearchWindow(SearchWindow &searchWindow, IplImage *maskImage, IplImage *depthMap, int maxIteration, int resizeMethod, bool initDepth);\r
336 \r
337 public:\r
338         CvFuzzyMeanShiftTracker();\r
339         ~CvFuzzyMeanShiftTracker();\r
340 \r
341         void track(IplImage *maskImage, IplImage *depthMap, int resizeMethod, bool resetSearch, int minKernelMass = MinKernelMass);\r
342 };\r
343 \r
344 \r
345 namespace cv\r
346 {\r
347 \r
348 class CV_EXPORTS OctTree\r
349 {\r
350 public:    \r
351     struct Node\r
352     {\r
353         Node() {}\r
354         int begin, end;\r
355         float x_min, x_max, y_min, y_max, z_min, z_max;         \r
356         int maxLevels;\r
357         bool isLeaf;\r
358         int children[8];\r
359     };\r
360 \r
361     OctTree();\r
362     OctTree( const Vector<Point3f>& points, int maxLevels = 10, int minPoints = 20 );\r
363     virtual ~OctTree();\r
364 \r
365     virtual void buildTree( const Vector<Point3f>& points, int maxLevels = 10, int minPoints = 20 );\r
366     virtual void getPointsWithinSphere( const Point3f& center, float radius,\r
367                                         Vector<Point3f>& points ) const;\r
368     const Vector<Node>& getNodes() const { return nodes; }\r
369 private:\r
370     int minPoints;\r
371     Vector<Point3f> points;\r
372     Vector<Node> nodes;\r
373         \r
374         virtual void buildNext(size_t node_ind);\r
375 };\r
376 \r
377 \r
378 class CV_EXPORTS Mesh3D\r
379 {\r
380 public:\r
381     struct EmptyMeshException {};\r
382 \r
383     Mesh3D();\r
384     Mesh3D(const Vector<Point3f>& vtx);\r
385     ~Mesh3D();\r
386 \r
387     void buildOctTree();\r
388     void clearOctTree();\r
389     float estimateResolution(float tryRatio = 0.1f);        \r
390     void computeNormals(float normalRadius, int minNeighbors = 20);\r
391     void computeNormals(const Vector<int>& subset, float normalRadius, int minNeighbors = 20);\r
392     \r
393     void writeAsVrml(const String& file, const Vector<Scalar>& colors = Vector<Scalar>()) const;\r
394     \r
395     Vector<Point3f> vtx;\r
396     Vector<Point3f> normals;\r
397     float resolution;    \r
398     OctTree octree;\r
399 \r
400     const static Point3f allzero;\r
401 };\r
402 \r
403 class CV_EXPORTS SpinImageModel\r
404 {\r
405 public:\r
406     \r
407     /* model parameters, leave unset for default or auto estimate */\r
408     float normalRadius;\r
409     int minNeighbors;\r
410 \r
411     float binSize;\r
412     int imageWidth;\r
413 \r
414     float lambda;                        \r
415     float gamma;\r
416     float Tgc;\r
417 \r
418     /* public interface */\r
419     SpinImageModel();\r
420     explicit SpinImageModel(const Mesh3D& mesh);\r
421     ~SpinImageModel();\r
422 \r
423     void setLogger(std::ostream* log);\r
424     void selectRandomSubset(float ratio);         \r
425     void compute();\r
426 \r
427     Vector< Vector< Vec2i > > match(const SpinImageModel& scene); \r
428 \r
429     Mat packRandomScaledSpins(bool separateScale = false, size_t xCount = 10, size_t yCount = 10) const;\r
430     \r
431     size_t getSpinCount() const { return spinImages.rows; }\r
432     Mat getSpinImage(size_t index) const { return spinImages.row(index); }\r
433     const Point3f& getSpinVertex(size_t index) const { return mesh.vtx[subset[index]]; }\r
434     const Point3f& getSpinNormal(size_t index) const { return mesh.normals[subset[index]]; }\r
435 \r
436     const Mesh3D& getMesh() const { return mesh; }\r
437 \r
438     /* static utility functions */\r
439     static bool spinCorrelation(const Mat& spin1, const Mat& spin2, float lambda, float& result);\r
440 \r
441     static Point2f calcSpinMapCoo(const Point3f& point, const Point3f& vertex, const Point3f& normal);\r
442 \r
443     static float geometricConsistency(const Point3f& pointScene1, const Point3f& normalScene1,\r
444                                       const Point3f& pointModel1, const Point3f& normalModel1,   \r
445                                       const Point3f& pointScene2, const Point3f& normalScene2,                               \r
446                                       const Point3f& pointModel2, const Point3f& normalModel2);\r
447 \r
448     static float groupingCreteria(const Point3f& pointScene1, const Point3f& normalScene1,\r
449                                   const Point3f& pointModel1, const Point3f& normalModel1,\r
450                                   const Point3f& pointScene2, const Point3f& normalScene2,                               \r
451                                   const Point3f& pointModel2, const Point3f& normalModel2, \r
452                                   float gamma);\r
453 protected:       \r
454     void defaultParams();\r
455 \r
456     void matchSpinToModel(const Mat& spin, Vector<int>& indeces, \r
457         Vector<float>& corrCoeffs, bool useExtremeOutliers = true) const; \r
458 \r
459     void repackSpinImages(const Vector<uchar>& mask, Mat& spinImages, bool reAlloc = true) const;\r
460              \r
461     Vector<int> subset;\r
462     Mesh3D mesh;\r
463     Mat spinImages;\r
464     std::ostream* out;\r
465 };\r
466 \r
467 class CV_EXPORTS TickMeter\r
468 {\r
469 public:\r
470     TickMeter();\r
471     void start();    \r
472     void stop();\r
473 \r
474     int64 getTimeTicks() const;\r
475     double getTimeMicro() const;\r
476     double getTimeMilli() const;\r
477     double getTimeSec()   const;\r
478     int64 getCounter() const;\r
479 \r
480     void reset();\r
481 private:\r
482     int64 counter;\r
483     int64 sumTime;\r
484     int64 startTime;\r
485 };\r
486 \r
487 CV_EXPORTS std::ostream& operator<<(std::ostream& out, const TickMeter& tm);\r
488 \r
489 /****************************************************************************************\\r
490 *            HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector        *\r
491 \****************************************************************************************/\r
492 \r
493 struct CV_EXPORTS HOGDescriptor\r
494 {\r
495 public:\r
496     enum { L2Hys=0 };\r
497 \r
498     HOGDescriptor() : winSize(64,128), blockSize(16,16), blockStride(8,8),\r
499         cellSize(8,8), nbins(9), derivAperture(1), winSigma(-1),\r
500         histogramNormType(L2Hys), L2HysThreshold(0.2), gammaCorrection(true)\r
501     {}\r
502 \r
503     HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride,\r
504         Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1,\r
505         int _histogramNormType=L2Hys, double _L2HysThreshold=0.2, bool _gammaCorrection=false)\r
506         : winSize(_winSize), blockSize(_blockSize), blockStride(_blockStride), cellSize(_cellSize),\r
507         nbins(_nbins), derivAperture(_derivAperture), winSigma(_winSigma),\r
508         histogramNormType(_histogramNormType), L2HysThreshold(_L2HysThreshold),\r
509         gammaCorrection(_gammaCorrection)\r
510     {}\r
511 \r
512     HOGDescriptor(const String& filename)\r
513     {\r
514         load(filename);\r
515     }\r
516 \r
517     virtual ~HOGDescriptor() {}\r
518 \r
519     size_t getDescriptorSize() const;\r
520     bool checkDetectorSize() const;\r
521     double getWinSigma() const;\r
522 \r
523     virtual void setSVMDetector(const Vector<float>& _svmdetector);\r
524 \r
525     virtual bool load(const String& filename, const String& objname=String());\r
526     virtual void save(const String& filename, const String& objname=String()) const;\r
527 \r
528     virtual void compute(const Mat& img,\r
529                          Vector<float>& descriptors,\r
530                          Size winStride=Size(), Size padding=Size(),\r
531                          const Vector<Point>& locations=Vector<Point>()) const;\r
532     virtual void detect(const Mat& img, Vector<Point>& foundLocations,\r
533                         double hitThreshold=0, Size winStride=Size(),\r
534                         Size padding=Size(),\r
535                         const Vector<Point>& searchLocations=Vector<Point>()) const;\r
536     virtual void detectMultiScale(const Mat& img, Vector<Rect>& foundLocations,\r
537                                   double hitThreshold=0, Size winStride=Size(),\r
538                                   Size padding=Size(), double scale=1.05,\r
539                                   int groupThreshold=2) const;\r
540     virtual void computeGradient(const Mat& img, Mat& grad, Mat& angleOfs,\r
541                                  Size paddingTL=Size(), Size paddingBR=Size()) const;\r
542     virtual void normalizeBlockHistogram(Vector<float>& histogram) const;\r
543 \r
544     static Vector<float> getDefaultPeopleDetector();\r
545 \r
546     Size winSize;\r
547     Size blockSize;\r
548     Size blockStride;\r
549     Size cellSize;\r
550     int nbins;\r
551     int derivAperture;\r
552     double winSigma;\r
553     int histogramNormType;\r
554     double L2HysThreshold;\r
555     bool gammaCorrection;\r
556     Vector<float> svmDetector;\r
557 };\r
558 \r
559 \r
560 class CV_EXPORTS SelfSimDescriptor\r
561 {\r
562 public:\r
563     SelfSimDescriptor();\r
564     SelfSimDescriptor(int _ssize, int _lsize,\r
565         int _startDistanceBucket=DEFAULT_START_DISTANCE_BUCKET,\r
566         int _numberOfDistanceBuckets=DEFAULT_NUM_DISTANCE_BUCKETS,\r
567         int _nangles=DEFAULT_NUM_ANGLES);\r
568         SelfSimDescriptor(const SelfSimDescriptor& ss);\r
569         virtual ~SelfSimDescriptor();\r
570     SelfSimDescriptor& operator = (const SelfSimDescriptor& ss);\r
571 \r
572     size_t getDescriptorSize() const;\r
573     Size getGridSize( Size imgsize, Size winStride ) const;\r
574 \r
575     virtual void compute(const Mat& img, Vector<float>& descriptors, Size winStride=Size(),\r
576                          const Vector<Point>& locations=Vector<Point>()) const;\r
577     virtual void computeLogPolarMapping(Mat& mappingMask) const;\r
578     virtual void SSD(const Mat& img, Point pt, Mat& ssd) const;\r
579 \r
580         int smallSize;\r
581         int largeSize;\r
582     int startDistanceBucket;\r
583     int numberOfDistanceBuckets;\r
584     int numberOfAngles;\r
585 \r
586     enum { DEFAULT_SMALL_SIZE = 5, DEFAULT_LARGE_SIZE = 41,\r
587         DEFAULT_NUM_ANGLES = 20, DEFAULT_START_DISTANCE_BUCKET = 3,\r
588         DEFAULT_NUM_DISTANCE_BUCKETS = 7 };\r
589 };\r
590 \r
591     \r
592 class CV_EXPORTS PatchGenerator\r
593 {\r
594 public:\r
595     PatchGenerator();\r
596     PatchGenerator(double _backgroundMin, double _backgroundMax,\r
597                    double _noiseRange, bool _randomBlur=true,\r
598                    double _lambdaMin=0.6, double _lambdaMax=1.5,\r
599                    double _thetaMin=-CV_PI, double _thetaMax=CV_PI,\r
600                    double _phiMin=-CV_PI, double _phiMax=CV_PI );\r
601     void operator()(const Mat& image, Point2f pt, Mat& patch, Size patchSize, RNG& rng) const;\r
602     void operator()(const Mat& image, const Mat& transform, Mat& patch,\r
603                     Size patchSize, RNG& rng) const;\r
604     void warpWholeImage(const Mat& image, Mat& _T, Mat& buf,\r
605                         Mat& warped, int border, RNG& rng) const;\r
606     void generateRandomTransform(Point2f srcCenter, Point2f dstCenter,\r
607                                  Mat& transform, RNG& rng, bool inverse=false) const;\r
608     double backgroundMin, backgroundMax;\r
609     double noiseRange;\r
610     bool randomBlur;\r
611     double lambdaMin, lambdaMax;\r
612     double thetaMin, thetaMax;\r
613     double phiMin, phiMax;\r
614 };\r
615 \r
616 \r
617 class CV_EXPORTS LDetector\r
618 {\r
619 public:    \r
620     LDetector();\r
621     LDetector(int _radius, int _threshold, int _nOctaves,\r
622               int _nViews, double _baseFeatureSize, double _clusteringDistance);\r
623     void operator()(const Mat& image, Vector<KeyPoint>& keypoints, int maxCount=0, bool scaleCoords=true) const;\r
624     void operator()(const Vector<Mat>& pyr, Vector<KeyPoint>& keypoints, int maxCount=0, bool scaleCoords=true) const;\r
625     void getMostStable2D(const Mat& image, Vector<KeyPoint>& keypoints,\r
626                          int maxCount, const PatchGenerator& patchGenerator) const;\r
627     void setVerbose(bool verbose);\r
628     \r
629     void read(const FileNode& node);\r
630     void write(FileStorage& fs, const String& name=String()) const;\r
631     \r
632     int radius;\r
633     int threshold;\r
634     int nOctaves;\r
635     int nViews;\r
636     bool verbose;\r
637     \r
638     double baseFeatureSize;\r
639     double clusteringDistance;\r
640 };\r
641 \r
642 \r
643 class CV_EXPORTS FernClassifier\r
644 {\r
645 public:\r
646     FernClassifier();\r
647     FernClassifier(const FileNode& node);\r
648     FernClassifier(const Vector<Point2f>& points,\r
649                    const Vector<Ptr<Mat> >& refimgs,\r
650                    const Vector<int>& labels=Vector<int>(),\r
651                    int _nclasses=0, int _patchSize=PATCH_SIZE,\r
652                    int _signatureSize=DEFAULT_SIGNATURE_SIZE,\r
653                    int _nstructs=DEFAULT_STRUCTS,\r
654                    int _structSize=DEFAULT_STRUCT_SIZE,\r
655                    int _nviews=DEFAULT_VIEWS,\r
656                    int _compressionMethod=COMPRESSION_NONE,\r
657                    const PatchGenerator& patchGenerator=PatchGenerator());\r
658     virtual ~FernClassifier();\r
659     virtual void read(const FileNode& n);\r
660     virtual void write(FileStorage& fs, const String& name=String()) const;\r
661     virtual void trainFromSingleView(const Mat& image,\r
662                                      const Vector<KeyPoint>& keypoints,\r
663                                      int _patchSize=PATCH_SIZE,\r
664                                      int _signatureSize=DEFAULT_SIGNATURE_SIZE,\r
665                                      int _nstructs=DEFAULT_STRUCTS,\r
666                                      int _structSize=DEFAULT_STRUCT_SIZE,\r
667                                      int _nviews=DEFAULT_VIEWS,\r
668                                      int _compressionMethod=COMPRESSION_NONE,\r
669                                      const PatchGenerator& patchGenerator=PatchGenerator());\r
670     virtual void train(const Vector<Point2f>& points,\r
671                        const Vector<Ptr<Mat> >& refimgs,\r
672                        const Vector<int>& labels=Vector<int>(),\r
673                        int _nclasses=0, int _patchSize=PATCH_SIZE,\r
674                        int _signatureSize=DEFAULT_SIGNATURE_SIZE,\r
675                        int _nstructs=DEFAULT_STRUCTS,\r
676                        int _structSize=DEFAULT_STRUCT_SIZE,\r
677                        int _nviews=DEFAULT_VIEWS,\r
678                        int _compressionMethod=COMPRESSION_NONE,\r
679                        const PatchGenerator& patchGenerator=PatchGenerator());\r
680     virtual int operator()(const Mat& img, Point2f kpt, Vector<float>& signature) const;\r
681     virtual int operator()(const Mat& patch, Vector<float>& signature) const;\r
682     virtual void clear();\r
683     void setVerbose(bool verbose);\r
684     \r
685     int getClassCount() const;\r
686     int getStructCount() const;\r
687     int getStructSize() const;\r
688     int getSignatureSize() const;\r
689     int getCompressionMethod() const;\r
690     Size getPatchSize() const;    \r
691     \r
692     struct Feature\r
693     {\r
694         uchar x1, y1, x2, y2;\r
695         Feature() : x1(0), y1(0), x2(0), y2(0) {}\r
696         Feature(int _x1, int _y1, int _x2, int _y2)\r
697         : x1((uchar)_x1), y1((uchar)_y1), x2((uchar)_x2), y2((uchar)_y2)\r
698         {}\r
699         template<typename _Tp> bool operator ()(const Mat_<_Tp>& patch) const\r
700         { return patch(y1,x1) > patch(y2, x2); }\r
701     };\r
702     \r
703     enum\r
704     {\r
705         PATCH_SIZE = 31,\r
706         DEFAULT_STRUCTS = 50,\r
707         DEFAULT_STRUCT_SIZE = 9,\r
708         DEFAULT_VIEWS = 5000,\r
709         DEFAULT_SIGNATURE_SIZE = 176,\r
710         COMPRESSION_NONE = 0,\r
711         COMPRESSION_RANDOM_PROJ = 1,\r
712         COMPRESSION_PCA = 2,\r
713         DEFAULT_COMPRESSION_METHOD = COMPRESSION_NONE\r
714     };\r
715     \r
716 protected:\r
717     virtual void prepare(int _nclasses, int _patchSize, int _signatureSize,\r
718                          int _nstructs, int _structSize,\r
719                          int _nviews, int _compressionMethod);\r
720     virtual void finalize(RNG& rng);\r
721     virtual int getLeaf(int fidx, const Mat& patch) const;\r
722     \r
723     bool verbose;\r
724     int nstructs;\r
725     int structSize;\r
726     int nclasses;\r
727     int signatureSize;\r
728     int compressionMethod;\r
729     int leavesPerStruct;\r
730     Size patchSize;\r
731     Vector<Feature> features;\r
732     Vector<int> classCounters;\r
733     Vector<float> posteriors;\r
734 };\r
735 \r
736 class CV_EXPORTS PlanarObjectDetector\r
737 {\r
738 public:\r
739     PlanarObjectDetector();\r
740     PlanarObjectDetector(const FileNode& node);\r
741     PlanarObjectDetector(const Vector<Mat>& pyr, int _npoints=300,\r
742                          int _patchSize=FernClassifier::PATCH_SIZE,\r
743                          int _nstructs=FernClassifier::DEFAULT_STRUCTS,\r
744                          int _structSize=FernClassifier::DEFAULT_STRUCT_SIZE,\r
745                          int _nviews=FernClassifier::DEFAULT_VIEWS,\r
746                          const LDetector& detector=LDetector(),\r
747                          const PatchGenerator& patchGenerator=PatchGenerator()); \r
748     virtual ~PlanarObjectDetector();\r
749     virtual void train(const Vector<Mat>& pyr, int _npoints=300,\r
750                        int _patchSize=FernClassifier::PATCH_SIZE,\r
751                        int _nstructs=FernClassifier::DEFAULT_STRUCTS,\r
752                        int _structSize=FernClassifier::DEFAULT_STRUCT_SIZE,\r
753                        int _nviews=FernClassifier::DEFAULT_VIEWS,\r
754                        const LDetector& detector=LDetector(),\r
755                        const PatchGenerator& patchGenerator=PatchGenerator());\r
756     virtual void train(const Vector<Mat>& pyr, const Vector<KeyPoint>& keypoints,\r
757                        int _patchSize=FernClassifier::PATCH_SIZE,\r
758                        int _nstructs=FernClassifier::DEFAULT_STRUCTS,\r
759                        int _structSize=FernClassifier::DEFAULT_STRUCT_SIZE,\r
760                        int _nviews=FernClassifier::DEFAULT_VIEWS,\r
761                        const LDetector& detector=LDetector(),\r
762                        const PatchGenerator& patchGenerator=PatchGenerator());\r
763     Rect getModelROI() const;\r
764     Vector<KeyPoint> getModelPoints() const;\r
765     const LDetector& getDetector() const;\r
766     const FernClassifier& getClassifier() const;\r
767     void setVerbose(bool verbose);\r
768     \r
769     void read(const FileNode& node);\r
770     void write(FileStorage& fs, const String& name=String()) const;\r
771     bool operator()(const Mat& image, Mat& H, Vector<Point2f>& corners) const;\r
772     bool operator()(const Vector<Mat>& pyr, const Vector<KeyPoint>& keypoints,\r
773                     Mat& H, Vector<Point2f>& corners, Vector<int>* pairs=0) const;\r
774     \r
775 protected:\r
776     bool verbose;\r
777     Rect modelROI;\r
778     Vector<KeyPoint> modelPoints;\r
779     LDetector ldetector;\r
780     FernClassifier fernClassifier;\r
781 };\r
782     \r
783 }\r
784 \r
785 #endif /* __cplusplus */\r
786 \r
787 #endif /* __CVAUX_HPP__ */\r
788 \r
789 /* End of file. */\r