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