detection_based_tracker.hpp 7.5 KB

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