tldDetector.hpp 4.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

Vladimir's avatar
Vladimir committed
42 43 44 45
#ifndef OPENCV_TLD_DETECTOR
#define OPENCV_TLD_DETECTOR

#include "precomp.hpp"
46
#include "opencl_kernels_tracking.hpp"
Vladimir's avatar
Vladimir committed
47 48 49 50 51 52 53 54 55 56 57 58 59
#include "tldEnsembleClassifier.hpp"
#include "tldUtils.hpp"

namespace cv
{
	namespace tld
	{
		const int STANDARD_PATCH_SIZE = 15;
		const int NEG_EXAMPLES_IN_INIT_MODEL = 300;
		const int MAX_EXAMPLES_IN_MODEL = 500;
		const int MEASURES_PER_CLASSIFIER = 13;
		const int GRIDSIZE = 15;
		const int DOWNSCALE_MODE = cv::INTER_LINEAR;
60
        const double THETA_NN = 0.5;
Vladimir's avatar
Vladimir committed
61
		const double CORE_THRESHOLD = 0.5;
62
        const double CLASSIFIER_MARGIN = 0.1;
Vladimir's avatar
Vladimir committed
63 64 65 66 67 68 69
		const double SCALE_STEP = 1.2;
		const double ENSEMBLE_THRESHOLD = 0.5;
		const double VARIANCE_THRESHOLD = 0.5;
		const double NEXPERT_THRESHOLD = 0.2;

		static const cv::Size GaussBlurKernelSize(3, 3);

70 71


Vladimir's avatar
Vladimir committed
72 73 74 75 76
		class TLDDetector
		{
		public:
			TLDDetector(){}
			~TLDDetector(){}
Vladimir's avatar
Vladimir committed
77 78
			double ensembleClassifierNum(const uchar* data);
			void prepareClassifiers(int rowstep);
79 80
			double Sr(const Mat_<uchar>& patch) const;
			double Sc(const Mat_<uchar>& patch) const;
81
            std::pair<double, double> SrAndSc(const Mat_<uchar>& patch) const;
82 83
#ifdef HAVE_OPENCL
			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
Vladimir's avatar
Vladimir committed
87 88

			std::vector<TLDEnsembleClassifier> classifiers;
89 90
			Mat *posExp, *negExp;
			int *posNum, *negNum;
Vladimir's avatar
Vladimir committed
91 92
			std::vector<Mat_<uchar> > *positiveExamples, *negativeExamples;
			std::vector<int> *timeStampsPositive, *timeStampsNegative;
Vladimir's avatar
Vladimir committed
93
			double *originalVariancePtr;
94 95 96 97 98 99
			std::vector<double> scValues, srValues;
			std::vector<Mat_<uchar> > standardPatches;

			std::vector <Mat> resized_imgs, blurred_imgs;
			std::vector <Point> varBuffer, ensBuffer;
			std::vector <int> varScaleIDs, ensScaleIDs;
Vladimir's avatar
Vladimir committed
100 101 102 103 104 105

			static void generateScanGrid(int rows, int cols, Size initBox, std::vector<Rect2d>& res, bool withScaling = false);
			struct LabeledPatch
			{
				Rect2d rect;
				bool isObject, shouldBeIntegrated;
106
                double Sc;
Vladimir's avatar
Vladimir committed
107 108
			};
			bool detect(const Mat& img, const Mat& imgBlurred, Rect2d& res, std::vector<LabeledPatch>& patches, Size initSize);
109
			bool ocl_detect(const Mat& img, const Mat& imgBlurred, Rect2d& res, std::vector<LabeledPatch>& patches,  Size initSize);
Vladimir's avatar
Vladimir committed
110 111

			friend class MyMouseCallbackDEBUG;
112 113
			static void computeIntegralImages(const Mat& img, Mat_<double>& intImgP, Mat_<double>& intImgP2){ integral(img, intImgP, intImgP2, CV_64F); }
			static inline bool patchVariance(Mat_<double>& intImgP, Mat_<double>& intImgP2, double *originalVariance, Point pt, Size size);
114 115 116

        protected:
            double computeSminus(const Mat_<uchar>& patch) const;
Vladimir's avatar
Vladimir committed
117
		};
118 119


Vladimir's avatar
Vladimir committed
120 121 122
	}
}

123
#endif