perf_bgfg_knn.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "perf_precomp.hpp"
  5. #include "perf_bgfg_utils.hpp"
  6. namespace opencv_test { namespace {
  7. //////////////////////////// KNN//////////////////////////
  8. typedef tuple<std::string, int> VideoKNNParamType;
  9. typedef TestBaseWithParam<VideoKNNParamType> KNN_Apply;
  10. typedef TestBaseWithParam<VideoKNNParamType> KNN_GetBackgroundImage;
  11. PERF_TEST_P(KNN_Apply, KNN, Combine(Values("cv/video/768x576.avi", "cv/video/1920x1080.avi"), Values(1,3)))
  12. {
  13. VideoKNNParamType params = GetParam();
  14. const string inputFile = getDataPath(get<0>(params));
  15. const int cn = get<1>(params);
  16. int nFrame = 5;
  17. vector<Mat> frame_buffer(nFrame);
  18. cv::VideoCapture cap(inputFile);
  19. if (!cap.isOpened())
  20. throw SkipTestException("Video file can not be opened");
  21. prepareData(cap, cn, frame_buffer);
  22. Mat foreground;
  23. TEST_CYCLE()
  24. {
  25. Ptr<cv::BackgroundSubtractorKNN> knn = createBackgroundSubtractorKNN();
  26. knn->setDetectShadows(false);
  27. foreground.release();
  28. for (int i = 0; i < nFrame; i++)
  29. {
  30. knn->apply(frame_buffer[i], foreground);
  31. }
  32. }
  33. SANITY_CHECK_NOTHING();
  34. }
  35. PERF_TEST_P(KNN_GetBackgroundImage, KNN, Values(
  36. std::make_pair<string, int>("cv/video/768x576.avi", 5),
  37. std::make_pair<string, int>("cv/video/1920x1080.avi", 5)))
  38. {
  39. VideoKNNParamType params = GetParam();
  40. const string inputFile = getDataPath(get<0>(params));
  41. const int cn = 3;
  42. const int skipFrames = get<1>(params);
  43. int nFrame = 10;
  44. vector<Mat> frame_buffer(nFrame);
  45. cv::VideoCapture cap(inputFile);
  46. if (!cap.isOpened())
  47. throw SkipTestException("Video file can not be opened");
  48. prepareData(cap, cn, frame_buffer, skipFrames);
  49. Mat foreground, background;
  50. TEST_CYCLE()
  51. {
  52. Ptr<cv::BackgroundSubtractorKNN> knn = createBackgroundSubtractorKNN();
  53. knn->setDetectShadows(false);
  54. foreground.release();
  55. background.release();
  56. for (int i = 0; i < nFrame; i++)
  57. {
  58. knn->apply(frame_buffer[i], foreground);
  59. }
  60. knn->getBackgroundImage(background);
  61. }
  62. #ifdef DEBUG_BGFG
  63. imwrite(format("fg_%d_%d_knn.png", frame_buffer[0].rows, cn), foreground);
  64. imwrite(format("bg_%d_%d_knn.png", frame_buffer[0].rows, cn), background);
  65. #endif
  66. SANITY_CHECK_NOTHING();
  67. }
  68. }}// namespace