OpenCV 2.4.8 components for OpenCVgrabber.
[mmanager-3rdparty.git] / OpenCV2.4.8 / build / include / opencv2 / contrib / detection_based_tracker.hpp
1 #pragma once
2
3 #if defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(ANDROID)
4
5 #include <opencv2/core/core.hpp>
6 #include <opencv2/objdetect/objdetect.hpp>
7
8 #include <vector>
9
10 class DetectionBasedTracker
11 {
12     public:
13         struct Parameters
14         {
15             int minObjectSize;
16             int maxObjectSize;
17             double scaleFactor;
18             int maxTrackLifetime;
19             int minNeighbors;
20             int minDetectionPeriod; //the minimal time between run of the big object detector (on the whole frame) in ms (1000 mean 1 sec), default=0
21
22             Parameters();
23         };
24
25         DetectionBasedTracker(const std::string& cascadeFilename, const Parameters& params);
26         virtual ~DetectionBasedTracker();
27
28         virtual bool run();
29         virtual void stop();
30         virtual void resetTracking();
31
32         virtual void process(const cv::Mat& imageGray);
33
34         bool setParameters(const Parameters& params);
35         const Parameters& getParameters();
36
37
38         typedef std::pair<cv::Rect, int> Object;
39         virtual void getObjects(std::vector<cv::Rect>& result) const;
40         virtual void getObjects(std::vector<Object>& result) const;
41
42     protected:
43         class SeparateDetectionWork;
44         cv::Ptr<SeparateDetectionWork> separateDetectionWork;
45         friend void* workcycleObjectDetectorFunction(void* p);
46
47
48         struct InnerParameters
49         {
50             int numLastPositionsToTrack;
51             int numStepsToWaitBeforeFirstShow;
52             int numStepsToTrackWithoutDetectingIfObjectHasNotBeenShown;
53             int numStepsToShowWithoutDetecting;
54
55             float coeffTrackingWindowSize;
56             float coeffObjectSizeToTrack;
57             float coeffObjectSpeedUsingInPrediction;
58
59             InnerParameters();
60         };
61         Parameters parameters;
62         InnerParameters innerParameters;
63
64         struct TrackedObject
65         {
66             typedef std::vector<cv::Rect> PositionsVector;
67
68             PositionsVector lastPositions;
69
70             int numDetectedFrames;
71             int numFramesNotDetected;
72             int id;
73
74             TrackedObject(const cv::Rect& rect):numDetectedFrames(1), numFramesNotDetected(0)
75             {
76                 lastPositions.push_back(rect);
77                 id=getNextId();
78             };
79
80             static int getNextId()
81             {
82                 static int _id=0;
83                 return _id++;
84             }
85         };
86
87         int numTrackedSteps;
88         std::vector<TrackedObject> trackedObjects;
89
90         std::vector<float> weightsPositionsSmoothing;
91         std::vector<float> weightsSizesSmoothing;
92
93         cv::CascadeClassifier cascadeForTracking;
94
95
96         void updateTrackedObjects(const std::vector<cv::Rect>& detectedObjects);
97         cv::Rect calcTrackedObjectPositionToShow(int i) const;
98         void detectInRegion(const cv::Mat& img, const cv::Rect& r, std::vector<cv::Rect>& detectedObjectsInRegions);
99 };
100
101 namespace cv
102 {
103     using ::DetectionBasedTracker;
104 } //end of cv namespace
105
106 #endif