test_bgsegm.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. #ifdef HAVE_CUDA
  44. namespace opencv_test { namespace {
  45. //////////////////////////////////////////////////////
  46. // MOG2
  47. #ifdef HAVE_VIDEO_INPUT
  48. namespace
  49. {
  50. IMPLEMENT_PARAM_CLASS(UseGray, bool)
  51. IMPLEMENT_PARAM_CLASS(DetectShadow, bool)
  52. }
  53. PARAM_TEST_CASE(MOG2, cv::cuda::DeviceInfo, std::string, UseGray, DetectShadow, UseRoi)
  54. {
  55. cv::cuda::DeviceInfo devInfo;
  56. std::string inputFile;
  57. bool useGray;
  58. bool detectShadow;
  59. bool useRoi;
  60. virtual void SetUp()
  61. {
  62. devInfo = GET_PARAM(0);
  63. cv::cuda::setDevice(devInfo.deviceID());
  64. inputFile = std::string(cvtest::TS::ptr()->get_data_path()) + "video/" + GET_PARAM(1);
  65. useGray = GET_PARAM(2);
  66. detectShadow = GET_PARAM(3);
  67. useRoi = GET_PARAM(4);
  68. }
  69. };
  70. CUDA_TEST_P(MOG2, Update)
  71. {
  72. cv::VideoCapture cap(inputFile);
  73. ASSERT_TRUE(cap.isOpened());
  74. cv::Mat frame;
  75. cap >> frame;
  76. ASSERT_FALSE(frame.empty());
  77. cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = cv::cuda::createBackgroundSubtractorMOG2();
  78. mog2->setDetectShadows(detectShadow);
  79. cv::cuda::GpuMat foreground = createMat(frame.size(), CV_8UC1, useRoi);
  80. cv::Ptr<cv::BackgroundSubtractorMOG2> mog2_gold = cv::createBackgroundSubtractorMOG2();
  81. mog2_gold->setDetectShadows(detectShadow);
  82. cv::Mat foreground_gold;
  83. for (int i = 0; i < 10; ++i)
  84. {
  85. cap >> frame;
  86. ASSERT_FALSE(frame.empty());
  87. if (useGray)
  88. {
  89. cv::Mat temp;
  90. cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);
  91. cv::swap(temp, frame);
  92. }
  93. mog2->apply(loadMat(frame, useRoi), foreground);
  94. mog2_gold->apply(frame, foreground_gold);
  95. ASSERT_MAT_SIMILAR(foreground_gold, foreground, detectShadow ? 13e-3 : 2e-4);
  96. }
  97. }
  98. CUDA_TEST_P(MOG2, getBackgroundImage)
  99. {
  100. if (useGray)
  101. return;
  102. cv::VideoCapture cap(inputFile);
  103. ASSERT_TRUE(cap.isOpened());
  104. cv::Mat frame;
  105. cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = cv::cuda::createBackgroundSubtractorMOG2();
  106. mog2->setDetectShadows(detectShadow);
  107. cv::cuda::GpuMat foreground;
  108. cv::Ptr<cv::BackgroundSubtractorMOG2> mog2_gold = cv::createBackgroundSubtractorMOG2();
  109. mog2_gold->setDetectShadows(detectShadow);
  110. cv::Mat foreground_gold;
  111. for (int i = 0; i < 10; ++i)
  112. {
  113. cap >> frame;
  114. ASSERT_FALSE(frame.empty());
  115. mog2->apply(loadMat(frame, useRoi), foreground);
  116. mog2_gold->apply(frame, foreground_gold);
  117. }
  118. cv::cuda::GpuMat background = createMat(frame.size(), frame.type(), useRoi);
  119. mog2->getBackgroundImage(background);
  120. cv::Mat background_gold;
  121. mog2_gold->getBackgroundImage(background_gold);
  122. ASSERT_MAT_NEAR(background_gold, background, 1);
  123. }
  124. INSTANTIATE_TEST_CASE_P(CUDA_BgSegm, MOG2, testing::Combine(
  125. ALL_DEVICES,
  126. testing::Values(std::string("768x576.avi")),
  127. testing::Values(UseGray(true), UseGray(false)),
  128. testing::Values(DetectShadow(true), DetectShadow(false)),
  129. WHOLE_SUBMAT));
  130. #endif
  131. }} // namespace
  132. #endif // HAVE_CUDA