test_tvl1optflow.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. //#define DUMP
  44. // first four bytes, should be the same in little endian
  45. const float FLO_TAG_FLOAT = 202021.25f; // check for this when READING the file
  46. #ifdef DUMP
  47. // binary file format for flow data specified here:
  48. // http://vision.middlebury.edu/flow/data/
  49. void writeOpticalFlowToFile(const Mat_<Point2f>& flow, const string& fileName)
  50. {
  51. const char FLO_TAG_STRING[] = "PIEH"; // use this when WRITING the file
  52. ofstream file(fileName.c_str(), ios_base::binary);
  53. file << FLO_TAG_STRING;
  54. file.write((const char*) &flow.cols, sizeof(int));
  55. file.write((const char*) &flow.rows, sizeof(int));
  56. for (int i = 0; i < flow.rows; ++i)
  57. {
  58. for (int j = 0; j < flow.cols; ++j)
  59. {
  60. const Point2f u = flow(i, j);
  61. file.write((const char*) &u.x, sizeof(float));
  62. file.write((const char*) &u.y, sizeof(float));
  63. }
  64. }
  65. }
  66. #endif
  67. // binary file format for flow data specified here:
  68. // http://vision.middlebury.edu/flow/data/
  69. void readOpticalFlowFromFile(Mat_<Point2f>& flow, const string& fileName)
  70. {
  71. std::ifstream file(fileName.c_str(), std::ios_base::binary);
  72. float tag;
  73. file.read((char*) &tag, sizeof(float));
  74. CV_Assert( tag == FLO_TAG_FLOAT );
  75. Size size;
  76. file.read((char*) &size.width, sizeof(int));
  77. file.read((char*) &size.height, sizeof(int));
  78. flow.create(size);
  79. for (int i = 0; i < flow.rows; ++i)
  80. {
  81. for (int j = 0; j < flow.cols; ++j)
  82. {
  83. Point2f u;
  84. file.read((char*) &u.x, sizeof(float));
  85. file.read((char*) &u.y, sizeof(float));
  86. flow(i, j) = u;
  87. }
  88. }
  89. file.close();
  90. }
  91. bool isFlowCorrect(Point2f u)
  92. {
  93. return !cvIsNaN(u.x) && !cvIsNaN(u.y) && (fabs(u.x) < 1e9) && (fabs(u.y) < 1e9);
  94. }
  95. void check(const Mat_<Point2f>& gold, const Mat_<Point2f>& flow, double threshold = 0.1, double expectedAccuracy = 0.95)
  96. {
  97. threshold = threshold*threshold;
  98. size_t gold_counter = 0;
  99. size_t valid_counter = 0;
  100. for (int i = 0; i < gold.rows; ++i)
  101. {
  102. for (int j = 0; j < gold.cols; ++j)
  103. {
  104. const Point2f u1 = gold(i, j);
  105. const Point2f u2 = flow(i, j);
  106. if (isFlowCorrect(u1))
  107. {
  108. gold_counter++;
  109. if (isFlowCorrect(u2))
  110. {
  111. const Point2f diff = u1 - u2;
  112. double err = diff.ddot(diff);
  113. if (err <= threshold)
  114. valid_counter++;
  115. }
  116. }
  117. }
  118. }
  119. EXPECT_GE(valid_counter, expectedAccuracy * gold_counter);
  120. }
  121. TEST(Contrib_calcOpticalFlowDual_TVL1, Regression)
  122. {
  123. const string frame1_path = TS::ptr()->get_data_path() + "optflow/RubberWhale1.png";
  124. const string frame2_path = TS::ptr()->get_data_path() + "optflow/RubberWhale2.png";
  125. const string gold_flow_path = TS::ptr()->get_data_path() + "optflow/tvl1_flow.flo";
  126. Mat frame1 = imread(frame1_path, IMREAD_GRAYSCALE);
  127. Mat frame2 = imread(frame2_path, IMREAD_GRAYSCALE);
  128. ASSERT_FALSE(frame1.empty());
  129. ASSERT_FALSE(frame2.empty());
  130. Mat_<Point2f> flow;
  131. Ptr<DualTVL1OpticalFlow> tvl1 = cv::optflow::DualTVL1OpticalFlow::create();
  132. tvl1->calc(frame1, frame2, flow);
  133. #ifdef DUMP
  134. writeOpticalFlowToFile(flow, gold_flow_path);
  135. #else
  136. Mat_<Point2f> gold;
  137. readOpticalFlowFromFile(gold, gold_flow_path);
  138. ASSERT_EQ(gold.rows, flow.rows);
  139. ASSERT_EQ(gold.cols, flow.cols);
  140. check(gold, flow);
  141. #endif
  142. }
  143. }} // namespace