opencv 2.2.0

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
contrib.hpp
Go to the documentation of this file.
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 // License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #ifndef __OPENCV_CONTRIB_HPP__
44 #define __OPENCV_CONTRIB_HPP__
45
46 #include "opencv2/core/core.hpp"
47 #include "opencv2/features2d/features2d.hpp"
48 #include "opencv2/objdetect/objdetect.hpp"
49
50 #ifdef __cplusplus
51
52 /****************************************************************************************\
53 * Adaptive Skin Detector *
54 \****************************************************************************************/
55
56 class CV_EXPORTS CvAdaptiveSkinDetector
57 {
58 private:
59 enum {
60 GSD_HUE_LT = 3,
61 GSD_HUE_UT = 33,
62 GSD_INTENSITY_LT = 15,
63 GSD_INTENSITY_UT = 250
64 };
65
66 class CV_EXPORTS Histogram
67 {
68 private:
69 enum {
70 HistogramSize = (GSD_HUE_UT - GSD_HUE_LT + 1)
71 };
72
73 protected:
74 int findCoverageIndex(double surfaceToCover, int defaultValue = 0);
75
76 public:
77 CvHistogram *fHistogram;
78 Histogram();
79 virtual ~Histogram();
80
81 void findCurveThresholds(int &x1, int &x2, double percent = 0.05);
82 void mergeWith(Histogram *source, double weight);
83 };
84
85 int nStartCounter, nFrameCount, nSkinHueLowerBound, nSkinHueUpperBound, nMorphingMethod, nSamplingDivider;
86 double fHistogramMergeFactor, fHuePercentCovered;
87 Histogram histogramHueMotion, skinHueHistogram;
88 IplImage *imgHueFrame, *imgSaturationFrame, *imgLastGrayFrame, *imgMotionFrame, *imgFilteredFrame;
89 IplImage *imgShrinked, *imgTemp, *imgGrayFrame, *imgHSVFrame;
90
91 protected:
92 void initData(IplImage *src, int widthDivider, int heightDivider);
93 void adaptiveFilter();
94
95 public:
96
97 enum {
98 MORPHING_METHOD_NONE = 0,
99 MORPHING_METHOD_ERODE = 1,
100 MORPHING_METHOD_ERODE_ERODE = 2,
101 MORPHING_METHOD_ERODE_DILATE = 3
102 };
103
104 CvAdaptiveSkinDetector(int samplingDivider = 1, int morphingMethod = MORPHING_METHOD_NONE);
105 virtual ~CvAdaptiveSkinDetector();
106
107 virtual void process(IplImage *inputBGRImage, IplImage *outputHueMask);
108 };
109
110
111 /****************************************************************************************\
112 * Fuzzy MeanShift Tracker *
113 \****************************************************************************************/
114
115 class CV_EXPORTS CvFuzzyPoint {
116 public:
117 double x, y, value;
118
119 CvFuzzyPoint(double _x, double _y);
120 };
121
122 class CV_EXPORTS CvFuzzyCurve {
123 private:
124 std::vector<CvFuzzyPoint> points;
125 double value, centre;
126
127 bool between(double x, double x1, double x2);
128
129 public:
130 CvFuzzyCurve();
131 ~CvFuzzyCurve();
132
133 void setCentre(double _centre);
134 double getCentre();
135 void clear();
136 void addPoint(double x, double y);
137 double calcValue(double param);
138 double getValue();
139 void setValue(double _value);
140 };
141
142 class CV_EXPORTS CvFuzzyFunction {
143 public:
144 std::vector<CvFuzzyCurve> curves;
145
146 CvFuzzyFunction();
147 ~CvFuzzyFunction();
148 void addCurve(CvFuzzyCurve *curve, double value = 0);
149 void resetValues();
150 double calcValue();
151 CvFuzzyCurve *newCurve();
152 };
153
154 class CV_EXPORTS CvFuzzyRule {
155 private:
156 CvFuzzyCurve *fuzzyInput1, *fuzzyInput2;
157 CvFuzzyCurve *fuzzyOutput;
158 public:
159 CvFuzzyRule();
160 ~CvFuzzyRule();
161 void setRule(CvFuzzyCurve *c1, CvFuzzyCurve *c2, CvFuzzyCurve *o1);
162 double calcValue(double param1, double param2);
163 CvFuzzyCurve *getOutputCurve();
164 };
165
166 class CV_EXPORTS CvFuzzyController {
167 private:
168 std::vector<CvFuzzyRule*> rules;
169 public:
170 CvFuzzyController();
171 ~CvFuzzyController();
172 void addRule(CvFuzzyCurve *c1, CvFuzzyCurve *c2, CvFuzzyCurve *o1);
173 double calcOutput(double param1, double param2);
174 };
175
176 class CV_EXPORTS CvFuzzyMeanShiftTracker
177 {
178 private:
179 class FuzzyResizer
180 {
181 private:
182 CvFuzzyFunction iInput, iOutput;
183 CvFuzzyController fuzzyController;
184 public:
185 FuzzyResizer();
186 int calcOutput(double edgeDensity, double density);
187 };
188
189 class SearchWindow
190 {
191 public:
192 FuzzyResizer *fuzzyResizer;
193 int x, y;
194 int width, height, maxWidth, maxHeight, ellipseHeight, ellipseWidth;
195 int ldx, ldy, ldw, ldh, numShifts, numIters;
196 int xGc, yGc;
197 long m00, m01, m10, m11, m02, m20;
198 double ellipseAngle;
199 double density;
200 unsigned int depthLow, depthHigh;
201 int verticalEdgeLeft, verticalEdgeRight, horizontalEdgeTop, horizontalEdgeBottom;
202
203 SearchWindow();
204 ~SearchWindow();
205 void setSize(int _x, int _y, int _width, int _height);
206 void initDepthValues(IplImage *maskImage, IplImage *depthMap);
207 bool shift();
208 void extractInfo(IplImage *maskImage, IplImage *depthMap, bool initDepth);
209 void getResizeAttribsEdgeDensityLinear(int &resizeDx, int &resizeDy, int &resizeDw, int &resizeDh);
210 void getResizeAttribsInnerDensity(int &resizeDx, int &resizeDy, int &resizeDw, int &resizeDh);
211 void getResizeAttribsEdgeDensityFuzzy(int &resizeDx, int &resizeDy, int &resizeDw, int &resizeDh);
212 bool meanShift(IplImage *maskImage, IplImage *depthMap, int maxIteration, bool initDepth);
213 };
214
215 public:
216 enum TrackingState
217 {
218 tsNone = 0,
219 tsSearching = 1,
220 tsTracking = 2,
221 tsSetWindow = 3,
222 tsDisabled = 10
223 };
224
225 enum ResizeMethod {
226 rmEdgeDensityLinear = 0,
227 rmEdgeDensityFuzzy = 1,
228 rmInnerDensity = 2
229 };
230
231 enum {
232 MinKernelMass = 1000
233 };
234
235 SearchWindow kernel;
236 int searchMode;
237
238 private:
239 enum
240 {
241 MaxMeanShiftIteration = 5,
242 MaxSetSizeIteration = 5
243 };
244
245 void findOptimumSearchWindow(SearchWindow &searchWindow, IplImage *maskImage, IplImage *depthMap, int maxIteration, int resizeMethod, bool initDepth);
246
247 public:
248 CvFuzzyMeanShiftTracker();
249 ~CvFuzzyMeanShiftTracker();
250
251 void track(IplImage *maskImage, IplImage *depthMap, int resizeMethod, bool resetSearch, int minKernelMass = MinKernelMass);
252 };
253
254
255 namespace cv
256 {
257
258 class CV_EXPORTS Octree
259 {
260 public:
261 struct Node
262 {
263 Node() {}
264 int begin, end;
265 float x_min, x_max, y_min, y_max, z_min, z_max;
266 int maxLevels;
267 bool isLeaf;
268 int children[8];
269 };
270
271 Octree();
272 Octree( const vector<Point3f>& points, int maxLevels = 10, int minPoints = 20 );
273 virtual ~Octree();
274
275 virtual void buildTree( const vector<Point3f>& points, int maxLevels = 10, int minPoints = 20 );
276 virtual void getPointsWithinSphere( const Point3f& center, float radius,
277 vector<Point3f>& points ) const;
278 const vector<Node>& getNodes() const { return nodes; }
279 private:
280 int minPoints;
281 vector<Point3f> points;
282 vector<Node> nodes;
283
284 virtual void buildNext(size_t node_ind);
285 };
286
287
288 class CV_EXPORTS Mesh3D
289 {
290 public:
291 struct EmptyMeshException {};
292
293 Mesh3D();
294 Mesh3D(const vector<Point3f>& vtx);
295 ~Mesh3D();
296
297 void buildOctree();
298 void clearOctree();
299 float estimateResolution(float tryRatio = 0.1f);
300 void computeNormals(float normalRadius, int minNeighbors = 20);
301 void computeNormals(const vector<int>& subset, float normalRadius, int minNeighbors = 20);
302
303 void writeAsVrml(const String& file, const vector<Scalar>& colors = vector<Scalar>()) const;
304
305 vector<Point3f> vtx;
306 vector<Point3f> normals;
307 float resolution;
308 Octree octree;
309
310 const static Point3f allzero;
311 };
312
313 class CV_EXPORTS SpinImageModel
314 {
315 public:
316
317 /* model parameters, leave unset for default or auto estimate */
318 float normalRadius;
319 int minNeighbors;
320
321 float binSize;
322 int imageWidth;
323
324 float lambda;
325 float gamma;
326
327 float T_GeometriccConsistency;
328 float T_GroupingCorespondances;
329
330 /* public interface */
331 SpinImageModel();
332 explicit SpinImageModel(const Mesh3D& mesh);
333 ~SpinImageModel();
334
335 void setLogger(std::ostream* log);
336 void selectRandomSubset(float ratio);
337 void setSubset(const vector<int>& subset);
338 void compute();
339
340 void match(const SpinImageModel& scene, vector< vector<Vec2i> >& result);
341
342 Mat packRandomScaledSpins(bool separateScale = false, size_t xCount = 10, size_t yCount = 10) const;
343
344 size_t getSpinCount() const { return spinImages.rows; }
345 Mat getSpinImage(size_t index) const { return spinImages.row((int)index); }
346 const Point3f& getSpinVertex(size_t index) const { return mesh.vtx[subset[index]]; }
347 const Point3f& getSpinNormal(size_t index) const { return mesh.normals[subset[index]]; }
348
349 const Mesh3D& getMesh() const { return mesh; }
350 Mesh3D& getMesh() { return mesh; }
351
352 /* static utility functions */
353 static bool spinCorrelation(const Mat& spin1, const Mat& spin2, float lambda, float& result);
354
355 static Point2f calcSpinMapCoo(const Point3f& point, const Point3f& vertex, const Point3f& normal);
356
357 static float geometricConsistency(const Point3f& pointScene1, const Point3f& normalScene1,
358 const Point3f& pointModel1, const Point3f& normalModel1,
359 const Point3f& pointScene2, const Point3f& normalScene2,
360 const Point3f& pointModel2, const Point3f& normalModel2);
361
362 static float groupingCreteria(const Point3f& pointScene1, const Point3f& normalScene1,
363 const Point3f& pointModel1, const Point3f& normalModel1,
364 const Point3f& pointScene2, const Point3f& normalScene2,
365 const Point3f& pointModel2, const Point3f& normalModel2,
366 float gamma);
367 protected:
368 void defaultParams();
369
370 void matchSpinToModel(const Mat& spin, vector<int>& indeces,
371 vector<float>& corrCoeffs, bool useExtremeOutliers = true) const;
372
373 void repackSpinImages(const vector<uchar>& mask, Mat& spinImages, bool reAlloc = true) const;
374
375 vector<int> subset;
376 Mesh3D mesh;
377 Mat spinImages;
378 std::ostream* out;
379 };
380
381 class CV_EXPORTS TickMeter
382 {
383 public:
384 TickMeter();
385 void start();
386 void stop();
387
388 int64 getTimeTicks() const;
389 double getTimeMicro() const;
390 double getTimeMilli() const;
391 double getTimeSec() const;
392 int64 getCounter() const;
393
394 void reset();
395 private:
396 int64 counter;
397 int64 sumTime;
398 int64 startTime;
399 };
400
401 CV_EXPORTS std::ostream& operator<<(std::ostream& out, const TickMeter& tm);
402
403 class CV_EXPORTS SelfSimDescriptor
404 {
405 public:
406 SelfSimDescriptor();
407 SelfSimDescriptor(int _ssize, int _lsize,
408 int _startDistanceBucket=DEFAULT_START_DISTANCE_BUCKET,
409 int _numberOfDistanceBuckets=DEFAULT_NUM_DISTANCE_BUCKETS,
410 int _nangles=DEFAULT_NUM_ANGLES);
411 SelfSimDescriptor(const SelfSimDescriptor& ss);
412 virtual ~SelfSimDescriptor();
413 SelfSimDescriptor& operator = (const SelfSimDescriptor& ss);
414
415 size_t getDescriptorSize() const;
416 Size getGridSize( Size imgsize, Size winStride ) const;
417
418 virtual void compute(const Mat& img, vector<float>& descriptors, Size winStride=Size(),
419 const vector<Point>& locations=vector<Point>()) const;
420 virtual void computeLogPolarMapping(Mat& mappingMask) const;
421 virtual void SSD(const Mat& img, Point pt, Mat& ssd) const;
422
423 int smallSize;
424 int largeSize;
425 int startDistanceBucket;
426 int numberOfDistanceBuckets;
427 int numberOfAngles;
428
429 enum { DEFAULT_SMALL_SIZE = 5, DEFAULT_LARGE_SIZE = 41,
430 DEFAULT_NUM_ANGLES = 20, DEFAULT_START_DISTANCE_BUCKET = 3,
431 DEFAULT_NUM_DISTANCE_BUCKETS = 7 };
432 };
433
434 class CV_EXPORTS LevMarqSparse
435 {
436 public:
437 LevMarqSparse();
438 LevMarqSparse(int npoints, // number of points
439 int ncameras, // number of cameras
440 int nPointParams, // number of params per one point (3 in case of 3D points)
441 int nCameraParams, // number of parameters per one camera
442 int nErrParams, // number of parameters in measurement vector
443 // for 1 point at one camera (2 in case of 2D projections)
444 Mat& visibility, // visibility matrix. rows correspond to points, columns correspond to cameras
445 // 1 - point is visible for the camera, 0 - invisible
446 Mat& P0, // starting vector of parameters, first cameras then points
447 Mat& X, // measurements, in order of visibility. non visible cases are skipped
448 TermCriteria criteria, // termination criteria
449
450 // callback for estimation of Jacobian matrices
451 void (CV_CDECL * fjac)(int i, int j, Mat& point_params,
452 Mat& cam_params, Mat& A, Mat& B, void* data),
453 // callback for estimation of backprojection errors
454 void (CV_CDECL * func)(int i, int j, Mat& point_params,
455 Mat& cam_params, Mat& estim, void* data),
456 void* data // user-specific data passed to the callbacks
457 );
458 virtual ~LevMarqSparse();
459
460 virtual void run( int npoints, // number of points
461 int ncameras, // number of cameras
462 int nPointParams, // number of params per one point (3 in case of 3D points)
463 int nCameraParams, // number of parameters per one camera
464 int nErrParams, // number of parameters in measurement vector
465 // for 1 point at one camera (2 in case of 2D projections)
466 Mat& visibility, // visibility matrix. rows correspond to points, columns correspond to cameras
467 // 1 - point is visible for the camera, 0 - invisible
468 Mat& P0, // starting vector of parameters, first cameras then points
469 Mat& X, // measurements, in order of visibility. non visible cases are skipped
470 TermCriteria criteria, // termination criteria
471
472 // callback for estimation of Jacobian matrices
473 void (CV_CDECL * fjac)(int i, int j, Mat& point_params,
474 Mat& cam_params, Mat& A, Mat& B, void* data),
475 // callback for estimation of backprojection errors
476 void (CV_CDECL * func)(int i, int j, Mat& point_params,
477 Mat& cam_params, Mat& estim, void* data),
478 void* data // user-specific data passed to the callbacks
479 );
480
481 virtual void clear();
482
483 // useful function to do simple bundle adjastment tasks
484 static void bundleAdjust(vector<Point3d>& points, //positions of points in global coordinate system (input and output)
485 const vector<vector<Point2d> >& imagePoints, //projections of 3d points for every camera
486 const vector<vector<int> >& visibility, //visibility of 3d points for every camera
487 vector<Mat>& cameraMatrix, //intrinsic matrices of all cameras (input and output)
488 vector<Mat>& R, //rotation matrices of all cameras (input and output)
489 vector<Mat>& T, //translation vector of all cameras (input and output)
490 vector<Mat>& distCoeffs, //distortion coefficients of all cameras (input and output)
491 const TermCriteria& criteria=
492 TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, DBL_EPSILON));
493
494 protected:
495 virtual void optimize(); //main function that runs minimization
496
497 //iteratively asks for measurement for visible camera-point pairs
498 void ask_for_proj();
499 //iteratively asks for Jacobians for every camera_point pair
500 void ask_for_projac();
501
502 CvMat* err; //error X-hX
503 double prevErrNorm, errNorm;
504 double lambda;
505 CvTermCriteria criteria;
506 int iters;
507
508 CvMat** U; //size of array is equal to number of cameras
509 CvMat** V; //size of array is equal to number of points
510 CvMat** inv_V_star; //inverse of V*
511
512 CvMat* A;
513 CvMat* B;
514 CvMat* W;
515
516 CvMat* X; //measurement
517 CvMat* hX; //current measurement extimation given new parameter vector
518
519 CvMat* prevP; //current already accepted parameter.
520 CvMat* P; // parameters used to evaluate function with new params
521 // this parameters may be rejected
522
523 CvMat* deltaP; //computed increase of parameters (result of normal system solution )
524
525 CvMat** ea; // sum_i AijT * e_ij , used as right part of normal equation
526 // length of array is j = number of cameras
527 CvMat** eb; // sum_j BijT * e_ij , used as right part of normal equation
528 // length of array is i = number of points
529
530 CvMat** Yj; //length of array is i = num_points
531
532 CvMat* S; //big matrix of block Sjk , each block has size num_cam_params x num_cam_params
533
534 CvMat* JtJ_diag; //diagonal of JtJ, used to backup diagonal elements before augmentation
535
536 CvMat* Vis_index; // matrix which element is index of measurement for point i and camera j
537
538 int num_cams;
539 int num_points;
540 int num_err_param;
541 int num_cam_param;
542 int num_point_param;
543
544 //target function and jacobian pointers, which needs to be initialized
545 void (*fjac)(int i, int j, Mat& point_params, Mat& cam_params, Mat& A, Mat& B, void* data);
546 void (*func)(int i, int j, Mat& point_params, Mat& cam_params, Mat& estim, void* data );
547
548 void* data;
549 };
550
551
552 CV_EXPORTS bool find4QuadCornerSubpix(const Mat& img, std::vector<Point2f>& corners, Size region_size);
553
554 CV_EXPORTS int chamerMatching( Mat& img, Mat& templ,
555 vector<vector<Point> >& results, vector<float>& cost,
556 double templScale=1, int maxMatches = 20,
557 double minMatchDistance = 1.0, int padX = 3,
558 int padY = 3, int scales = 5, double minScale = 0.6, double maxScale = 1.6,
559 double orientationWeight = 0.5, double truncate = 20);
560 }
561
562
563 #endif
564
565 #endif
566