test_OF_reproducibility.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // Intel License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of Intel Corporation may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. namespace opencv_test { namespace {
  43. typedef tuple<Size> OFParams;
  44. typedef TestWithParam<OFParams> DenseOpticalFlow_DIS;
  45. typedef TestWithParam<OFParams> DenseOpticalFlow_VariationalRefinement;
  46. TEST_P(DenseOpticalFlow_DIS, MultithreadReproducibility)
  47. {
  48. double MAX_DIF = 0.01;
  49. double MAX_MEAN_DIF = 0.001;
  50. int loopsCount = 2;
  51. RNG rng(0);
  52. OFParams params = GetParam();
  53. Size size = get<0>(params);
  54. int nThreads = cv::getNumThreads();
  55. if (nThreads == 1)
  56. throw SkipTestException("Single thread environment");
  57. for (int iter = 0; iter <= loopsCount; iter++)
  58. {
  59. Mat frame1(size, CV_8U);
  60. randu(frame1, 0, 255);
  61. Mat frame2(size, CV_8U);
  62. randu(frame2, 0, 255);
  63. Ptr<DISOpticalFlow> algo = DISOpticalFlow::create();
  64. int psz = rng.uniform(4, 16);
  65. int pstr = rng.uniform(1, psz - 1);
  66. int grad_iter = rng.uniform(1, 64);
  67. int var_iter = rng.uniform(0, 10);
  68. bool use_mean_normalization = !!rng.uniform(0, 2);
  69. bool use_spatial_propagation = !!rng.uniform(0, 2);
  70. algo->setFinestScale(0);
  71. algo->setPatchSize(psz);
  72. algo->setPatchStride(pstr);
  73. algo->setGradientDescentIterations(grad_iter);
  74. algo->setVariationalRefinementIterations(var_iter);
  75. algo->setUseMeanNormalization(use_mean_normalization);
  76. algo->setUseSpatialPropagation(use_spatial_propagation);
  77. cv::setNumThreads(nThreads);
  78. Mat resMultiThread;
  79. algo->calc(frame1, frame2, resMultiThread);
  80. cv::setNumThreads(1);
  81. Mat resSingleThread;
  82. algo->calc(frame1, frame2, resSingleThread);
  83. EXPECT_LE(cv::norm(resSingleThread, resMultiThread, NORM_INF), MAX_DIF);
  84. EXPECT_LE(cv::norm(resSingleThread, resMultiThread, NORM_L1), MAX_MEAN_DIF * frame1.total());
  85. // resulting flow should be within the frame bounds:
  86. double min_val, max_val;
  87. minMaxLoc(resMultiThread, &min_val, &max_val);
  88. EXPECT_LE(abs(min_val), sqrt( static_cast<double>(size.height * size.height + size.width * size.width)) );
  89. EXPECT_LE(abs(max_val), sqrt( static_cast<double>(size.height * size.height + size.width * size.width)) );
  90. }
  91. }
  92. INSTANTIATE_TEST_CASE_P(FullSet, DenseOpticalFlow_DIS, Values(szODD, szQVGA));
  93. TEST_P(DenseOpticalFlow_VariationalRefinement, MultithreadReproducibility)
  94. {
  95. double MAX_DIF = 0.01;
  96. double MAX_MEAN_DIF = 0.001;
  97. float input_flow_rad = 5.0;
  98. int loopsCount = 2;
  99. RNG rng(0);
  100. OFParams params = GetParam();
  101. Size size = get<0>(params);
  102. int nThreads = cv::getNumThreads();
  103. if (nThreads == 1)
  104. throw SkipTestException("Single thread environment");
  105. for (int iter = 0; iter <= loopsCount; iter++)
  106. {
  107. Mat frame1(size, CV_8U);
  108. randu(frame1, 0, 255);
  109. Mat frame2(size, CV_8U);
  110. randu(frame2, 0, 255);
  111. Mat flow(size, CV_32FC2);
  112. randu(flow, -input_flow_rad, input_flow_rad);
  113. Ptr<VariationalRefinement> var = VariationalRefinement::create();
  114. var->setAlpha(rng.uniform(1.0f, 100.0f));
  115. var->setGamma(rng.uniform(0.1f, 10.0f));
  116. var->setDelta(rng.uniform(0.1f, 10.0f));
  117. var->setSorIterations(rng.uniform(1, 20));
  118. var->setFixedPointIterations(rng.uniform(1, 20));
  119. var->setOmega(rng.uniform(1.01f, 1.99f));
  120. cv::setNumThreads(nThreads);
  121. Mat resMultiThread;
  122. flow.copyTo(resMultiThread);
  123. var->calc(frame1, frame2, resMultiThread);
  124. cv::setNumThreads(1);
  125. Mat resSingleThread;
  126. flow.copyTo(resSingleThread);
  127. var->calc(frame1, frame2, resSingleThread);
  128. EXPECT_LE(cv::norm(resSingleThread, resMultiThread, NORM_INF), MAX_DIF);
  129. EXPECT_LE(cv::norm(resSingleThread, resMultiThread, NORM_L1), MAX_MEAN_DIF * frame1.total());
  130. // resulting flow should be within the frame bounds:
  131. double min_val, max_val;
  132. minMaxLoc(resMultiThread, &min_val, &max_val);
  133. EXPECT_LE(abs(min_val), sqrt( static_cast<double>(size.height * size.height + size.width * size.width)) );
  134. EXPECT_LE(abs(max_val), sqrt( static_cast<double>(size.height * size.height + size.width * size.width)) );
  135. }
  136. }
  137. INSTANTIATE_TEST_CASE_P(FullSet, DenseOpticalFlow_VariationalRefinement, Values(szODD, szQVGA));
  138. }} // namespace