detection_based_tracker.hpp 7.5 KB

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