detection_based_tracker.hpp 7.5 KB

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