test_reproject_image_to_3d.cpp 6.6 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. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. namespace opencv_test { namespace {
  44. template<class T> double thres() { return 1.0; }
  45. template<> double thres<float>() { return 1e-5; }
  46. class CV_ReprojectImageTo3DTest : public cvtest::BaseTest
  47. {
  48. public:
  49. CV_ReprojectImageTo3DTest() {}
  50. ~CV_ReprojectImageTo3DTest() {}
  51. protected:
  52. void run(int)
  53. {
  54. ts->set_failed_test_info(cvtest::TS::OK);
  55. int progress = 0;
  56. int caseId = 0;
  57. progress = update_progress( progress, 1, 14, 0 );
  58. runCase<float, float>(++caseId, -100.f, 100.f);
  59. progress = update_progress( progress, 2, 14, 0 );
  60. runCase<int, float>(++caseId, -100, 100);
  61. progress = update_progress( progress, 3, 14, 0 );
  62. runCase<short, float>(++caseId, -100, 100);
  63. progress = update_progress( progress, 4, 14, 0 );
  64. runCase<unsigned char, float>(++caseId, 10, 100);
  65. progress = update_progress( progress, 5, 14, 0 );
  66. runCase<float, int>(++caseId, -100.f, 100.f);
  67. progress = update_progress( progress, 6, 14, 0 );
  68. runCase<int, int>(++caseId, -100, 100);
  69. progress = update_progress( progress, 7, 14, 0 );
  70. runCase<short, int>(++caseId, -100, 100);
  71. progress = update_progress( progress, 8, 14, 0 );
  72. runCase<unsigned char, int>(++caseId, 10, 100);
  73. progress = update_progress( progress, 10, 14, 0 );
  74. runCase<float, short>(++caseId, -100.f, 100.f);
  75. progress = update_progress( progress, 11, 14, 0 );
  76. runCase<int, short>(++caseId, -100, 100);
  77. progress = update_progress( progress, 12, 14, 0 );
  78. runCase<short, short>(++caseId, -100, 100);
  79. progress = update_progress( progress, 13, 14, 0 );
  80. runCase<unsigned char, short>(++caseId, 10, 100);
  81. progress = update_progress( progress, 14, 14, 0 );
  82. }
  83. template<class U, class V> double error(const Vec<U, 3>& v1, const Vec<V, 3>& v2) const
  84. {
  85. double tmp, sum = 0;
  86. double nsum = 0;
  87. for(int i = 0; i < 3; ++i)
  88. {
  89. tmp = v1[i];
  90. nsum += tmp * tmp;
  91. tmp = tmp - v2[i];
  92. sum += tmp * tmp;
  93. }
  94. return sqrt(sum)/(sqrt(nsum)+1.);
  95. }
  96. template<class InT, class OutT> void runCase(int caseId, InT min, InT max)
  97. {
  98. typedef Vec<OutT, 3> out3d_t;
  99. bool handleMissingValues = (unsigned)theRNG() % 2 == 0;
  100. Mat_<InT> disp(Size(320, 240));
  101. randu(disp, Scalar(min), Scalar(max));
  102. if (handleMissingValues)
  103. disp(disp.rows/2, disp.cols/2) = min - 1;
  104. Mat_<double> Q(4, 4);
  105. randu(Q, Scalar(-5), Scalar(5));
  106. Mat_<out3d_t> _3dImg(disp.size());
  107. reprojectImageTo3D(disp, _3dImg, Q, handleMissingValues);
  108. for(int y = 0; y < disp.rows; ++y)
  109. for(int x = 0; x < disp.cols; ++x)
  110. {
  111. InT d = disp(y, x);
  112. double from[4] = {
  113. static_cast<double>(x),
  114. static_cast<double>(y),
  115. static_cast<double>(d),
  116. 1.0,
  117. };
  118. Mat_<double> res = Q * Mat_<double>(4, 1, from);
  119. res /= res(3, 0);
  120. out3d_t pixel_exp = *res.ptr<Vec3d>();
  121. out3d_t pixel_out = _3dImg(y, x);
  122. const int largeZValue = 10000; /* see documentation */
  123. if (handleMissingValues && y == disp.rows/2 && x == disp.cols/2)
  124. {
  125. if (pixel_out[2] == largeZValue)
  126. continue;
  127. ts->printf(cvtest::TS::LOG, "Missing values are handled improperly\n");
  128. ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
  129. return;
  130. }
  131. else
  132. {
  133. double err = error(pixel_out, pixel_exp), t = thres<OutT>();
  134. if ( err > t )
  135. {
  136. ts->printf(cvtest::TS::LOG, "case %d. too big error at (%d, %d): %g vs expected %g: res = (%g, %g, %g, w=%g) vs pixel_out = (%g, %g, %g)\n",
  137. caseId, x, y, err, t, res(0,0), res(1,0), res(2,0), res(3,0),
  138. (double)pixel_out[0], (double)pixel_out[1], (double)pixel_out[2]);
  139. ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
  140. return;
  141. }
  142. }
  143. }
  144. }
  145. };
  146. TEST(Calib3d_ReprojectImageTo3D, accuracy) { CV_ReprojectImageTo3DTest test; test.safe_run(); }
  147. }} // namespace