test_backgroundsubtractor_gbh.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Author: andrewgodbehere
  6. #include "test_precomp.hpp"
  7. namespace opencv_test { namespace {
  8. class CV_BackgroundSubtractorTest : public cvtest::BaseTest
  9. {
  10. public:
  11. CV_BackgroundSubtractorTest();
  12. protected:
  13. void run(int);
  14. };
  15. CV_BackgroundSubtractorTest::CV_BackgroundSubtractorTest()
  16. {
  17. }
  18. /**
  19. * This test checks the following:
  20. * (i) BackgroundSubtractorGMG can operate with matrices of various types and sizes
  21. * (ii) Training mode returns empty fgmask
  22. * (iii) End of training mode, and anomalous frame yields every pixel detected as FG
  23. */
  24. void CV_BackgroundSubtractorTest::run(int)
  25. {
  26. int code = cvtest::TS::OK;
  27. RNG& rng = ts->get_rng();
  28. int type = ((unsigned int)rng)%7; //!< pick a random type, 0 - 6, defined in types_c.h
  29. int channels = 1 + ((unsigned int)rng)%4; //!< random number of channels from 1 to 4.
  30. int channelsAndType = CV_MAKETYPE(type,channels);
  31. int width = 2 + ((unsigned int)rng)%98; //!< Mat will be 2 to 100 in width and height
  32. int height = 2 + ((unsigned int)rng)%98;
  33. Ptr<BackgroundSubtractorGMG> fgbg = createBackgroundSubtractorGMG();
  34. Mat fgmask;
  35. if (!fgbg)
  36. CV_Error(Error::StsError,"Failed to create Algorithm\n");
  37. /**
  38. * Set a few parameters
  39. */
  40. fgbg->setSmoothingRadius(7);
  41. fgbg->setDecisionThreshold(0.7);
  42. fgbg->setNumFrames(120);
  43. /**
  44. * Generate bounds for the values in the matrix for each type
  45. */
  46. double maxd = 0, mind = 0;
  47. /**
  48. * Max value for simulated images picked randomly in upper half of type range
  49. * Min value for simulated images picked randomly in lower half of type range
  50. */
  51. if (type == CV_8U)
  52. {
  53. uchar half = UCHAR_MAX/2;
  54. maxd = (unsigned char)rng.uniform(half+32, UCHAR_MAX);
  55. mind = (unsigned char)rng.uniform(0, half-32);
  56. }
  57. else if (type == CV_8S)
  58. {
  59. maxd = (char)rng.uniform(32, CHAR_MAX);
  60. mind = (char)rng.uniform(CHAR_MIN, -32);
  61. }
  62. else if (type == CV_16U)
  63. {
  64. ushort half = USHRT_MAX/2;
  65. maxd = (unsigned int)rng.uniform(half+32, USHRT_MAX);
  66. mind = (unsigned int)rng.uniform(0, half-32);
  67. }
  68. else if (type == CV_16S)
  69. {
  70. maxd = rng.uniform(32, SHRT_MAX);
  71. mind = rng.uniform(SHRT_MIN, -32);
  72. }
  73. else if (type == CV_32S)
  74. {
  75. maxd = rng.uniform(32, INT_MAX);
  76. mind = rng.uniform(INT_MIN, -32);
  77. }
  78. else if (type == CV_32F)
  79. {
  80. maxd = rng.uniform(32.0f, FLT_MAX);
  81. mind = rng.uniform(-FLT_MAX, -32.0f);
  82. }
  83. else if (type == CV_64F)
  84. {
  85. maxd = rng.uniform(32.0, DBL_MAX);
  86. mind = rng.uniform(-DBL_MAX, -32.0);
  87. }
  88. fgbg->setMinVal(mind);
  89. fgbg->setMaxVal(maxd);
  90. Mat simImage = Mat::zeros(height, width, channelsAndType);
  91. int numLearningFrames = 120;
  92. for (int i = 0; i < numLearningFrames; ++i)
  93. {
  94. /**
  95. * Genrate simulated "image" for any type. Values always confined to upper half of range.
  96. */
  97. rng.fill(simImage, RNG::UNIFORM, (mind + maxd)*0.5, maxd);
  98. /**
  99. * Feed simulated images into background subtractor
  100. */
  101. fgbg->apply(simImage,fgmask);
  102. Mat fullbg = Mat::zeros(simImage.rows, simImage.cols, CV_8U);
  103. //! fgmask should be entirely background during training
  104. code = cvtest::cmpEps2( ts, fgmask, fullbg, 0, false, "The training foreground mask" );
  105. if (code < 0)
  106. ts->set_failed_test_info( code );
  107. }
  108. //! generate last image, distinct from training images
  109. rng.fill(simImage, RNG::UNIFORM, mind, maxd);
  110. fgbg->apply(simImage,fgmask);
  111. //! now fgmask should be entirely foreground
  112. Mat fullfg = 255*Mat::ones(simImage.rows, simImage.cols, CV_8U);
  113. code = cvtest::cmpEps2( ts, fgmask, fullfg, 255, false, "The final foreground mask" );
  114. if (code < 0)
  115. {
  116. ts->set_failed_test_info( code );
  117. }
  118. }
  119. TEST(VIDEO_BGSUBGMG, accuracy) { CV_BackgroundSubtractorTest test; test.safe_run(); }
  120. }} // namespace