]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/include/opencv/cvaux.hpp
another small fix for cv::OctTree
[opencv.git] / opencv / include / opencv / cvaux.hpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #ifndef __CVAUX_HPP__
43 #define __CVAUX_HPP__
44
45 #ifdef __cplusplus
46
47 /****************************************************************************************\
48 *                                       CamShiftTracker                                  *
49 \****************************************************************************************/
50
51 class CV_EXPORTS CvCamShiftTracker
52 {
53 public:
54
55     CvCamShiftTracker();
56     virtual ~CvCamShiftTracker();
57
58     /**** Characteristics of the object that are calculated by track_object method *****/
59     float   get_orientation() const // orientation of the object in degrees
60     { return m_box.angle; }
61     float   get_length() const // the larger linear size of the object
62     { return m_box.size.height; }
63     float   get_width() const // the smaller linear size of the object
64     { return m_box.size.width; }
65     CvPoint2D32f get_center() const // center of the object
66     { return m_box.center; }
67     CvRect get_window() const // bounding rectangle for the object
68     { return m_comp.rect; }
69
70     /*********************** Tracking parameters ************************/
71     int     get_threshold() const // thresholding value that applied to back project
72     { return m_threshold; }
73
74     int     get_hist_dims( int* dims = 0 ) const // returns number of histogram dimensions and sets
75     { return m_hist ? cvGetDims( m_hist->bins, dims ) : 0; }
76
77     int     get_min_ch_val( int channel ) const // get the minimum allowed value of the specified channel
78     { return m_min_ch_val[channel]; }
79
80     int     get_max_ch_val( int channel ) const // get the maximum allowed value of the specified channel
81     { return m_max_ch_val[channel]; }
82
83     // set initial object rectangle (must be called before initial calculation of the histogram)
84     bool    set_window( CvRect window)
85     { m_comp.rect = window; return true; }
86
87     bool    set_threshold( int threshold ) // threshold applied to the histogram bins
88     { m_threshold = threshold; return true; }
89
90     bool    set_hist_bin_range( int dim, int min_val, int max_val );
91
92     bool    set_hist_dims( int c_dims, int* dims );// set the histogram parameters
93
94     bool    set_min_ch_val( int channel, int val ) // set the minimum allowed value of the specified channel
95     { m_min_ch_val[channel] = val; return true; }
96     bool    set_max_ch_val( int channel, int val ) // set the maximum allowed value of the specified channel
97     { m_max_ch_val[channel] = val; return true; }
98
99     /************************ The processing methods *********************************/
100     // update object position
101     virtual bool  track_object( const IplImage* cur_frame );
102
103     // update object histogram
104     virtual bool  update_histogram( const IplImage* cur_frame );
105
106     // reset histogram
107     virtual void  reset_histogram();
108
109     /************************ Retrieving internal data *******************************/
110     // get back project image
111     virtual IplImage* get_back_project()
112     { return m_back_project; }
113
114     float query( int* bin ) const
115     { return m_hist ? (float)cvGetRealND(m_hist->bins, bin) : 0.f; }
116
117 protected:
118
119     // internal method for color conversion: fills m_color_planes group
120     virtual void color_transform( const IplImage* img );
121
122     CvHistogram* m_hist;
123
124     CvBox2D    m_box;
125     CvConnectedComp m_comp;
126
127     float      m_hist_ranges_data[CV_MAX_DIM][2];
128     float*     m_hist_ranges[CV_MAX_DIM];
129
130     int        m_min_ch_val[CV_MAX_DIM];
131     int        m_max_ch_val[CV_MAX_DIM];
132     int        m_threshold;
133
134     IplImage*  m_color_planes[CV_MAX_DIM];
135     IplImage*  m_back_project;
136     IplImage*  m_temp;
137     IplImage*  m_mask;
138 };
139
140 /****************************************************************************************\
141 *                                   Adaptive Skin Detector                               *
142 \****************************************************************************************/
143
144 class CvAdaptiveSkinDetector
145 {
146 private:
147         enum {
148                 GSD_HUE_LT = 3,
149                 GSD_HUE_UT = 33,
150                 GSD_INTENSITY_LT = 15,
151                 GSD_INTENSITY_UT = 250
152         };
153
154         class Histogram
155         {
156         private:
157                 enum {
158                         HistogramSize = (GSD_HUE_UT - GSD_HUE_LT + 1)
159                 };
160
161         protected:
162                 int findCoverageIndex(double surfaceToCover, int defaultValue = 0);
163
164         public:
165                 CvHistogram *fHistogram;
166                 Histogram();
167                 virtual ~Histogram();
168
169                 void findCurveThresholds(int &x1, int &x2, double percent = 0.05);
170                 void mergeWith(Histogram *source, double weight);
171         };
172
173         int nStartCounter, nFrameCount, nSkinHueLowerBound, nSkinHueUpperBound, nMorphingMethod, nSamplingDivider;
174         double fHistogramMergeFactor, fHuePercentCovered;
175         Histogram histogramHueMotion, skinHueHistogram;
176         IplImage *imgHueFrame, *imgSaturationFrame, *imgLastGrayFrame, *imgMotionFrame, *imgFilteredFrame;
177         IplImage *imgShrinked, *imgTemp, *imgGrayFrame, *imgHSVFrame;
178
179 protected:
180         void initData(IplImage *src, int widthDivider, int heightDivider);
181         void adaptiveFilter();
182
183 public:
184
185         enum {
186                 MORPHING_METHOD_NONE = 0,
187                 MORPHING_METHOD_ERODE = 1,
188                 MORPHING_METHOD_ERODE_ERODE     = 2,
189                 MORPHING_METHOD_ERODE_DILATE = 3
190         };
191
192         CvAdaptiveSkinDetector(int samplingDivider = 1, int morphingMethod = MORPHING_METHOD_NONE);
193         virtual ~CvAdaptiveSkinDetector();
194
195         virtual void process(IplImage *inputBGRImage, IplImage *outputHueMask);
196 };
197
198
199 /****************************************************************************************\
200 *                                  Fuzzy MeanShift Tracker                               *
201 \****************************************************************************************/
202
203 class CvFuzzyPoint {
204 public:
205         double x, y, value;
206
207         CvFuzzyPoint(double _x, double _y);
208 };
209
210 class CvFuzzyCurve {
211 private:
212     std::vector<CvFuzzyPoint> points;
213         double value, centre;
214
215         bool between(double x, double x1, double x2);
216
217 public:
218         CvFuzzyCurve();
219         ~CvFuzzyCurve();
220
221         void setCentre(double _centre);
222         double getCentre();
223         void clear();
224         void addPoint(double x, double y);
225         double calcValue(double param);
226         double getValue();
227         void setValue(double _value);
228 };
229
230 class CvFuzzyFunction {
231 public:
232     std::vector<CvFuzzyCurve> curves;
233
234         CvFuzzyFunction();
235         ~CvFuzzyFunction();
236         void addCurve(CvFuzzyCurve *curve, double value = 0);
237         void resetValues();
238         double calcValue();
239         CvFuzzyCurve *newCurve();
240 };
241
242 class CvFuzzyRule {
243 private:
244         CvFuzzyCurve *fuzzyInput1, *fuzzyInput2;
245         CvFuzzyCurve *fuzzyOutput;
246 public:
247         CvFuzzyRule();
248         ~CvFuzzyRule();
249         void setRule(CvFuzzyCurve *c1, CvFuzzyCurve *c2, CvFuzzyCurve *o1);
250         double calcValue(double param1, double param2);
251         CvFuzzyCurve *getOutputCurve();
252 };
253
254 class CvFuzzyController {
255 private:
256     std::vector<CvFuzzyRule*> rules;
257 public:
258         CvFuzzyController();
259         ~CvFuzzyController();
260         void addRule(CvFuzzyCurve *c1, CvFuzzyCurve *c2, CvFuzzyCurve *o1);
261         double calcOutput(double param1, double param2);
262 };
263
264 class CvFuzzyMeanShiftTracker
265 {
266 private:
267         class FuzzyResizer
268         {
269         private:
270                 CvFuzzyFunction iInput, iOutput;
271                 CvFuzzyController fuzzyController;
272         public:
273                 FuzzyResizer();
274                 int calcOutput(double edgeDensity, double density);
275         };
276
277         class SearchWindow
278         {
279         public:
280                 FuzzyResizer *fuzzyResizer;
281                 int x, y;
282                 int width, height, maxWidth, maxHeight, ellipseHeight, ellipseWidth;
283                 int ldx, ldy, ldw, ldh, numShifts, numIters;
284                 int xGc, yGc;
285                 long m00, m01, m10, m11, m02, m20;
286                 double ellipseAngle;
287                 double density;
288                 unsigned int depthLow, depthHigh;
289                 int verticalEdgeLeft, verticalEdgeRight, horizontalEdgeTop, horizontalEdgeBottom;
290
291                 SearchWindow();
292                 ~SearchWindow();
293                 void setSize(int _x, int _y, int _width, int _height);
294                 void initDepthValues(IplImage *maskImage, IplImage *depthMap);
295                 bool shift();
296                 void extractInfo(IplImage *maskImage, IplImage *depthMap, bool initDepth);
297                 void getResizeAttribsEdgeDensityLinear(int &resizeDx, int &resizeDy, int &resizeDw, int &resizeDh);
298                 void getResizeAttribsInnerDensity(int &resizeDx, int &resizeDy, int &resizeDw, int &resizeDh);
299                 void getResizeAttribsEdgeDensityFuzzy(int &resizeDx, int &resizeDy, int &resizeDw, int &resizeDh);
300                 bool meanShift(IplImage *maskImage, IplImage *depthMap, int maxIteration, bool initDepth);
301         };
302
303 public:
304         enum TrackingState
305         {
306                 tsNone                  = 0,
307                 tsSearching     = 1,
308                 tsTracking              = 2,
309                 tsSetWindow     = 3,
310                 tsDisabled              = 10
311         };
312
313         enum ResizeMethod {
314                 rmEdgeDensityLinear             = 0,
315                 rmEdgeDensityFuzzy              = 1,
316                 rmInnerDensity                  = 2
317         };
318
319         enum {
320                 MinKernelMass                   = 1000
321         };
322
323         SearchWindow kernel;
324         int searchMode;
325
326 private:
327         enum
328         {
329                 MaxMeanShiftIteration   = 5,
330                 MaxSetSizeIteration     = 5
331         };
332
333         void findOptimumSearchWindow(SearchWindow &searchWindow, IplImage *maskImage, IplImage *depthMap, int maxIteration, int resizeMethod, bool initDepth);
334
335 public:
336         CvFuzzyMeanShiftTracker();
337         ~CvFuzzyMeanShiftTracker();
338
339         void track(IplImage *maskImage, IplImage *depthMap, int resizeMethod, bool resetSearch, int minKernelMass = MinKernelMass);
340 };
341
342
343 namespace cv
344 {
345
346 class CV_EXPORTS OctTree
347 {
348 public:    
349     struct Node
350     {
351         Node() {}
352         int begin, end;
353         float x_min, x_max, y_min, y_max, z_min, z_max;         
354         int maxLevels;
355         bool isLeaf;
356         int children[8];
357     };
358
359     OctTree();
360     OctTree( const Vector<Point3f>& points, int maxLevels = 10, int minPoints = 20 );
361     virtual ~OctTree();
362
363     virtual void buildTree( const Vector<Point3f>& points, int maxLevels = 10, int minPoints = 20 );
364     virtual void getPointsWithinSphere( const Point3f& center, float radius,
365                                         Vector<Point3f>& points ) const;
366     const Vector<Node>& getNodes() const { return nodes; }
367 private:
368     int minPoints;
369     Vector<Point3f> points;
370     Vector<Node> nodes;
371         
372         virtual void buildNext(size_t node_ind);
373 };
374
375
376 CV_EXPORTS void computeNormals( const OctTree& octtree, 
377                      const Vector<Point3f>& centers, 
378                      Vector<Point3f>& normals, 
379                      Vector<uchar>& mask, 
380                      float normalRadius,
381                      int minNeighbors = 20);
382
383 CV_EXPORTS void computeSpinImages( const OctTree& octtree, 
384                         const Vector<Point3f>& points,
385                         const Vector<Point3f>& normals,
386                         Vector<uchar>& mask,
387                         Mat& spinImages,                                                  
388                         float support, 
389                         float pixelsPerMeter );
390
391
392 /****************************************************************************************\
393 *            HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector        *
394 \****************************************************************************************/
395
396 struct CV_EXPORTS HOGDescriptor
397 {
398 public:
399     enum { L2Hys=0 };
400
401     HOGDescriptor() : winSize(64,128), blockSize(16,16), blockStride(8,8),
402         cellSize(8,8), nbins(9), derivAperture(1), winSigma(-1),
403         histogramNormType(L2Hys), L2HysThreshold(0.2), gammaCorrection(true)
404     {}
405
406     HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride,
407         Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1,
408         int _histogramNormType=L2Hys, double _L2HysThreshold=0.2, bool _gammaCorrection=false)
409         : winSize(_winSize), blockSize(_blockSize), blockStride(_blockStride), cellSize(_cellSize),
410         nbins(_nbins), derivAperture(_derivAperture), winSigma(_winSigma),
411         histogramNormType(_histogramNormType), L2HysThreshold(_L2HysThreshold),
412         gammaCorrection(_gammaCorrection)
413     {}
414
415     HOGDescriptor(const String& filename)
416     {
417         load(filename);
418     }
419
420     virtual ~HOGDescriptor() {}
421
422     size_t getDescriptorSize() const;
423     bool checkDetectorSize() const;
424     double getWinSigma() const;
425
426     virtual void setSVMDetector(const Vector<float>& _svmdetector);
427
428     virtual bool load(const String& filename, const String& objname=String());
429     virtual void save(const String& filename, const String& objname=String()) const;
430
431     virtual void compute(const Mat& img,
432                          Vector<float>& descriptors,
433                          Size winStride=Size(), Size padding=Size(),
434                          const Vector<Point>& locations=Vector<Point>()) const;
435     virtual void detect(const Mat& img, Vector<Point>& foundLocations,
436                         double hitThreshold=0, Size winStride=Size(),
437                         Size padding=Size(),
438                         const Vector<Point>& searchLocations=Vector<Point>()) const;
439     virtual void detectMultiScale(const Mat& img, Vector<Rect>& foundLocations,
440                                   double hitThreshold=0, Size winStride=Size(),
441                                   Size padding=Size(), double scale=1.05,
442                                   int groupThreshold=2) const;
443     virtual void computeGradient(const Mat& img, Mat& grad, Mat& angleOfs,
444                                  Size paddingTL=Size(), Size paddingBR=Size()) const;
445     virtual void normalizeBlockHistogram(Vector<float>& histogram) const;
446
447     static Vector<float> getDefaultPeopleDetector();
448
449     Size winSize;
450     Size blockSize;
451     Size blockStride;
452     Size cellSize;
453     int nbins;
454     int derivAperture;
455     double winSigma;
456     int histogramNormType;
457     double L2HysThreshold;
458     bool gammaCorrection;
459     Vector<float> svmDetector;
460 };
461
462 }
463
464 #endif /* __cplusplus */
465
466 #endif /* __CVAUX_HPP__ */
467
468 /* End of file. */