perf_bgfg_knn.cpp 2.7 KB

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