1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #ifndef __OPENCV_CONTRIB_COMPAT_HPP__
44 #define __OPENCV_CONTRIB_COMPAT_HPP__
46 #include "opencv2/core/core_c.h"
50 /****************************************************************************************\
51 * Adaptive Skin Detector *
52 \****************************************************************************************/
54 class CV_EXPORTS CvAdaptiveSkinDetector
60 GSD_INTENSITY_LT = 15,
61 GSD_INTENSITY_UT = 250
64 class CV_EXPORTS Histogram
68 HistogramSize = (GSD_HUE_UT - GSD_HUE_LT + 1)
72 int findCoverageIndex(double surfaceToCover, int defaultValue = 0);
75 CvHistogram *fHistogram;
79 void findCurveThresholds(int &x1, int &x2, double percent = 0.05);
80 void mergeWith(Histogram *source, double weight);
83 int nStartCounter, nFrameCount, nSkinHueLowerBound, nSkinHueUpperBound, nMorphingMethod, nSamplingDivider;
84 double fHistogramMergeFactor, fHuePercentCovered;
85 Histogram histogramHueMotion, skinHueHistogram;
86 IplImage *imgHueFrame, *imgSaturationFrame, *imgLastGrayFrame, *imgMotionFrame, *imgFilteredFrame;
87 IplImage *imgShrinked, *imgTemp, *imgGrayFrame, *imgHSVFrame;
90 void initData(IplImage *src, int widthDivider, int heightDivider);
91 void adaptiveFilter();
96 MORPHING_METHOD_NONE = 0,
97 MORPHING_METHOD_ERODE = 1,
98 MORPHING_METHOD_ERODE_ERODE = 2,
99 MORPHING_METHOD_ERODE_DILATE = 3
102 CvAdaptiveSkinDetector(int samplingDivider = 1, int morphingMethod = MORPHING_METHOD_NONE);
103 virtual ~CvAdaptiveSkinDetector();
105 virtual void process(IplImage *inputBGRImage, IplImage *outputHueMask);
109 /****************************************************************************************\
110 * Fuzzy MeanShift Tracker *
111 \****************************************************************************************/
113 class CV_EXPORTS CvFuzzyPoint {
117 CvFuzzyPoint(double _x, double _y);
120 class CV_EXPORTS CvFuzzyCurve {
122 std::vector<CvFuzzyPoint> points;
123 double value, centre;
125 bool between(double x, double x1, double x2);
131 void setCentre(double _centre);
134 void addPoint(double x, double y);
135 double calcValue(double param);
137 void setValue(double _value);
140 class CV_EXPORTS CvFuzzyFunction {
142 std::vector<CvFuzzyCurve> curves;
146 void addCurve(CvFuzzyCurve *curve, double value = 0);
149 CvFuzzyCurve *newCurve();
152 class CV_EXPORTS CvFuzzyRule {
154 CvFuzzyCurve *fuzzyInput1, *fuzzyInput2;
155 CvFuzzyCurve *fuzzyOutput;
159 void setRule(CvFuzzyCurve *c1, CvFuzzyCurve *c2, CvFuzzyCurve *o1);
160 double calcValue(double param1, double param2);
161 CvFuzzyCurve *getOutputCurve();
164 class CV_EXPORTS CvFuzzyController {
166 std::vector<CvFuzzyRule*> rules;
169 ~CvFuzzyController();
170 void addRule(CvFuzzyCurve *c1, CvFuzzyCurve *c2, CvFuzzyCurve *o1);
171 double calcOutput(double param1, double param2);
174 class CV_EXPORTS CvFuzzyMeanShiftTracker
180 CvFuzzyFunction iInput, iOutput;
181 CvFuzzyController fuzzyController;
184 int calcOutput(double edgeDensity, double density);
190 FuzzyResizer *fuzzyResizer;
192 int width, height, maxWidth, maxHeight, ellipseHeight, ellipseWidth;
193 int ldx, ldy, ldw, ldh, numShifts, numIters;
195 long m00, m01, m10, m11, m02, m20;
198 unsigned int depthLow, depthHigh;
199 int verticalEdgeLeft, verticalEdgeRight, horizontalEdgeTop, horizontalEdgeBottom;
203 void setSize(int _x, int _y, int _width, int _height);
204 void initDepthValues(IplImage *maskImage, IplImage *depthMap);
206 void extractInfo(IplImage *maskImage, IplImage *depthMap, bool initDepth);
207 void getResizeAttribsEdgeDensityLinear(int &resizeDx, int &resizeDy, int &resizeDw, int &resizeDh);
208 void getResizeAttribsInnerDensity(int &resizeDx, int &resizeDy, int &resizeDw, int &resizeDh);
209 void getResizeAttribsEdgeDensityFuzzy(int &resizeDx, int &resizeDy, int &resizeDw, int &resizeDh);
210 bool meanShift(IplImage *maskImage, IplImage *depthMap, int maxIteration, bool initDepth);
224 rmEdgeDensityLinear = 0,
225 rmEdgeDensityFuzzy = 1,
239 MaxMeanShiftIteration = 5,
240 MaxSetSizeIteration = 5
243 void findOptimumSearchWindow(SearchWindow &searchWindow, IplImage *maskImage, IplImage *depthMap, int maxIteration, int resizeMethod, bool initDepth);
246 CvFuzzyMeanShiftTracker();
247 ~CvFuzzyMeanShiftTracker();
249 void track(IplImage *maskImage, IplImage *depthMap, int resizeMethod, bool resetSearch, int minKernelMass = MinKernelMass);
256 typedef bool (*BundleAdjustCallback)(int iteration, double norm_error, void* user_data);
258 class CV_EXPORTS LevMarqSparse {
261 LevMarqSparse(int npoints, // number of points
262 int ncameras, // number of cameras
263 int nPointParams, // number of params per one point (3 in case of 3D points)
264 int nCameraParams, // number of parameters per one camera
265 int nErrParams, // number of parameters in measurement vector
266 // for 1 point at one camera (2 in case of 2D projections)
267 Mat& visibility, // visibility matrix. rows correspond to points, columns correspond to cameras
268 // 1 - point is visible for the camera, 0 - invisible
269 Mat& P0, // starting vector of parameters, first cameras then points
270 Mat& X, // measurements, in order of visibility. non visible cases are skipped
271 TermCriteria criteria, // termination criteria
273 // callback for estimation of Jacobian matrices
274 void (*fjac)(int i, int j, Mat& point_params,
275 Mat& cam_params, Mat& A, Mat& B, void* data),
276 // callback for estimation of backprojection errors
277 void (*func)(int i, int j, Mat& point_params,
278 Mat& cam_params, Mat& estim, void* data),
279 void* data, // user-specific data passed to the callbacks
280 BundleAdjustCallback cb, void* user_data
283 virtual ~LevMarqSparse();
285 virtual void run( int npoints, // number of points
286 int ncameras, // number of cameras
287 int nPointParams, // number of params per one point (3 in case of 3D points)
288 int nCameraParams, // number of parameters per one camera
289 int nErrParams, // number of parameters in measurement vector
290 // for 1 point at one camera (2 in case of 2D projections)
291 Mat& visibility, // visibility matrix. rows correspond to points, columns correspond to cameras
292 // 1 - point is visible for the camera, 0 - invisible
293 Mat& P0, // starting vector of parameters, first cameras then points
294 Mat& X, // measurements, in order of visibility. non visible cases are skipped
295 TermCriteria criteria, // termination criteria
297 // callback for estimation of Jacobian matrices
298 void (CV_CDECL * fjac)(int i, int j, Mat& point_params,
299 Mat& cam_params, Mat& A, Mat& B, void* data),
300 // callback for estimation of backprojection errors
301 void (CV_CDECL * func)(int i, int j, Mat& point_params,
302 Mat& cam_params, Mat& estim, void* data),
303 void* data // user-specific data passed to the callbacks
306 virtual void clear();
308 // useful function to do simple bundle adjustment tasks
309 static void bundleAdjust(std::vector<Point3d>& points, // positions of points in global coordinate system (input and output)
310 const std::vector<std::vector<Point2d> >& imagePoints, // projections of 3d points for every camera
311 const std::vector<std::vector<int> >& visibility, // visibility of 3d points for every camera
312 std::vector<Mat>& cameraMatrix, // intrinsic matrices of all cameras (input and output)
313 std::vector<Mat>& R, // rotation matrices of all cameras (input and output)
314 std::vector<Mat>& T, // translation vector of all cameras (input and output)
315 std::vector<Mat>& distCoeffs, // distortion coefficients of all cameras (input and output)
316 const TermCriteria& criteria=
317 TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, DBL_EPSILON),
318 BundleAdjustCallback cb = 0, void* user_data = 0);
321 virtual void optimize(CvMat &_vis); //main function that runs minimization
323 //iteratively asks for measurement for visible camera-point pairs
324 void ask_for_proj(CvMat &_vis,bool once=false);
325 //iteratively asks for Jacobians for every camera_point pair
326 void ask_for_projac(CvMat &_vis);
328 CvMat* err; //error X-hX
329 double prevErrNorm, errNorm;
331 CvTermCriteria criteria;
334 CvMat** U; //size of array is equal to number of cameras
335 CvMat** V; //size of array is equal to number of points
336 CvMat** inv_V_star; //inverse of V*
342 CvMat* X; //measurement
343 CvMat* hX; //current measurement extimation given new parameter vector
345 CvMat* prevP; //current already accepted parameter.
346 CvMat* P; // parameters used to evaluate function with new params
347 // this parameters may be rejected
349 CvMat* deltaP; //computed increase of parameters (result of normal system solution )
351 CvMat** ea; // sum_i AijT * e_ij , used as right part of normal equation
352 // length of array is j = number of cameras
353 CvMat** eb; // sum_j BijT * e_ij , used as right part of normal equation
354 // length of array is i = number of points
356 CvMat** Yj; //length of array is i = num_points
358 CvMat* S; //big matrix of block Sjk , each block has size num_cam_params x num_cam_params
360 CvMat* JtJ_diag; //diagonal of JtJ, used to backup diagonal elements before augmentation
362 CvMat* Vis_index; // matrix which element is index of measurement for point i and camera j
370 //target function and jacobian pointers, which needs to be initialized
371 void (*fjac)(int i, int j, Mat& point_params, Mat& cam_params, Mat& A, Mat& B, void* data);
372 void (*func)(int i, int j, Mat& point_params, Mat& cam_params, Mat& estim, void* data);
376 BundleAdjustCallback cb;
382 #endif /* __cplusplus */
384 #endif /* __OPENCV_CONTRIB_COMPAT_HPP__ */