test_optflowpyrlk.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
  14. // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
  15. // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #include "../test_precomp.hpp"
  44. #include "opencv2/ts/ocl_test.hpp"
  45. #ifdef HAVE_OPENCL
  46. namespace opencv_test { namespace ocl {
  47. /////////////////////////////////////////////////////////////////////////////////////////////////
  48. // PyrLKOpticalFlow
  49. PARAM_TEST_CASE(PyrLKOpticalFlow, int, int)
  50. {
  51. Size winSize;
  52. int maxLevel;
  53. TermCriteria criteria;
  54. int flags;
  55. double minEigThreshold;
  56. virtual void SetUp()
  57. {
  58. winSize = Size(GET_PARAM(0), GET_PARAM(0));
  59. maxLevel = GET_PARAM(1);
  60. criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01);
  61. flags = 0;
  62. minEigThreshold = 1e-4f;
  63. }
  64. };
  65. OCL_TEST_P(PyrLKOpticalFlow, Mat)
  66. {
  67. static const int npoints = 1000;
  68. static const float eps = 0.03f;
  69. static const float erreps = 0.1f;
  70. cv::Mat frame0 = readImage("optflow/RubberWhale1.png", cv::IMREAD_GRAYSCALE);
  71. ASSERT_FALSE(frame0.empty());
  72. UMat umatFrame0; frame0.copyTo(umatFrame0);
  73. cv::Mat frame1 = readImage("optflow/RubberWhale2.png", cv::IMREAD_GRAYSCALE);
  74. ASSERT_FALSE(frame1.empty());
  75. UMat umatFrame1; frame1.copyTo(umatFrame1);
  76. // SKIP unstable tests
  77. #ifdef __linux__
  78. if (cvtest::skipUnstableTests && ocl::useOpenCL())
  79. {
  80. if (ocl::Device::getDefault().isIntel())
  81. throw cvtest::SkipTestException("Skip unstable test");
  82. }
  83. #endif
  84. std::vector<cv::Point2f> pts;
  85. cv::goodFeaturesToTrack(frame0, pts, npoints, 0.01, 0.0);
  86. std::vector<cv::Point2f> cpuNextPts;
  87. std::vector<unsigned char> cpuStatusCPU;
  88. std::vector<float> cpuErr;
  89. OCL_OFF(cv::calcOpticalFlowPyrLK(frame0, frame1, pts, cpuNextPts, cpuStatusCPU, cpuErr, winSize, maxLevel, criteria, flags, minEigThreshold));
  90. UMat umatNextPts, umatStatus, umatErr;
  91. OCL_ON(cv::calcOpticalFlowPyrLK(umatFrame0, umatFrame1, pts, umatNextPts, umatStatus, umatErr, winSize, maxLevel, criteria, flags, minEigThreshold));
  92. std::vector<cv::Point2f> nextPts; umatNextPts.reshape(2, 1).copyTo(nextPts);
  93. std::vector<unsigned char> status; umatStatus.reshape(1, 1).copyTo(status);
  94. std::vector<float> err; umatErr.reshape(1, 1).copyTo(err);
  95. ASSERT_EQ(cpuNextPts.size(), nextPts.size());
  96. ASSERT_EQ(cpuStatusCPU.size(), status.size());
  97. size_t mistmatch = 0;
  98. size_t errmatch = 0;
  99. for (size_t i = 0; i < nextPts.size(); ++i)
  100. {
  101. if (status[i] != cpuStatusCPU[i])
  102. {
  103. ++mistmatch;
  104. continue;
  105. }
  106. if (status[i])
  107. {
  108. cv::Point2i a = nextPts[i];
  109. cv::Point2i b = cpuNextPts[i];
  110. bool eq = std::abs(a.x - b.x) < 1 && std::abs(a.y - b.y) < 1;
  111. float errdiff = 0.0f;
  112. if (!eq || errdiff > 1e-1)
  113. {
  114. ++mistmatch;
  115. continue;
  116. }
  117. eq = std::abs(cpuErr[i] - err[i]) <= (0.01 * std::max(1.0f, cpuErr[i]));
  118. if(!eq)
  119. ++errmatch;
  120. }
  121. }
  122. double bad_ratio = static_cast<double>(mistmatch) / (nextPts.size());
  123. double err_ratio = static_cast<double>(errmatch) / (nextPts.size());
  124. ASSERT_LE(bad_ratio, eps);
  125. ASSERT_LE(err_ratio, erreps);
  126. }
  127. OCL_INSTANTIATE_TEST_CASE_P(Video, PyrLKOpticalFlow,
  128. Combine(
  129. Values(11, 15, 21, 25),
  130. Values(3, 5)
  131. )
  132. );
  133. }} // namespace
  134. #endif // HAVE_OPENCL