test_OF_accuracy.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. static string getDataDir() { return TS::ptr()->get_data_path(); }
  44. static string getRubberWhaleFrame1() { return getDataDir() + "optflow/RubberWhale1.png"; }
  45. static string getRubberWhaleFrame2() { return getDataDir() + "optflow/RubberWhale2.png"; }
  46. static string getRubberWhaleGroundTruth() { return getDataDir() + "optflow/RubberWhale.flo"; }
  47. static bool isFlowCorrect(float u) { return !cvIsNaN(u) && (fabs(u) < 1e9); }
  48. static float calcRMSE(Mat flow1, Mat flow2)
  49. {
  50. float sum = 0;
  51. int counter = 0;
  52. const int rows = flow1.rows;
  53. const int cols = flow1.cols;
  54. for (int y = 0; y < rows; ++y)
  55. {
  56. for (int x = 0; x < cols; ++x)
  57. {
  58. Vec2f flow1_at_point = flow1.at<Vec2f>(y, x);
  59. Vec2f flow2_at_point = flow2.at<Vec2f>(y, x);
  60. float u1 = flow1_at_point[0];
  61. float v1 = flow1_at_point[1];
  62. float u2 = flow2_at_point[0];
  63. float v2 = flow2_at_point[1];
  64. if (isFlowCorrect(u1) && isFlowCorrect(u2) && isFlowCorrect(v1) && isFlowCorrect(v2))
  65. {
  66. sum += (u1 - u2) * (u1 - u2) + (v1 - v2) * (v1 - v2);
  67. counter++;
  68. }
  69. }
  70. }
  71. return (float)sqrt(sum / (1e-9 + counter));
  72. }
  73. bool readRubberWhale(Mat &dst_frame_1, Mat &dst_frame_2, Mat &dst_GT)
  74. {
  75. const string frame1_path = getRubberWhaleFrame1();
  76. const string frame2_path = getRubberWhaleFrame2();
  77. const string gt_flow_path = getRubberWhaleGroundTruth();
  78. dst_frame_1 = imread(frame1_path);
  79. dst_frame_2 = imread(frame2_path);
  80. dst_GT = readOpticalFlow(gt_flow_path);
  81. if (dst_frame_1.empty() || dst_frame_2.empty() || dst_GT.empty())
  82. return false;
  83. else
  84. return true;
  85. }
  86. TEST(DenseOpticalFlow_DIS, ReferenceAccuracy)
  87. {
  88. Mat frame1, frame2, GT;
  89. ASSERT_TRUE(readRubberWhale(frame1, frame2, GT));
  90. int presets[] = {DISOpticalFlow::PRESET_ULTRAFAST, DISOpticalFlow::PRESET_FAST, DISOpticalFlow::PRESET_MEDIUM};
  91. float target_RMSE[] = {0.86f, 0.74f, 0.49f};
  92. cvtColor(frame1, frame1, COLOR_BGR2GRAY);
  93. cvtColor(frame2, frame2, COLOR_BGR2GRAY);
  94. Ptr<DenseOpticalFlow> algo;
  95. // iterate over presets:
  96. for (int i = 0; i < 3; i++)
  97. {
  98. Mat flow;
  99. algo = DISOpticalFlow::create(presets[i]);
  100. algo->calc(frame1, frame2, flow);
  101. ASSERT_EQ(GT.rows, flow.rows);
  102. ASSERT_EQ(GT.cols, flow.cols);
  103. EXPECT_LE(calcRMSE(GT, flow), target_RMSE[i]);
  104. }
  105. }
  106. TEST(DenseOpticalFlow_DIS, InvalidImgSize_CoarsestLevelLessThanZero)
  107. {
  108. cv::Ptr<cv::DISOpticalFlow> of = cv::DISOpticalFlow::create();
  109. const int mat_size = 10;
  110. cv::Mat x(mat_size, mat_size, CV_8UC1, 42);
  111. cv::Mat y(mat_size, mat_size, CV_8UC1, 42);
  112. cv::Mat flow;
  113. ASSERT_THROW(of->calc(x, y, flow), cv::Exception);
  114. }
  115. // make sure that autoSelectPatchSizeAndScales() works properly.
  116. TEST(DenseOpticalFlow_DIS, InvalidImgSize_CoarsestLevelLessThanFinestLevel)
  117. {
  118. cv::Ptr<cv::DISOpticalFlow> of = cv::DISOpticalFlow::create();
  119. const int mat_size = 80;
  120. cv::Mat x(mat_size, mat_size, CV_8UC1, 42);
  121. cv::Mat y(mat_size, mat_size, CV_8UC1, 42);
  122. cv::Mat flow;
  123. of->calc(x, y, flow);
  124. ASSERT_EQ(flow.rows, mat_size);
  125. ASSERT_EQ(flow.cols, mat_size);
  126. }
  127. TEST(DenseOpticalFlow_VariationalRefinement, ReferenceAccuracy)
  128. {
  129. Mat frame1, frame2, GT;
  130. ASSERT_TRUE(readRubberWhale(frame1, frame2, GT));
  131. float target_RMSE = 0.86f;
  132. cvtColor(frame1, frame1, COLOR_BGR2GRAY);
  133. cvtColor(frame2, frame2, COLOR_BGR2GRAY);
  134. Ptr<VariationalRefinement> var_ref;
  135. var_ref = VariationalRefinement::create();
  136. var_ref->setAlpha(20.0f);
  137. var_ref->setDelta(5.0f);
  138. var_ref->setGamma(10.0f);
  139. var_ref->setSorIterations(25);
  140. var_ref->setFixedPointIterations(25);
  141. Mat flow(frame1.size(), CV_32FC2);
  142. flow.setTo(0.0f);
  143. var_ref->calc(frame1, frame2, flow);
  144. ASSERT_EQ(GT.rows, flow.rows);
  145. ASSERT_EQ(GT.cols, flow.cols);
  146. EXPECT_LE(calcRMSE(GT, flow), target_RMSE);
  147. }
  148. }} // namespace