test_bgfg2.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include "test_precomp.hpp"
  5. #include "opencv2/video/background_segm.hpp"
  6. namespace opencv_test { namespace {
  7. using namespace cv;
  8. ///////////////////////// MOG2 //////////////////////////////
  9. TEST(BackgroundSubtractorMOG2, KnownForegroundMaskShadowsTrue)
  10. {
  11. Ptr<BackgroundSubtractorMOG2> mog2 = createBackgroundSubtractorMOG2(500, 16, true);
  12. //Black Frame
  13. Mat input = Mat::zeros(480,640 , CV_8UC3);
  14. //White Rectangle
  15. Mat knownFG = Mat::zeros(input.size(), CV_8U);
  16. rectangle(knownFG, Rect(3,3,5,5), Scalar(255,255,255), -1);
  17. Mat output;
  18. mog2->apply(input, knownFG, output);
  19. for(int y = 3; y < 8; y++)
  20. {
  21. for (int x = 3; x < 8; x++){
  22. EXPECT_EQ(255,output.at<uchar>(y,x)) << "Expected foreground at (" << x << "," << y << ")";
  23. }
  24. }
  25. }
  26. TEST(BackgroundSubtractorMOG2, KnownForegroundMaskShadowsFalse)
  27. {
  28. Ptr<BackgroundSubtractorMOG2> mog2 = createBackgroundSubtractorMOG2(500, 16, false);
  29. //Black Frame
  30. Mat input = Mat::zeros(480,640 , CV_8UC3);
  31. //White Rectangle
  32. Mat knownFG = Mat::zeros(input.size(), CV_8U);
  33. rectangle(knownFG, Rect(3,3,5,5), Scalar(255,255,255), FILLED);
  34. Mat output;
  35. mog2->apply(input, knownFG, output);
  36. for(int y = 3; y < 8; y++)
  37. {
  38. for (int x = 3; x < 8; x++){
  39. EXPECT_EQ(255,output.at<uchar>(y,x)) << "Expected foreground at (" << x << "," << y << ")";
  40. }
  41. }
  42. }
  43. ///////////////////////// KNN //////////////////////////////
  44. TEST(BackgroundSubtractorKNN, KnownForegroundMaskShadowsTrue)
  45. {
  46. Ptr<BackgroundSubtractorKNN> knn = createBackgroundSubtractorKNN(500, 400.0, true);
  47. //Black Frame
  48. Mat input = Mat::zeros(480,640 , CV_8UC3);
  49. //White Rectangle
  50. Mat knownFG = Mat::zeros(input.size(), CV_8U);
  51. rectangle(knownFG, Rect(3,3,5,5), Scalar(255,255,255), FILLED);
  52. Mat output;
  53. knn->apply(input, knownFG, output);
  54. for(int y = 3; y < 8; y++)
  55. {
  56. for (int x = 3; x < 8; x++){
  57. EXPECT_EQ(255,output.at<uchar>(y,x)) << "Expected foreground at (" << x << "," << y << ")";
  58. }
  59. }
  60. }
  61. TEST(BackgroundSubtractorKNN, KnownForegroundMaskShadowsFalse)
  62. {
  63. Ptr<BackgroundSubtractorKNN> knn = createBackgroundSubtractorKNN(500, 400.0, false);
  64. //Black Frame
  65. Mat input = Mat::zeros(480,640 , CV_8UC3);
  66. //White Rectangle
  67. Mat knownFG = Mat::zeros(input.size(), CV_8U);
  68. rectangle(knownFG, Rect(3,3,5,5), Scalar(255,255,255), FILLED);
  69. Mat output;
  70. knn->apply(input, knownFG, output);
  71. for(int y = 3; y < 8; y++)
  72. {
  73. for (int x = 3; x < 8; x++){
  74. EXPECT_EQ(255,output.at<uchar>(y,x)) << "Expected foreground at (" << x << "," << y << ")";
  75. }
  76. }
  77. }
  78. }} // namespace
  79. /* End of file. */