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 #if defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(__ANDROID__) || \ 48 (defined(__cplusplus) && __cplusplus > 201103L) || (defined(_MSC_VER) && _MSC_VER >= 1700) 49 50 #include <vector> 51 52 namespace cv 53 { 54 55 //! @addtogroup objdetect 56 //! @{ 57 58 class CV_EXPORTS DetectionBasedTracker 59 { 60 public: 61 struct Parameters 62 { 63 int maxTrackLifetime; 64 int minDetectionPeriod; //the minimal time between run of the big object detector (on the whole frame) in ms (1000 mean 1 sec), default=0 65 66 Parameters(); 67 }; 68 69 class IDetector 70 { 71 public: 72 IDetector(): 73 minObjSize(96, 96), 74 maxObjSize(INT_MAX, INT_MAX), 75 minNeighbours(2), 76 scaleFactor(1.1f) 77 {} 78 79 virtual void detect(const cv::Mat& image, std::vector<cv::Rect>& objects) = 0; 80 81 void setMinObjectSize(const cv::Size& min) 82 { 83 minObjSize = min; 84 } 85 void setMaxObjectSize(const cv::Size& max) 86 { 87 maxObjSize = max; 88 } 89 cv::Size getMinObjectSize() const 90 { 91 return minObjSize; 92 } 93 cv::Size getMaxObjectSize() const 94 { 95 return maxObjSize; 96 } 97 float getScaleFactor() 98 { 99 return scaleFactor; 100 } 101 void setScaleFactor(float value) 102 { 103 scaleFactor = value; 104 } 105 int getMinNeighbours() 106 { 107 return minNeighbours; 108 } 109 void setMinNeighbours(int value) 110 { 111 minNeighbours = value; 112 } 113 virtual ~IDetector() {} 114 115 protected: 116 cv::Size minObjSize; 117 cv::Size maxObjSize; 118 int minNeighbours; 119 float scaleFactor; 120 }; 121 122 DetectionBasedTracker(cv::Ptr<IDetector> mainDetector, cv::Ptr<IDetector> trackingDetector, const Parameters& params); 123 virtual ~DetectionBasedTracker(); 124 125 virtual bool run(); 126 virtual void stop(); 127 virtual void resetTracking(); 128 129 virtual void process(const cv::Mat& imageGray); 130 131 bool setParameters(const Parameters& params); 132 const Parameters& getParameters() const; 133 134 135 typedef std::pair<cv::Rect, int> Object; 136 virtual void getObjects(std::vector<cv::Rect>& result) const; 137 virtual void getObjects(std::vector<Object>& result) const; 138 139 enum ObjectStatus 140 { 141 DETECTED_NOT_SHOWN_YET, 142 DETECTED, 143 DETECTED_TEMPORARY_LOST, 144 WRONG_OBJECT 145 }; 146 struct ExtObject 147 { 148 int id; 149 cv::Rect location; 150 ObjectStatus status; 151 ExtObject(int _id, cv::Rect _location, ObjectStatus _status) 152 :id(_id), location(_location), status(_status) 153 { 154 } 155 }; 156 virtual void getObjects(std::vector<ExtObject>& result) const; 157 158 159 virtual int addObject(const cv::Rect& location); //returns id of the new object 160 161 protected: 162 class SeparateDetectionWork; 163 cv::Ptr<SeparateDetectionWork> separateDetectionWork; 164 friend void* workcycleObjectDetectorFunction(void* p); 165 166 struct InnerParameters 167 { 168 int numLastPositionsToTrack; 169 int numStepsToWaitBeforeFirstShow; 170 int numStepsToTrackWithoutDetectingIfObjectHasNotBeenShown; 171 int numStepsToShowWithoutDetecting; 172 173 float coeffTrackingWindowSize; 174 float coeffObjectSizeToTrack; 175 float coeffObjectSpeedUsingInPrediction; 176 177 InnerParameters(); 178 }; 179 Parameters parameters; 180 InnerParameters innerParameters; 181 182 struct TrackedObject 183 { 184 typedef std::vector<cv::Rect> PositionsVector; 185 186 PositionsVector lastPositions; 187 188 int numDetectedFrames; 189 int numFramesNotDetected; 190 int id; 191 192 TrackedObject(const cv::Rect& rect):numDetectedFrames(1), numFramesNotDetected(0) 193 { 194 lastPositions.push_back(rect); 195 id=getNextId(); 196 }; 197 198 static int getNextId() 199 { 200 static int _id=0; 201 return _id++; 202 } 203 }; 204 205 int numTrackedSteps; 206 std::vector<TrackedObject> trackedObjects; 207 208 std::vector<float> weightsPositionsSmoothing; 209 std::vector<float> weightsSizesSmoothing; 210 211 cv::Ptr<IDetector> cascadeForTracking; 212 213 void updateTrackedObjects(const std::vector<cv::Rect>& detectedObjects); 214 cv::Rect calcTrackedObjectPositionToShow(int i) const; 215 cv::Rect calcTrackedObjectPositionToShow(int i, ObjectStatus& status) const; 216 void detectInRegion(const cv::Mat& img, const cv::Rect& r, std::vector<cv::Rect>& detectedObjectsInRegions); 217 }; 218 219 //! @} objdetect 220 221 } //end of cv namespace 222 #endif 223 224 #endif 225