bgsegm.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. By downloading, copying, installing or using the software you agree to this
  3. license. If you do not agree to this license, do not download, install,
  4. copy or use the software.
  5. License Agreement
  6. For Open Source Computer Vision Library
  7. (3-clause BSD License)
  8. Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  9. Third party copyrights are property of their respective owners.
  10. Redistribution and use in source and binary forms, with or without modification,
  11. are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright notice,
  15. this list of conditions and the following disclaimer in the documentation
  16. and/or other materials provided with the distribution.
  17. * Neither the names of the copyright holders nor the names of the contributors
  18. may be used to endorse or promote products derived from this software
  19. without specific prior written permission.
  20. This software is provided by the copyright holders and contributors "as is" and
  21. any express or implied warranties, including, but not limited to, the implied
  22. warranties of merchantability and fitness for a particular purpose are
  23. disclaimed. In no event shall copyright holders or contributors be liable for
  24. any direct, indirect, incidental, special, exemplary, or consequential damages
  25. (including, but not limited to, procurement of substitute goods or services;
  26. loss of use, data, or profits; or business interruption) however caused
  27. and on any theory of liability, whether in contract, strict liability,
  28. or tort (including negligence or otherwise) arising in any way out of
  29. the use of this software, even if advised of the possibility of such damage.
  30. */
  31. #ifndef __OPENCV_BGSEGM_HPP__
  32. #define __OPENCV_BGSEGM_HPP__
  33. #include "opencv2/video.hpp"
  34. #ifdef __cplusplus
  35. /** @defgroup bgsegm Improved Background-Foreground Segmentation Methods
  36. */
  37. namespace cv
  38. {
  39. namespace bgsegm
  40. {
  41. //! @addtogroup bgsegm
  42. //! @{
  43. /** @brief Gaussian Mixture-based Background/Foreground Segmentation Algorithm.
  44. The class implements the algorithm described in @cite KB2001 .
  45. */
  46. class CV_EXPORTS_W BackgroundSubtractorMOG : public BackgroundSubtractor
  47. {
  48. public:
  49. CV_WRAP virtual int getHistory() const = 0;
  50. CV_WRAP virtual void setHistory(int nframes) = 0;
  51. CV_WRAP virtual int getNMixtures() const = 0;
  52. CV_WRAP virtual void setNMixtures(int nmix) = 0;
  53. CV_WRAP virtual double getBackgroundRatio() const = 0;
  54. CV_WRAP virtual void setBackgroundRatio(double backgroundRatio) = 0;
  55. CV_WRAP virtual double getNoiseSigma() const = 0;
  56. CV_WRAP virtual void setNoiseSigma(double noiseSigma) = 0;
  57. };
  58. /** @brief Creates mixture-of-gaussian background subtractor
  59. @param history Length of the history.
  60. @param nmixtures Number of Gaussian mixtures.
  61. @param backgroundRatio Background ratio.
  62. @param noiseSigma Noise strength (standard deviation of the brightness or each color channel). 0
  63. means some automatic value.
  64. */
  65. CV_EXPORTS_W Ptr<BackgroundSubtractorMOG>
  66. createBackgroundSubtractorMOG(int history=200, int nmixtures=5,
  67. double backgroundRatio=0.7, double noiseSigma=0);
  68. /** @brief Background Subtractor module based on the algorithm given in @cite Gold2012 .
  69. Takes a series of images and returns a sequence of mask (8UC1)
  70. images of the same size, where 255 indicates Foreground and 0 represents Background.
  71. This class implements an algorithm described in "Visual Tracking of Human Visitors under
  72. Variable-Lighting Conditions for a Responsive Audio Art Installation," A. Godbehere,
  73. A. Matsukawa, K. Goldberg, American Control Conference, Montreal, June 2012.
  74. */
  75. class CV_EXPORTS_W BackgroundSubtractorGMG : public BackgroundSubtractor
  76. {
  77. public:
  78. /** @brief Returns total number of distinct colors to maintain in histogram.
  79. */
  80. CV_WRAP virtual int getMaxFeatures() const = 0;
  81. /** @brief Sets total number of distinct colors to maintain in histogram.
  82. */
  83. CV_WRAP virtual void setMaxFeatures(int maxFeatures) = 0;
  84. /** @brief Returns the learning rate of the algorithm.
  85. It lies between 0.0 and 1.0. It determines how quickly features are "forgotten" from
  86. histograms.
  87. */
  88. CV_WRAP virtual double getDefaultLearningRate() const = 0;
  89. /** @brief Sets the learning rate of the algorithm.
  90. */
  91. CV_WRAP virtual void setDefaultLearningRate(double lr) = 0;
  92. /** @brief Returns the number of frames used to initialize background model.
  93. */
  94. CV_WRAP virtual int getNumFrames() const = 0;
  95. /** @brief Sets the number of frames used to initialize background model.
  96. */
  97. CV_WRAP virtual void setNumFrames(int nframes) = 0;
  98. /** @brief Returns the parameter used for quantization of color-space.
  99. It is the number of discrete levels in each channel to be used in histograms.
  100. */
  101. CV_WRAP virtual int getQuantizationLevels() const = 0;
  102. /** @brief Sets the parameter used for quantization of color-space
  103. */
  104. CV_WRAP virtual void setQuantizationLevels(int nlevels) = 0;
  105. /** @brief Returns the prior probability that each individual pixel is a background pixel.
  106. */
  107. CV_WRAP virtual double getBackgroundPrior() const = 0;
  108. /** @brief Sets the prior probability that each individual pixel is a background pixel.
  109. */
  110. CV_WRAP virtual void setBackgroundPrior(double bgprior) = 0;
  111. /** @brief Returns the kernel radius used for morphological operations
  112. */
  113. CV_WRAP virtual int getSmoothingRadius() const = 0;
  114. /** @brief Sets the kernel radius used for morphological operations
  115. */
  116. CV_WRAP virtual void setSmoothingRadius(int radius) = 0;
  117. /** @brief Returns the value of decision threshold.
  118. Decision value is the value above which pixel is determined to be FG.
  119. */
  120. CV_WRAP virtual double getDecisionThreshold() const = 0;
  121. /** @brief Sets the value of decision threshold.
  122. */
  123. CV_WRAP virtual void setDecisionThreshold(double thresh) = 0;
  124. /** @brief Returns the status of background model update
  125. */
  126. CV_WRAP virtual bool getUpdateBackgroundModel() const = 0;
  127. /** @brief Sets the status of background model update
  128. */
  129. CV_WRAP virtual void setUpdateBackgroundModel(bool update) = 0;
  130. /** @brief Returns the minimum value taken on by pixels in image sequence. Usually 0.
  131. */
  132. CV_WRAP virtual double getMinVal() const = 0;
  133. /** @brief Sets the minimum value taken on by pixels in image sequence.
  134. */
  135. CV_WRAP virtual void setMinVal(double val) = 0;
  136. /** @brief Returns the maximum value taken on by pixels in image sequence. e.g. 1.0 or 255.
  137. */
  138. CV_WRAP virtual double getMaxVal() const = 0;
  139. /** @brief Sets the maximum value taken on by pixels in image sequence.
  140. */
  141. CV_WRAP virtual void setMaxVal(double val) = 0;
  142. };
  143. /** @brief Creates a GMG Background Subtractor
  144. @param initializationFrames number of frames used to initialize the background models.
  145. @param decisionThreshold Threshold value, above which it is marked foreground, else background.
  146. */
  147. CV_EXPORTS_W Ptr<BackgroundSubtractorGMG> createBackgroundSubtractorGMG(int initializationFrames=120,
  148. double decisionThreshold=0.8);
  149. /** @brief Background subtraction based on counting.
  150. About as fast as MOG2 on a high end system.
  151. More than twice faster than MOG2 on cheap hardware (benchmarked on Raspberry Pi3).
  152. %Algorithm by Sagi Zeevi ( https://github.com/sagi-z/BackgroundSubtractorCNT )
  153. */
  154. class CV_EXPORTS_W BackgroundSubtractorCNT : public BackgroundSubtractor
  155. {
  156. public:
  157. // BackgroundSubtractor interface
  158. CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
  159. CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
  160. /** @brief Returns number of frames with same pixel color to consider stable.
  161. */
  162. CV_WRAP virtual int getMinPixelStability() const = 0;
  163. /** @brief Sets the number of frames with same pixel color to consider stable.
  164. */
  165. CV_WRAP virtual void setMinPixelStability(int value) = 0;
  166. /** @brief Returns maximum allowed credit for a pixel in history.
  167. */
  168. CV_WRAP virtual int getMaxPixelStability() const = 0;
  169. /** @brief Sets the maximum allowed credit for a pixel in history.
  170. */
  171. CV_WRAP virtual void setMaxPixelStability(int value) = 0;
  172. /** @brief Returns if we're giving a pixel credit for being stable for a long time.
  173. */
  174. CV_WRAP virtual bool getUseHistory() const = 0;
  175. /** @brief Sets if we're giving a pixel credit for being stable for a long time.
  176. */
  177. CV_WRAP virtual void setUseHistory(bool value) = 0;
  178. /** @brief Returns if we're parallelizing the algorithm.
  179. */
  180. CV_WRAP virtual bool getIsParallel() const = 0;
  181. /** @brief Sets if we're parallelizing the algorithm.
  182. */
  183. CV_WRAP virtual void setIsParallel(bool value) = 0;
  184. };
  185. /** @brief Creates a CNT Background Subtractor
  186. @param minPixelStability number of frames with same pixel color to consider stable
  187. @param useHistory determines if we're giving a pixel credit for being stable for a long time
  188. @param maxPixelStability maximum allowed credit for a pixel in history
  189. @param isParallel determines if we're parallelizing the algorithm
  190. */
  191. CV_EXPORTS_W Ptr<BackgroundSubtractorCNT>
  192. createBackgroundSubtractorCNT(int minPixelStability = 15,
  193. bool useHistory = true,
  194. int maxPixelStability = 15*60,
  195. bool isParallel = true);
  196. enum LSBPCameraMotionCompensation {
  197. LSBP_CAMERA_MOTION_COMPENSATION_NONE = 0,
  198. LSBP_CAMERA_MOTION_COMPENSATION_LK
  199. };
  200. /** @brief Implementation of the different yet better algorithm which is called GSOC, as it was implemented during GSOC and was not originated from any paper.
  201. This algorithm demonstrates better performance on CDNET 2014 dataset compared to other algorithms in OpenCV.
  202. */
  203. class CV_EXPORTS_W BackgroundSubtractorGSOC : public BackgroundSubtractor
  204. {
  205. public:
  206. // BackgroundSubtractor interface
  207. CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
  208. CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
  209. };
  210. /** @brief Background Subtraction using Local SVD Binary Pattern. More details about the algorithm can be found at @cite LGuo2016
  211. */
  212. class CV_EXPORTS_W BackgroundSubtractorLSBP : public BackgroundSubtractor
  213. {
  214. public:
  215. // BackgroundSubtractor interface
  216. CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
  217. CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
  218. };
  219. /** @brief This is for calculation of the LSBP descriptors.
  220. */
  221. class CV_EXPORTS_W BackgroundSubtractorLSBPDesc
  222. {
  223. public:
  224. static void calcLocalSVDValues(OutputArray localSVDValues, const Mat& frame);
  225. static void computeFromLocalSVDValues(OutputArray desc, const Mat& localSVDValues, const Point2i* LSBPSamplePoints);
  226. static void compute(OutputArray desc, const Mat& frame, const Point2i* LSBPSamplePoints);
  227. };
  228. /** @brief Creates an instance of BackgroundSubtractorGSOC algorithm.
  229. Implementation of the different yet better algorithm which is called GSOC, as it was implemented during GSOC and was not originated from any paper.
  230. @param mc Whether to use camera motion compensation.
  231. @param nSamples Number of samples to maintain at each point of the frame.
  232. @param replaceRate Probability of replacing the old sample - how fast the model will update itself.
  233. @param propagationRate Probability of propagating to neighbors.
  234. @param hitsThreshold How many positives the sample must get before it will be considered as a possible replacement.
  235. @param alpha Scale coefficient for threshold.
  236. @param beta Bias coefficient for threshold.
  237. @param blinkingSupressionDecay Blinking supression decay factor.
  238. @param blinkingSupressionMultiplier Blinking supression multiplier.
  239. @param noiseRemovalThresholdFacBG Strength of the noise removal for background points.
  240. @param noiseRemovalThresholdFacFG Strength of the noise removal for foreground points.
  241. */
  242. CV_EXPORTS_W Ptr<BackgroundSubtractorGSOC> createBackgroundSubtractorGSOC(int mc = LSBP_CAMERA_MOTION_COMPENSATION_NONE, int nSamples = 20, float replaceRate = 0.003f, float propagationRate = 0.01f, int hitsThreshold = 32, float alpha = 0.01f, float beta = 0.0022f, float blinkingSupressionDecay = 0.1f, float blinkingSupressionMultiplier = 0.1f, float noiseRemovalThresholdFacBG = 0.0004f, float noiseRemovalThresholdFacFG = 0.0008f);
  243. /** @brief Creates an instance of BackgroundSubtractorLSBP algorithm.
  244. Background Subtraction using Local SVD Binary Pattern. More details about the algorithm can be found at @cite LGuo2016
  245. @param mc Whether to use camera motion compensation.
  246. @param nSamples Number of samples to maintain at each point of the frame.
  247. @param LSBPRadius LSBP descriptor radius.
  248. @param Tlower Lower bound for T-values. See @cite LGuo2016 for details.
  249. @param Tupper Upper bound for T-values. See @cite LGuo2016 for details.
  250. @param Tinc Increase step for T-values. See @cite LGuo2016 for details.
  251. @param Tdec Decrease step for T-values. See @cite LGuo2016 for details.
  252. @param Rscale Scale coefficient for threshold values.
  253. @param Rincdec Increase/Decrease step for threshold values.
  254. @param noiseRemovalThresholdFacBG Strength of the noise removal for background points.
  255. @param noiseRemovalThresholdFacFG Strength of the noise removal for foreground points.
  256. @param LSBPthreshold Threshold for LSBP binary string.
  257. @param minCount Minimal number of matches for sample to be considered as foreground.
  258. */
  259. CV_EXPORTS_W Ptr<BackgroundSubtractorLSBP> createBackgroundSubtractorLSBP(int mc = LSBP_CAMERA_MOTION_COMPENSATION_NONE, int nSamples = 20, int LSBPRadius = 16, float Tlower = 2.0f, float Tupper = 32.0f, float Tinc = 1.0f, float Tdec = 0.05f, float Rscale = 10.0f, float Rincdec = 0.005f, float noiseRemovalThresholdFacBG = 0.0004f, float noiseRemovalThresholdFacFG = 0.0008f, int LSBPthreshold = 8, int minCount = 2);
  260. /** @brief Synthetic frame sequence generator for testing background subtraction algorithms.
  261. It will generate the moving object on top of the background.
  262. It will apply some distortion to the background to make the test more complex.
  263. */
  264. class CV_EXPORTS_W SyntheticSequenceGenerator : public Algorithm
  265. {
  266. private:
  267. const double amplitude;
  268. const double wavelength;
  269. const double wavespeed;
  270. const double objspeed;
  271. unsigned timeStep;
  272. Point2d pos;
  273. Point2d dir;
  274. Mat background;
  275. Mat object;
  276. RNG rng;
  277. public:
  278. /** @brief Creates an instance of SyntheticSequenceGenerator.
  279. @param background Background image for object.
  280. @param object Object image which will move slowly over the background.
  281. @param amplitude Amplitude of wave distortion applied to background.
  282. @param wavelength Length of waves in distortion applied to background.
  283. @param wavespeed How fast waves will move.
  284. @param objspeed How fast object will fly over background.
  285. */
  286. CV_WRAP SyntheticSequenceGenerator(InputArray background, InputArray object, double amplitude, double wavelength, double wavespeed, double objspeed);
  287. /** @brief Obtain the next frame in the sequence.
  288. @param frame Output frame.
  289. @param gtMask Output ground-truth (reference) segmentation mask object/background.
  290. */
  291. CV_WRAP void getNextFrame(OutputArray frame, OutputArray gtMask);
  292. };
  293. /** @brief Creates an instance of SyntheticSequenceGenerator.
  294. @param background Background image for object.
  295. @param object Object image which will move slowly over the background.
  296. @param amplitude Amplitude of wave distortion applied to background.
  297. @param wavelength Length of waves in distortion applied to background.
  298. @param wavespeed How fast waves will move.
  299. @param objspeed How fast object will fly over background.
  300. */
  301. CV_EXPORTS_W Ptr<SyntheticSequenceGenerator> createSyntheticSequenceGenerator(InputArray background, InputArray object, double amplitude = 2.0, double wavelength = 20.0, double wavespeed = 0.2, double objspeed = 6.0);
  302. //! @}
  303. }
  304. }
  305. #endif
  306. #endif