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) 2000-2008, Intel Corporation, all rights reserved. 14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved. 15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved. 16 // Third party copyrights are property of their respective owners. 17 // 18 // Redistribution and use in source and binary forms, with or without modification, 19 // are permitted provided that the following conditions are met: 20 // 21 // * Redistribution's of source code must retain the above copyright notice, 22 // this list of conditions and the following disclaimer. 23 // 24 // * Redistribution's in binary form must reproduce the above copyright notice, 25 // this list of conditions and the following disclaimer in the documentation 26 // and/or other materials provided with the distribution. 27 // 28 // * The name of the copyright holders may not be used to endorse or promote products 29 // derived from this software without specific prior written permission. 30 // 31 // This software is provided by the copyright holders and contributors "as is" and 32 // any express or implied warranties, including, but not limited to, the implied 33 // warranties of merchantability and fitness for a particular purpose are disclaimed. 34 // In no event shall the Intel Corporation or contributors be liable for any direct, 35 // indirect, incidental, special, exemplary, or consequential damages 36 // (including, but not limited to, procurement of substitute goods or services; 37 // loss of use, data, or profits; or business interruption) however caused 38 // and on any theory of liability, whether in contract, strict liability, 39 // or tort (including negligence or otherwise) arising in any way out of 40 // the use of this software, even if advised of the possibility of such damage. 41 // 42 //M*/ 43 44 #ifndef OPENCV_OBJDETECT_DBT_HPP 45 #define OPENCV_OBJDETECT_DBT_HPP 46 47 #include <opencv2/core.hpp> 48 49 #include <vector> 50 51 namespace cv 52 { 53 54 //! @addtogroup objdetect 55 //! @{ 56 57 class CV_EXPORTS DetectionBasedTracker 58 { 59 public: 60 struct CV_EXPORTS Parameters 61 { 62 int maxTrackLifetime; 63 int minDetectionPeriod; //the minimal time between run of the big object detector (on the whole frame) in ms (1000 mean 1 sec), default=0 64 65 Parameters(); 66 }; 67 68 class IDetector 69 { 70 public: IDetector()71 IDetector(): 72 minObjSize(96, 96), 73 maxObjSize(INT_MAX, INT_MAX), 74 minNeighbours(2), 75 scaleFactor(1.1f) 76 {} 77 78 virtual void detect(const cv::Mat& image, std::vector<cv::Rect>& objects) = 0; 79 setMinObjectSize(const cv::Size & min)80 void setMinObjectSize(const cv::Size& min) 81 { 82 minObjSize = min; 83 } setMaxObjectSize(const cv::Size & max)84 void setMaxObjectSize(const cv::Size& max) 85 { 86 maxObjSize = max; 87 } getMinObjectSize() const88 cv::Size getMinObjectSize() const 89 { 90 return minObjSize; 91 } getMaxObjectSize() const92 cv::Size getMaxObjectSize() const 93 { 94 return maxObjSize; 95 } getScaleFactor()96 float getScaleFactor() 97 { 98 return scaleFactor; 99 } setScaleFactor(float value)100 void setScaleFactor(float value) 101 { 102 scaleFactor = value; 103 } getMinNeighbours()104 int getMinNeighbours() 105 { 106 return minNeighbours; 107 } setMinNeighbours(int value)108 void setMinNeighbours(int value) 109 { 110 minNeighbours = value; 111 } ~IDetector()112 virtual ~IDetector() {} 113 114 protected: 115 cv::Size minObjSize; 116 cv::Size maxObjSize; 117 int minNeighbours; 118 float scaleFactor; 119 }; 120 121 DetectionBasedTracker(cv::Ptr<IDetector> mainDetector, cv::Ptr<IDetector> trackingDetector, const Parameters& params); 122 virtual ~DetectionBasedTracker(); 123 124 virtual bool run(); 125 virtual void stop(); 126 virtual void resetTracking(); 127 128 virtual void process(const cv::Mat& imageGray); 129 130 bool setParameters(const Parameters& params); 131 const Parameters& getParameters() const; 132 133 134 typedef std::pair<cv::Rect, int> Object; 135 virtual void getObjects(std::vector<cv::Rect>& result) const; 136 virtual void getObjects(std::vector<Object>& result) const; 137 138 enum ObjectStatus 139 { 140 DETECTED_NOT_SHOWN_YET, 141 DETECTED, 142 DETECTED_TEMPORARY_LOST, 143 WRONG_OBJECT 144 }; 145 struct ExtObject 146 { 147 int id; 148 cv::Rect location; 149 ObjectStatus status; ExtObjectcv::DetectionBasedTracker::ExtObject150 ExtObject(int _id, cv::Rect _location, ObjectStatus _status) 151 :id(_id), location(_location), status(_status) 152 { 153 } 154 }; 155 virtual void getObjects(std::vector<ExtObject>& result) const; 156 157 158 virtual int addObject(const cv::Rect& location); //returns id of the new object 159 160 protected: 161 class SeparateDetectionWork; 162 cv::Ptr<SeparateDetectionWork> separateDetectionWork; 163 friend void* workcycleObjectDetectorFunction(void* p); 164 165 struct InnerParameters 166 { 167 int numLastPositionsToTrack; 168 int numStepsToWaitBeforeFirstShow; 169 int numStepsToTrackWithoutDetectingIfObjectHasNotBeenShown; 170 int numStepsToShowWithoutDetecting; 171 172 float coeffTrackingWindowSize; 173 float coeffObjectSizeToTrack; 174 float coeffObjectSpeedUsingInPrediction; 175 176 InnerParameters(); 177 }; 178 Parameters parameters; 179 InnerParameters innerParameters; 180 181 struct TrackedObject 182 { 183 typedef std::vector<cv::Rect> PositionsVector; 184 185 PositionsVector lastPositions; 186 187 int numDetectedFrames; 188 int numFramesNotDetected; 189 int id; 190 TrackedObjectcv::DetectionBasedTracker::TrackedObject191 TrackedObject(const cv::Rect& rect):numDetectedFrames(1), numFramesNotDetected(0) 192 { 193 lastPositions.push_back(rect); 194 id=getNextId(); 195 }; 196 getNextIdcv::DetectionBasedTracker::TrackedObject197 static int getNextId() 198 { 199 static int _id=0; 200 return _id++; 201 } 202 }; 203 204 int numTrackedSteps; 205 std::vector<TrackedObject> trackedObjects; 206 207 std::vector<float> weightsPositionsSmoothing; 208 std::vector<float> weightsSizesSmoothing; 209 210 cv::Ptr<IDetector> cascadeForTracking; 211 212 void updateTrackedObjects(const std::vector<cv::Rect>& detectedObjects); 213 cv::Rect calcTrackedObjectPositionToShow(int i) const; 214 cv::Rect calcTrackedObjectPositionToShow(int i, ObjectStatus& status) const; 215 void detectInRegion(const cv::Mat& img, const cv::Rect& r, std::vector<cv::Rect>& detectedObjectsInRegions); 216 }; 217 218 //! @} objdetect 219 220 } //end of cv namespace 221 222 #endif 223