test_OF_accuracy.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 bool isFlowCorrect(double u) { return !cvIsNaN(u) && (fabs(u) < 1e9); }
  49. static float calcRMSE(Mat flow1, Mat flow2)
  50. {
  51. float sum = 0;
  52. int counter = 0;
  53. const int rows = flow1.rows;
  54. const int cols = flow1.cols;
  55. for (int y = 0; y < rows; ++y)
  56. {
  57. for (int x = 0; x < cols; ++x)
  58. {
  59. Vec2f flow1_at_point = flow1.at<Vec2f>(y, x);
  60. Vec2f flow2_at_point = flow2.at<Vec2f>(y, x);
  61. float u1 = flow1_at_point[0];
  62. float v1 = flow1_at_point[1];
  63. float u2 = flow2_at_point[0];
  64. float v2 = flow2_at_point[1];
  65. if (isFlowCorrect(u1) && isFlowCorrect(u2) && isFlowCorrect(v1) && isFlowCorrect(v2))
  66. {
  67. sum += (u1 - u2) * (u1 - u2) + (v1 - v2) * (v1 - v2);
  68. counter++;
  69. }
  70. }
  71. }
  72. return (float)sqrt(sum / (1e-9 + counter));
  73. }
  74. static float calcRMSE(vector<Point2f> prevPts, vector<Point2f> currPts, Mat flow)
  75. {
  76. vector<float> ee;
  77. for (unsigned int n = 0; n < prevPts.size(); n++)
  78. {
  79. Point2f gtFlow = flow.at<Point2f>(prevPts[n]);
  80. if (isFlowCorrect(gtFlow.x) && isFlowCorrect(gtFlow.y))
  81. {
  82. Point2f diffFlow = (currPts[n] - prevPts[n]) - gtFlow;
  83. ee.push_back(sqrt(diffFlow.x * diffFlow.x + diffFlow.y * diffFlow.y));
  84. }
  85. }
  86. return static_cast<float>(mean(ee).val[0]);
  87. }
  88. static float calcAvgEPE(vector< pair<Point2i, Point2i> > corr, Mat flow)
  89. {
  90. double sum = 0;
  91. int counter = 0;
  92. for (size_t i = 0; i < corr.size(); ++i)
  93. {
  94. Vec2f flow1_at_point = Point2f(corr[i].second - corr[i].first);
  95. Vec2f flow2_at_point = flow.at<Vec2f>(corr[i].first.y, corr[i].first.x);
  96. double u1 = (double)flow1_at_point[0];
  97. double v1 = (double)flow1_at_point[1];
  98. double u2 = (double)flow2_at_point[0];
  99. double v2 = (double)flow2_at_point[1];
  100. if (isFlowCorrect(u1) && isFlowCorrect(u2) && isFlowCorrect(v1) && isFlowCorrect(v2))
  101. {
  102. sum += sqrt((u1 - u2) * (u1 - u2) + (v1 - v2) * (v1 - v2));
  103. counter++;
  104. }
  105. }
  106. return (float)(sum / counter);
  107. }
  108. bool readRubberWhale(Mat &dst_frame_1, Mat &dst_frame_2, Mat &dst_GT)
  109. {
  110. string frame1_path = getRubberWhaleFrame1();
  111. string frame2_path = getRubberWhaleFrame2();
  112. string gt_flow_path = getRubberWhaleGroundTruth();
  113. // removing space may be an issue on windows machines
  114. frame1_path.erase(std::remove_if(frame1_path.begin(), frame1_path.end(), isspace), frame1_path.end());
  115. frame2_path.erase(std::remove_if(frame2_path.begin(), frame2_path.end(), isspace), frame2_path.end());
  116. gt_flow_path.erase(std::remove_if(gt_flow_path.begin(), gt_flow_path.end(), isspace), gt_flow_path.end());
  117. dst_frame_1 = imread(frame1_path);
  118. dst_frame_2 = imread(frame2_path);
  119. dst_GT = readOpticalFlow(gt_flow_path);
  120. if (dst_frame_1.empty() || dst_frame_2.empty() || dst_GT.empty())
  121. return false;
  122. else
  123. return true;
  124. }
  125. TEST(DenseOpticalFlow_SimpleFlow, ReferenceAccuracy)
  126. {
  127. Mat frame1, frame2, GT;
  128. ASSERT_TRUE(readRubberWhale(frame1, frame2, GT));
  129. float target_RMSE = 0.37f;
  130. Mat flow;
  131. Ptr<DenseOpticalFlow> algo;
  132. algo = createOptFlow_SimpleFlow();
  133. algo->calc(frame1, frame2, flow);
  134. ASSERT_EQ(GT.rows, flow.rows);
  135. ASSERT_EQ(GT.cols, flow.cols);
  136. EXPECT_LE(calcRMSE(GT, flow), target_RMSE);
  137. }
  138. TEST(DenseOpticalFlow_DeepFlow, ReferenceAccuracy)
  139. {
  140. Mat frame1, frame2, GT;
  141. ASSERT_TRUE(readRubberWhale(frame1, frame2, GT));
  142. float target_RMSE = 0.35f;
  143. cvtColor(frame1, frame1, COLOR_BGR2GRAY);
  144. cvtColor(frame2, frame2, COLOR_BGR2GRAY);
  145. Mat flow;
  146. Ptr<DenseOpticalFlow> algo;
  147. algo = createOptFlow_DeepFlow();
  148. algo->calc(frame1, frame2, flow);
  149. ASSERT_EQ(GT.rows, flow.rows);
  150. ASSERT_EQ(GT.cols, flow.cols);
  151. EXPECT_LE(calcRMSE(GT, flow), target_RMSE);
  152. }
  153. TEST(SparseOpticalFlow, ReferenceAccuracy)
  154. {
  155. // with the following test each invoker class should be tested once
  156. Mat frame1, frame2, GT;
  157. ASSERT_TRUE(readRubberWhale(frame1, frame2, GT));
  158. vector<Point2f> prevPts, currPts;
  159. for (int r = 0; r < frame1.rows; r+=10)
  160. {
  161. for (int c = 0; c < frame1.cols; c+=10)
  162. {
  163. prevPts.push_back(Point2f(static_cast<float>(c), static_cast<float>(r)));
  164. }
  165. }
  166. vector<uchar> status(prevPts.size());
  167. vector<float> err(prevPts.size());
  168. Ptr<SparseRLOFOpticalFlow> algo = SparseRLOFOpticalFlow::create();
  169. algo->setForwardBackward(0.0f);
  170. Ptr<RLOFOpticalFlowParameter> param = Ptr<RLOFOpticalFlowParameter>(new RLOFOpticalFlowParameter);
  171. param->supportRegionType = SR_CROSS;
  172. param->useIlluminationModel = true;
  173. param->solverType = ST_BILINEAR;
  174. param->setUseMEstimator(true);
  175. algo->setRLOFOpticalFlowParameter(param);
  176. algo->calc(frame1, frame2, prevPts, currPts, status, err);
  177. EXPECT_LE(calcRMSE(prevPts, currPts, GT), 0.3f);
  178. param->solverType = ST_STANDART;
  179. algo->setRLOFOpticalFlowParameter(param);
  180. algo->calc(frame1, frame2, prevPts, currPts, status, err);
  181. EXPECT_LE(calcRMSE(prevPts, currPts, GT), 0.34f);
  182. param->useIlluminationModel = false;
  183. param->solverType = ST_BILINEAR;
  184. algo->setRLOFOpticalFlowParameter(param);
  185. algo->calc(frame1, frame2, prevPts, currPts, status, err);
  186. EXPECT_LE(calcRMSE(prevPts, currPts, GT), 0.27f);
  187. param->solverType = ST_STANDART;
  188. algo->setRLOFOpticalFlowParameter(param);
  189. algo->calc(frame1, frame2, prevPts, currPts, status, err);
  190. EXPECT_LE(calcRMSE(prevPts, currPts, GT), 0.27f);
  191. param->setUseMEstimator(false);
  192. param->useIlluminationModel = true;
  193. param->solverType = ST_BILINEAR;
  194. algo->setRLOFOpticalFlowParameter(param);
  195. algo->calc(frame1, frame2, prevPts, currPts, status, err);
  196. EXPECT_LE(calcRMSE(prevPts, currPts, GT), 0.28f);
  197. param->solverType = ST_STANDART;
  198. algo->setRLOFOpticalFlowParameter(param);
  199. algo->calc(frame1, frame2, prevPts, currPts, status, err);
  200. EXPECT_LE(calcRMSE(prevPts, currPts, GT), 0.28f);
  201. param->useIlluminationModel = false;
  202. param->solverType = ST_BILINEAR;
  203. algo->setRLOFOpticalFlowParameter(param);
  204. algo->calc(frame1, frame2, prevPts, currPts, status, err);
  205. EXPECT_LE(calcRMSE(prevPts, currPts, GT), 0.80f);
  206. param->solverType = ST_STANDART;
  207. algo->setRLOFOpticalFlowParameter(param);
  208. algo->calc(frame1, frame2, prevPts, currPts, status, err);
  209. EXPECT_LE(calcRMSE(prevPts, currPts, GT), 0.28f);
  210. }
  211. TEST(DenseOpticalFlow_RLOF, ReferenceAccuracy)
  212. {
  213. Mat frame1, frame2, GT;
  214. ASSERT_TRUE(readRubberWhale(frame1, frame2, GT));
  215. Mat flow;
  216. Ptr<DenseRLOFOpticalFlow> algo = DenseRLOFOpticalFlow::create();
  217. Ptr<RLOFOpticalFlowParameter> param = Ptr<RLOFOpticalFlowParameter>(new RLOFOpticalFlowParameter);
  218. param->setUseMEstimator(true);
  219. param->supportRegionType = SR_CROSS;
  220. param->solverType = ST_BILINEAR;
  221. algo->setRLOFOpticalFlowParameter(param);
  222. algo->setForwardBackward(1.0f);
  223. algo->setGridStep(cv::Size(4, 4));
  224. algo->setInterpolation(INTERP_EPIC);
  225. algo->calc(frame1, frame2, flow);
  226. ASSERT_EQ(GT.rows, flow.rows);
  227. ASSERT_EQ(GT.cols, flow.cols);
  228. EXPECT_LE(calcRMSE(GT, flow), 0.46f);
  229. algo->setInterpolation(INTERP_GEO);
  230. algo->calc(frame1, frame2, flow);
  231. ASSERT_EQ(GT.rows, flow.rows);
  232. ASSERT_EQ(GT.cols, flow.cols);
  233. EXPECT_LE(calcRMSE(GT, flow), 0.55f);
  234. }
  235. TEST(DenseOpticalFlow_SparseToDenseFlow, ReferenceAccuracy)
  236. {
  237. Mat frame1, frame2, GT;
  238. ASSERT_TRUE(readRubberWhale(frame1, frame2, GT));
  239. float target_RMSE = 0.52f;
  240. Mat flow;
  241. Ptr<DenseOpticalFlow> algo;
  242. algo = createOptFlow_SparseToDense();
  243. algo->calc(frame1, frame2, flow);
  244. ASSERT_EQ(GT.rows, flow.rows);
  245. ASSERT_EQ(GT.cols, flow.cols);
  246. EXPECT_LE(calcRMSE(GT, flow), target_RMSE);
  247. }
  248. TEST(DenseOpticalFlow_PCAFlow, ReferenceAccuracy)
  249. {
  250. Mat frame1, frame2, GT;
  251. ASSERT_TRUE(readRubberWhale(frame1, frame2, GT));
  252. const float target_RMSE = 0.55f;
  253. Mat flow;
  254. Ptr<DenseOpticalFlow> algo = createOptFlow_PCAFlow();
  255. algo->calc(frame1, frame2, flow);
  256. ASSERT_EQ(GT.rows, flow.rows);
  257. ASSERT_EQ(GT.cols, flow.cols);
  258. EXPECT_LE(calcRMSE(GT, flow), target_RMSE);
  259. }
  260. TEST(DenseOpticalFlow_GlobalPatchColliderDCT, ReferenceAccuracy)
  261. {
  262. Mat frame1, frame2, GT;
  263. ASSERT_TRUE(readRubberWhale(frame1, frame2, GT));
  264. const Size sz = frame1.size() / 2;
  265. frame1 = frame1(Rect(0, 0, sz.width, sz.height));
  266. frame2 = frame2(Rect(0, 0, sz.width, sz.height));
  267. GT = GT(Rect(0, 0, sz.width, sz.height));
  268. vector<Mat> img1, img2, gt;
  269. vector< pair<Point2i, Point2i> > corr;
  270. img1.push_back(frame1);
  271. img2.push_back(frame2);
  272. gt.push_back(GT);
  273. Ptr< GPCForest<5> > forest = GPCForest<5>::create();
  274. forest->train(img1, img2, gt, GPCTrainingParams(8, 3, GPC_DESCRIPTOR_DCT, false));
  275. forest->findCorrespondences(frame1, frame2, corr);
  276. ASSERT_LE(7500U, corr.size());
  277. ASSERT_LE(calcAvgEPE(corr, GT), 0.5f);
  278. }
  279. TEST(DenseOpticalFlow_GlobalPatchColliderWHT, ReferenceAccuracy)
  280. {
  281. Mat frame1, frame2, GT;
  282. ASSERT_TRUE(readRubberWhale(frame1, frame2, GT));
  283. const Size sz = frame1.size() / 2;
  284. frame1 = frame1(Rect(0, 0, sz.width, sz.height));
  285. frame2 = frame2(Rect(0, 0, sz.width, sz.height));
  286. GT = GT(Rect(0, 0, sz.width, sz.height));
  287. vector<Mat> img1, img2, gt;
  288. vector< pair<Point2i, Point2i> > corr;
  289. img1.push_back(frame1);
  290. img2.push_back(frame2);
  291. gt.push_back(GT);
  292. Ptr< GPCForest<5> > forest = GPCForest<5>::create();
  293. forest->train(img1, img2, gt, GPCTrainingParams(8, 3, GPC_DESCRIPTOR_WHT, false));
  294. forest->findCorrespondences(frame1, frame2, corr);
  295. ASSERT_LE(7000U, corr.size());
  296. ASSERT_LE(calcAvgEPE(corr, GT), 0.5f);
  297. }
  298. }} // namespace