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) 2013, OpenCV Foundation, 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 the copyright holders 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 OPENCV_TLD_DETECTOR
43 #define OPENCV_TLD_DETECTOR
44 
45 #include "opencl_kernels_tracking.hpp"
46 #include "tldEnsembleClassifier.hpp"
47 #include "tldUtils.hpp"
48 
49 namespace cv {
50 inline namespace tracking {
51 namespace impl {
52 namespace tld {
53 
54 		const int STANDARD_PATCH_SIZE = 15;
55 		const int NEG_EXAMPLES_IN_INIT_MODEL = 300;
56 		const int MAX_EXAMPLES_IN_MODEL = 500;
57 		const int MEASURES_PER_CLASSIFIER = 13;
58 		const int GRIDSIZE = 15;
59 		const int DOWNSCALE_MODE = cv::INTER_LINEAR_EXACT;
60         const double THETA_NN = 0.5;
61 		const double CORE_THRESHOLD = 0.5;
62         const double CLASSIFIER_MARGIN = 0.1;
63 		const double SCALE_STEP = 1.2;
64 		const double ENSEMBLE_THRESHOLD = 0.5;
65 		const double VARIANCE_THRESHOLD = 0.5;
66 		const double NEXPERT_THRESHOLD = 0.2;
67 
68 		static const cv::Size GaussBlurKernelSize(3, 3);
69 
70 
71 
72 		class TLDDetector
73 		{
74 		public:
TLDDetector()75 			TLDDetector(){}
~TLDDetector()76 			~TLDDetector(){}
77 			double ensembleClassifierNum(const uchar* data);
78 			void prepareClassifiers(int rowstep);
79 			double Sr(const Mat_<uchar>& patch) const;
80 			double Sc(const Mat_<uchar>& patch) const;
81             std::pair<double, double> SrAndSc(const Mat_<uchar>& patch) const;
82 #ifdef HAVE_OPENCL
83 			double ocl_Sr(const Mat_<uchar>& patch);
84 			double ocl_Sc(const Mat_<uchar>& patch);
85 			void ocl_batchSrSc(const Mat_<uchar>& patches, double *resultSr, double *resultSc, int numOfPatches);
86 #endif
87 
88 			std::vector<TLDEnsembleClassifier> classifiers;
89 			Mat *posExp, *negExp;
90 			int *posNum, *negNum;
91 			std::vector<Mat_<uchar> > *positiveExamples, *negativeExamples;
92 			std::vector<int> *timeStampsPositive, *timeStampsNegative;
93 			double *originalVariancePtr;
94 			std::vector<double> scValues, srValues;
95 			std::vector<Mat_<uchar> > standardPatches;
96 
97 			std::vector <Mat> resized_imgs, blurred_imgs;
98 			std::vector <Point> varBuffer, ensBuffer;
99 			std::vector <int> varScaleIDs, ensScaleIDs;
100 
101 			static void generateScanGrid(int rows, int cols, Size initBox, std::vector<Rect2d>& res, bool withScaling = false);
102 			struct LabeledPatch
103 			{
104 				Rect2d rect;
105 				bool isObject, shouldBeIntegrated;
106 			};
107 			bool detect(const Mat& img, const Mat& imgBlurred, Rect2d& res, std::vector<LabeledPatch>& patches, Size initSize);
108 			bool ocl_detect(const Mat& img, const Mat& imgBlurred, Rect2d& res, std::vector<LabeledPatch>& patches,  Size initSize);
109 
110 			friend class MyMouseCallbackDEBUG;
computeIntegralImages(const Mat & img,Mat_<double> & intImgP,Mat_<double> & intImgP2)111 			static void computeIntegralImages(const Mat& img, Mat_<double>& intImgP, Mat_<double>& intImgP2){ integral(img, intImgP, intImgP2, CV_64F); }
112 			static inline bool patchVariance(Mat_<double>& intImgP, Mat_<double>& intImgP2, double *originalVariance, Point pt, Size size);
113 
114         protected:
115             double computeSminus(const Mat_<uchar>& patch) const;
116 		};
117 
118 
119 }}}}  // namespace
120 
121 #endif
122