tldTracker.hpp 5.41 KB
Newer Older
Vladimir's avatar
Vladimir committed
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
/*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*/
41

Vladimir's avatar
Vladimir committed
42 43 44 45 46 47 48 49 50 51
#ifndef OPENCV_TLD_TRACKER
#define OPENCV_TLD_TRACKER

#include "precomp.hpp"
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc.hpp"
#include "tldModel.hpp"
#include<algorithm>
#include<limits.h>

Vladimir's avatar
Vladimir committed
52
namespace cv
Vladimir's avatar
Vladimir committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
{

namespace tld
{
class TrackerProxy
{
public:
	virtual bool init(const Mat& image, const Rect2d& boundingBox) = 0;
	virtual bool update(const Mat& image, Rect2d& boundingBox) = 0;
	virtual ~TrackerProxy(){}
};


class MyMouseCallbackDEBUG
{
public:
	MyMouseCallbackDEBUG(Mat& img, Mat& imgBlurred, TLDDetector* detector) :img_(img), imgBlurred_(imgBlurred), detector_(detector){}
	static void onMouse(int event, int x, int y, int, void* obj){ ((MyMouseCallbackDEBUG*)obj)->onMouse(event, x, y); }
	MyMouseCallbackDEBUG& operator = (const MyMouseCallbackDEBUG& /*other*/){ return *this; }
private:
	void onMouse(int event, int x, int y);
	Mat& img_, imgBlurred_;
	TLDDetector* detector_;
};


class Data
{
public:
	Data(Rect2d initBox);
	Size getMinSize(){ return minSize; }
	double getScale(){ return scale; }
	bool confident;
	bool failedLastTime;
	int frameNum;
	void printme(FILE*  port = stdout);
private:
	double scale;
	Size minSize;
};

template<class T, class Tparams>
class TrackerProxyImpl : public TrackerProxy
{
public:
	TrackerProxyImpl(Tparams params = Tparams()) :params_(params){}
99
	bool init(const Mat& image, const Rect2d& boundingBox) CV_OVERRIDE
Vladimir's avatar
Vladimir committed
100
	{
101
        trackerPtr = T::create();
Vladimir's avatar
Vladimir committed
102 103
		return trackerPtr->init(image, boundingBox);
	}
104
	bool update(const Mat& image, Rect2d& boundingBox) CV_OVERRIDE
Vladimir's avatar
Vladimir committed
105 106 107 108 109 110 111 112 113 114
	{
		return trackerPtr->update(image, boundingBox);
	}
private:
	Ptr<T> trackerPtr;
	Tparams params_;
	Rect2d boundingBox_;
};


115
#undef BLUR_AS_VADIM
Vladimir's avatar
Vladimir committed
116 117 118 119 120 121
#undef CLOSED_LOOP

class TrackerTLDImpl : public TrackerTLD
{
public:
	TrackerTLDImpl(const TrackerTLD::Params &parameters = TrackerTLD::Params());
122 123
	void read(const FileNode& fn) CV_OVERRIDE;
	void write(FileStorage& fs) const CV_OVERRIDE;
Vladimir's avatar
Vladimir committed
124

125 126 127 128 129
    Ptr<TrackerModel> getModel()
    {
      return model;
    }

Vladimir's avatar
Vladimir committed
130 131 132 133 134 135 136 137 138
	class Pexpert
	{
	public:
		Pexpert(const Mat& img_in, const Mat& imgBlurred_in, Rect2d& resultBox_in,
			const TLDDetector* detector_in, TrackerTLD::Params params_in, Size initSize_in) :
			img_(img_in), imgBlurred_(imgBlurred_in), resultBox_(resultBox_in), detector_(detector_in), params_(params_in), initSize_(initSize_in){}
		bool operator()(Rect2d /*box*/){ return false; }
		int additionalExamples(std::vector<Mat_<uchar> >& examplesForModel, std::vector<Mat_<uchar> >& examplesForEnsemble);
	protected:
139
		Pexpert() : detector_(NULL) {}
Vladimir's avatar
Vladimir committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
		Mat img_, imgBlurred_;
		Rect2d resultBox_;
		const TLDDetector* detector_;
		TrackerTLD::Params params_;
		RNG rng;
		Size initSize_;
	};

	class Nexpert : public Pexpert
	{
	public:
		Nexpert(const Mat& img_in, Rect2d& resultBox_in, const TLDDetector* detector_in, TrackerTLD::Params params_in)
		{
			img_ = img_in; resultBox_ = resultBox_in; detector_ = detector_in; params_ = params_in;
		}
		bool operator()(Rect2d box);
		int additionalExamples(std::vector<Mat_<uchar> >& examplesForModel, std::vector<Mat_<uchar> >& examplesForEnsemble)
		{
			examplesForModel.clear(); examplesForEnsemble.clear(); return 0;
		}
	};

162 163
	bool initImpl(const Mat& image, const Rect2d& boundingBox) CV_OVERRIDE;
	bool updateImpl(const Mat& image, Rect2d& boundingBox) CV_OVERRIDE;
Vladimir's avatar
Vladimir committed
164 165 166 167 168 169 170 171 172 173

	TrackerTLD::Params params;
	Ptr<Data> data;
	Ptr<TrackerProxy> trackerProxy;

};

}
}

174
#endif