background_segm.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_BACKGROUND_SEGM_HPP
  44. #define OPENCV_BACKGROUND_SEGM_HPP
  45. #include "opencv2/core.hpp"
  46. namespace cv
  47. {
  48. //! @addtogroup video_motion
  49. //! @{
  50. /** @brief Base class for background/foreground segmentation. :
  51. The class is only used to define the common interface for the whole family of background/foreground
  52. segmentation algorithms.
  53. */
  54. class CV_EXPORTS_W BackgroundSubtractor : public Algorithm
  55. {
  56. public:
  57. /** @brief Computes a foreground mask.
  58. @param image Next video frame.
  59. @param fgmask The output foreground mask as an 8-bit binary image.
  60. @param learningRate The value between 0 and 1 that indicates how fast the background model is
  61. learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
  62. rate. 0 means that the background model is not updated at all, 1 means that the background model
  63. is completely reinitialized from the last frame.
  64. */
  65. CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) = 0;
  66. /** @brief Computes a background image.
  67. @param backgroundImage The output background image.
  68. @note Sometimes the background image can be very blurry, as it contain the average background
  69. statistics.
  70. */
  71. CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const = 0;
  72. };
  73. /** @brief Gaussian Mixture-based Background/Foreground Segmentation Algorithm.
  74. The class implements the Gaussian mixture model background subtraction described in @cite Zivkovic2004
  75. and @cite Zivkovic2006 .
  76. */
  77. class CV_EXPORTS_W BackgroundSubtractorMOG2 : public BackgroundSubtractor
  78. {
  79. public:
  80. /** @brief Returns the number of last frames that affect the background model
  81. */
  82. CV_WRAP virtual int getHistory() const = 0;
  83. /** @brief Sets the number of last frames that affect the background model
  84. */
  85. CV_WRAP virtual void setHistory(int history) = 0;
  86. /** @brief Returns the number of gaussian components in the background model
  87. */
  88. CV_WRAP virtual int getNMixtures() const = 0;
  89. /** @brief Sets the number of gaussian components in the background model.
  90. The model needs to be reinitalized to reserve memory.
  91. */
  92. CV_WRAP virtual void setNMixtures(int nmixtures) = 0;//needs reinitialization!
  93. /** @brief Returns the "background ratio" parameter of the algorithm
  94. If a foreground pixel keeps semi-constant value for about backgroundRatio\*history frames, it's
  95. considered background and added to the model as a center of a new component. It corresponds to TB
  96. parameter in the paper.
  97. */
  98. CV_WRAP virtual double getBackgroundRatio() const = 0;
  99. /** @brief Sets the "background ratio" parameter of the algorithm
  100. */
  101. CV_WRAP virtual void setBackgroundRatio(double ratio) = 0;
  102. /** @brief Returns the variance threshold for the pixel-model match
  103. The main threshold on the squared Mahalanobis distance to decide if the sample is well described by
  104. the background model or not. Related to Cthr from the paper.
  105. */
  106. CV_WRAP virtual double getVarThreshold() const = 0;
  107. /** @brief Sets the variance threshold for the pixel-model match
  108. */
  109. CV_WRAP virtual void setVarThreshold(double varThreshold) = 0;
  110. /** @brief Returns the variance threshold for the pixel-model match used for new mixture component generation
  111. Threshold for the squared Mahalanobis distance that helps decide when a sample is close to the
  112. existing components (corresponds to Tg in the paper). If a pixel is not close to any component, it
  113. is considered foreground or added as a new component. 3 sigma =\> Tg=3\*3=9 is default. A smaller Tg
  114. value generates more components. A higher Tg value may result in a small number of components but
  115. they can grow too large.
  116. */
  117. CV_WRAP virtual double getVarThresholdGen() const = 0;
  118. /** @brief Sets the variance threshold for the pixel-model match used for new mixture component generation
  119. */
  120. CV_WRAP virtual void setVarThresholdGen(double varThresholdGen) = 0;
  121. /** @brief Returns the initial variance of each gaussian component
  122. */
  123. CV_WRAP virtual double getVarInit() const = 0;
  124. /** @brief Sets the initial variance of each gaussian component
  125. */
  126. CV_WRAP virtual void setVarInit(double varInit) = 0;
  127. CV_WRAP virtual double getVarMin() const = 0;
  128. CV_WRAP virtual void setVarMin(double varMin) = 0;
  129. CV_WRAP virtual double getVarMax() const = 0;
  130. CV_WRAP virtual void setVarMax(double varMax) = 0;
  131. /** @brief Returns the complexity reduction threshold
  132. This parameter defines the number of samples needed to accept to prove the component exists. CT=0.05
  133. is a default value for all the samples. By setting CT=0 you get an algorithm very similar to the
  134. standard Stauffer&Grimson algorithm.
  135. */
  136. CV_WRAP virtual double getComplexityReductionThreshold() const = 0;
  137. /** @brief Sets the complexity reduction threshold
  138. */
  139. CV_WRAP virtual void setComplexityReductionThreshold(double ct) = 0;
  140. /** @brief Returns the shadow detection flag
  141. If true, the algorithm detects shadows and marks them. See createBackgroundSubtractorMOG2 for
  142. details.
  143. */
  144. CV_WRAP virtual bool getDetectShadows() const = 0;
  145. /** @brief Enables or disables shadow detection
  146. */
  147. CV_WRAP virtual void setDetectShadows(bool detectShadows) = 0;
  148. /** @brief Returns the shadow value
  149. Shadow value is the value used to mark shadows in the foreground mask. Default value is 127. Value 0
  150. in the mask always means background, 255 means foreground.
  151. */
  152. CV_WRAP virtual int getShadowValue() const = 0;
  153. /** @brief Sets the shadow value
  154. */
  155. CV_WRAP virtual void setShadowValue(int value) = 0;
  156. /** @brief Returns the shadow threshold
  157. A shadow is detected if pixel is a darker version of the background. The shadow threshold (Tau in
  158. the paper) is a threshold defining how much darker the shadow can be. Tau= 0.5 means that if a pixel
  159. is more than twice darker then it is not shadow. See Prati, Mikic, Trivedi and Cucchiara,
  160. *Detecting Moving Shadows...*, IEEE PAMI,2003.
  161. */
  162. CV_WRAP virtual double getShadowThreshold() const = 0;
  163. /** @brief Sets the shadow threshold
  164. */
  165. CV_WRAP virtual void setShadowThreshold(double threshold) = 0;
  166. /** @brief Computes a foreground mask.
  167. @param image Next video frame. Floating point frame will be used without scaling and should be in range \f$[0,255]\f$.
  168. @param fgmask The output foreground mask as an 8-bit binary image.
  169. @param learningRate The value between 0 and 1 that indicates how fast the background model is
  170. learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
  171. rate. 0 means that the background model is not updated at all, 1 means that the background model
  172. is completely reinitialized from the last frame.
  173. */
  174. CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
  175. };
  176. /** @brief Creates MOG2 Background Subtractor
  177. @param history Length of the history.
  178. @param varThreshold Threshold on the squared Mahalanobis distance between the pixel and the model
  179. to decide whether a pixel is well described by the background model. This parameter does not
  180. affect the background update.
  181. @param detectShadows If true, the algorithm will detect shadows and mark them. It decreases the
  182. speed a bit, so if you do not need this feature, set the parameter to false.
  183. */
  184. CV_EXPORTS_W Ptr<BackgroundSubtractorMOG2>
  185. createBackgroundSubtractorMOG2(int history=500, double varThreshold=16,
  186. bool detectShadows=true);
  187. /** @brief K-nearest neighbours - based Background/Foreground Segmentation Algorithm.
  188. The class implements the K-nearest neighbours background subtraction described in @cite Zivkovic2006 .
  189. Very efficient if number of foreground pixels is low.
  190. */
  191. class CV_EXPORTS_W BackgroundSubtractorKNN : public BackgroundSubtractor
  192. {
  193. public:
  194. /** @brief Returns the number of last frames that affect the background model
  195. */
  196. CV_WRAP virtual int getHistory() const = 0;
  197. /** @brief Sets the number of last frames that affect the background model
  198. */
  199. CV_WRAP virtual void setHistory(int history) = 0;
  200. /** @brief Returns the number of data samples in the background model
  201. */
  202. CV_WRAP virtual int getNSamples() const = 0;
  203. /** @brief Sets the number of data samples in the background model.
  204. The model needs to be reinitalized to reserve memory.
  205. */
  206. CV_WRAP virtual void setNSamples(int _nN) = 0;//needs reinitialization!
  207. /** @brief Returns the threshold on the squared distance between the pixel and the sample
  208. The threshold on the squared distance between the pixel and the sample to decide whether a pixel is
  209. close to a data sample.
  210. */
  211. CV_WRAP virtual double getDist2Threshold() const = 0;
  212. /** @brief Sets the threshold on the squared distance
  213. */
  214. CV_WRAP virtual void setDist2Threshold(double _dist2Threshold) = 0;
  215. /** @brief Returns the number of neighbours, the k in the kNN.
  216. K is the number of samples that need to be within dist2Threshold in order to decide that that
  217. pixel is matching the kNN background model.
  218. */
  219. CV_WRAP virtual int getkNNSamples() const = 0;
  220. /** @brief Sets the k in the kNN. How many nearest neighbours need to match.
  221. */
  222. CV_WRAP virtual void setkNNSamples(int _nkNN) = 0;
  223. /** @brief Returns the shadow detection flag
  224. If true, the algorithm detects shadows and marks them. See createBackgroundSubtractorKNN for
  225. details.
  226. */
  227. CV_WRAP virtual bool getDetectShadows() const = 0;
  228. /** @brief Enables or disables shadow detection
  229. */
  230. CV_WRAP virtual void setDetectShadows(bool detectShadows) = 0;
  231. /** @brief Returns the shadow value
  232. Shadow value is the value used to mark shadows in the foreground mask. Default value is 127. Value 0
  233. in the mask always means background, 255 means foreground.
  234. */
  235. CV_WRAP virtual int getShadowValue() const = 0;
  236. /** @brief Sets the shadow value
  237. */
  238. CV_WRAP virtual void setShadowValue(int value) = 0;
  239. /** @brief Returns the shadow threshold
  240. A shadow is detected if pixel is a darker version of the background. The shadow threshold (Tau in
  241. the paper) is a threshold defining how much darker the shadow can be. Tau= 0.5 means that if a pixel
  242. is more than twice darker then it is not shadow. See Prati, Mikic, Trivedi and Cucchiara,
  243. *Detecting Moving Shadows...*, IEEE PAMI,2003.
  244. */
  245. CV_WRAP virtual double getShadowThreshold() const = 0;
  246. /** @brief Sets the shadow threshold
  247. */
  248. CV_WRAP virtual void setShadowThreshold(double threshold) = 0;
  249. };
  250. /** @brief Creates KNN Background Subtractor
  251. @param history Length of the history.
  252. @param dist2Threshold Threshold on the squared distance between the pixel and the sample to decide
  253. whether a pixel is close to that sample. This parameter does not affect the background update.
  254. @param detectShadows If true, the algorithm will detect shadows and mark them. It decreases the
  255. speed a bit, so if you do not need this feature, set the parameter to false.
  256. */
  257. CV_EXPORTS_W Ptr<BackgroundSubtractorKNN>
  258. createBackgroundSubtractorKNN(int history=500, double dist2Threshold=400.0,
  259. bool detectShadows=true);
  260. //! @} video_motion
  261. } // cv
  262. #endif