test_bgfg_mog2.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "../test_precomp.hpp"
  2. #include "opencv2/ts/ocl_test.hpp"
  3. #ifdef HAVE_OPENCL
  4. namespace opencv_test {
  5. namespace ocl {
  6. //////////////////////////Mog2_Update///////////////////////////////////
  7. namespace
  8. {
  9. IMPLEMENT_PARAM_CLASS(UseGray, bool)
  10. IMPLEMENT_PARAM_CLASS(DetectShadow, bool)
  11. IMPLEMENT_PARAM_CLASS(UseFloat, bool)
  12. }
  13. PARAM_TEST_CASE(Mog2_Update, UseGray, DetectShadow,UseFloat)
  14. {
  15. bool useGray;
  16. bool detectShadow;
  17. bool useFloat;
  18. virtual void SetUp()
  19. {
  20. useGray = GET_PARAM(0);
  21. detectShadow = GET_PARAM(1);
  22. useFloat = GET_PARAM(2);
  23. }
  24. };
  25. OCL_TEST_P(Mog2_Update, Accuracy)
  26. {
  27. string inputFile = string(TS::ptr()->get_data_path()) + "video/768x576.avi";
  28. VideoCapture cap(inputFile);
  29. if (!cap.isOpened())
  30. throw SkipTestException("Video file can not be opened");
  31. Ptr<BackgroundSubtractorMOG2> mog2_cpu = createBackgroundSubtractorMOG2();
  32. Ptr<BackgroundSubtractorMOG2> mog2_ocl = createBackgroundSubtractorMOG2();
  33. mog2_cpu->setDetectShadows(detectShadow);
  34. mog2_ocl->setDetectShadows(detectShadow);
  35. Mat frame, foreground;
  36. UMat u_foreground;
  37. for (int i = 0; i < 10; ++i)
  38. {
  39. cap >> frame;
  40. ASSERT_FALSE(frame.empty());
  41. if (useGray)
  42. {
  43. Mat temp;
  44. cvtColor(frame, temp, COLOR_BGR2GRAY);
  45. swap(temp, frame);
  46. }
  47. if(useFloat)
  48. {
  49. Mat temp;
  50. frame.convertTo(temp,CV_32F);
  51. swap(temp,frame);
  52. }
  53. OCL_OFF(mog2_cpu->apply(frame, foreground));
  54. OCL_ON (mog2_ocl->apply(frame, u_foreground));
  55. if (detectShadow)
  56. EXPECT_MAT_SIMILAR(foreground, u_foreground, 15e-3);
  57. else
  58. EXPECT_MAT_NEAR(foreground, u_foreground, 0);
  59. }
  60. }
  61. //////////////////////////Mog2_getBackgroundImage///////////////////////////////////
  62. PARAM_TEST_CASE(Mog2_getBackgroundImage, DetectShadow, UseFloat)
  63. {
  64. bool detectShadow;
  65. bool useFloat;
  66. virtual void SetUp()
  67. {
  68. detectShadow = GET_PARAM(0);
  69. useFloat = GET_PARAM(1);
  70. }
  71. };
  72. OCL_TEST_P(Mog2_getBackgroundImage, Accuracy)
  73. {
  74. string inputFile = string(TS::ptr()->get_data_path()) + "video/768x576.avi";
  75. VideoCapture cap(inputFile);
  76. if (!cap.isOpened())
  77. throw SkipTestException("Video file can not be opened");
  78. Ptr<BackgroundSubtractorMOG2> mog2_cpu = createBackgroundSubtractorMOG2();
  79. Ptr<BackgroundSubtractorMOG2> mog2_ocl = createBackgroundSubtractorMOG2();
  80. mog2_cpu->setDetectShadows(detectShadow);
  81. mog2_ocl->setDetectShadows(detectShadow);
  82. Mat frame, foreground;
  83. UMat u_foreground;
  84. for (int i = 0; i < 10; ++i)
  85. {
  86. cap >> frame;
  87. ASSERT_FALSE(frame.empty());
  88. if(useFloat)
  89. {
  90. Mat temp;
  91. frame.convertTo(temp,CV_32F);
  92. swap(temp,frame);
  93. }
  94. OCL_OFF(mog2_cpu->apply(frame, foreground));
  95. OCL_ON (mog2_ocl->apply(frame, u_foreground));
  96. }
  97. Mat background;
  98. OCL_OFF(mog2_cpu->getBackgroundImage(background));
  99. UMat u_background;
  100. OCL_ON (mog2_ocl->getBackgroundImage(u_background));
  101. EXPECT_MAT_NEAR(background, u_background, 1.0);
  102. }
  103. ///////////////////////////////////////////////////////////////////////////////////////////
  104. OCL_INSTANTIATE_TEST_CASE_P(OCL_Video, Mog2_Update, Combine(
  105. Values(UseGray(true),UseGray(false)),
  106. Values(DetectShadow(true), DetectShadow(false)),
  107. Values(UseFloat(false),UseFloat(true)))
  108. );
  109. OCL_INSTANTIATE_TEST_CASE_P(OCL_Video, Mog2_getBackgroundImage, Combine(
  110. Values(DetectShadow(true), DetectShadow(false)),
  111. Values(UseFloat(false),UseFloat(true)))
  112. );
  113. }}// namespace opencv_test::ocl
  114. #endif