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 43 #include "test_precomp.hpp" 44 #include "opencv2/calib3d/calib3d_c.h" 45 #include <string> 46 #include <limits> 47 48 using namespace cv; 49 using namespace std; 50 51 template<class T> double thres() { return 1.0; } 52 template<> double thres<float>() { return 1e-5; } 53 54 class CV_ReprojectImageTo3DTest : public cvtest::BaseTest 55 { 56 public: 57 CV_ReprojectImageTo3DTest() {} 58 ~CV_ReprojectImageTo3DTest() {} 59 protected: 60 61 62 void run(int) 63 { 64 ts->set_failed_test_info(cvtest::TS::OK); 65 int progress = 0; 66 int caseId = 0; 67 68 progress = update_progress( progress, 1, 14, 0 ); 69 runCase<float, float>(++caseId, -100.f, 100.f); 70 progress = update_progress( progress, 2, 14, 0 ); 71 runCase<int, float>(++caseId, -100, 100); 72 progress = update_progress( progress, 3, 14, 0 ); 73 runCase<short, float>(++caseId, -100, 100); 74 progress = update_progress( progress, 4, 14, 0 ); 75 runCase<unsigned char, float>(++caseId, 10, 100); 76 progress = update_progress( progress, 5, 14, 0 ); 77 78 runCase<float, int>(++caseId, -100.f, 100.f); 79 progress = update_progress( progress, 6, 14, 0 ); 80 runCase<int, int>(++caseId, -100, 100); 81 progress = update_progress( progress, 7, 14, 0 ); 82 runCase<short, int>(++caseId, -100, 100); 83 progress = update_progress( progress, 8, 14, 0 ); 84 runCase<unsigned char, int>(++caseId, 10, 100); 85 progress = update_progress( progress, 10, 14, 0 ); 86 87 runCase<float, short>(++caseId, -100.f, 100.f); 88 progress = update_progress( progress, 11, 14, 0 ); 89 runCase<int, short>(++caseId, -100, 100); 90 progress = update_progress( progress, 12, 14, 0 ); 91 runCase<short, short>(++caseId, -100, 100); 92 progress = update_progress( progress, 13, 14, 0 ); 93 runCase<unsigned char, short>(++caseId, 10, 100); 94 progress = update_progress( progress, 14, 14, 0 ); 95 } 96 97 template<class U, class V> double error(const Vec<U, 3>& v1, const Vec<V, 3>& v2) const 98 { 99 double tmp, sum = 0; 100 double nsum = 0; 101 for(int i = 0; i < 3; ++i) 102 { 103 tmp = v1[i]; 104 nsum += tmp * tmp; 105 106 tmp = tmp - v2[i]; 107 sum += tmp * tmp; 108 109 } 110 return sqrt(sum)/(sqrt(nsum)+1.); 111 } 112 113 template<class InT, class OutT> void runCase(int caseId, InT min, InT max) 114 { 115 typedef Vec<OutT, 3> out3d_t; 116 117 bool handleMissingValues = (unsigned)theRNG() % 2 == 0; 118 119 Mat_<InT> disp(Size(320, 240)); 120 randu(disp, Scalar(min), Scalar(max)); 121 122 if (handleMissingValues) 123 disp(disp.rows/2, disp.cols/2) = min - 1; 124 125 Mat_<double> Q(4, 4); 126 randu(Q, Scalar(-5), Scalar(5)); 127 128 Mat_<out3d_t> _3dImg(disp.size()); 129 130 CvMat cvdisp = disp; CvMat cv_3dImg = _3dImg; CvMat cvQ = Q; 131 cvReprojectImageTo3D( &cvdisp, &cv_3dImg, &cvQ, handleMissingValues ); 132 133 if (numeric_limits<OutT>::max() == numeric_limits<float>::max()) 134 reprojectImageTo3D(disp, _3dImg, Q, handleMissingValues); 135 136 for(int y = 0; y < disp.rows; ++y) 137 for(int x = 0; x < disp.cols; ++x) 138 { 139 InT d = disp(y, x); 140 141 double from[4] = { 142 static_cast<double>(x), 143 static_cast<double>(y), 144 static_cast<double>(d), 145 1.0, 146 }; 147 Mat_<double> res = Q * Mat_<double>(4, 1, from); 148 res /= res(3, 0); 149 150 out3d_t pixel_exp = *res.ptr<Vec3d>(); 151 out3d_t pixel_out = _3dImg(y, x); 152 153 const int largeZValue = 10000; /* see documentation */ 154 155 if (handleMissingValues && y == disp.rows/2 && x == disp.cols/2) 156 { 157 if (pixel_out[2] == largeZValue) 158 continue; 159 160 ts->printf(cvtest::TS::LOG, "Missing values are handled improperly\n"); 161 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY ); 162 return; 163 } 164 else 165 { 166 double err = error(pixel_out, pixel_exp), t = thres<OutT>(); 167 if ( err > t ) 168 { 169 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", 170 caseId, x, y, err, t, res(0,0), res(1,0), res(2,0), res(3,0), 171 (double)pixel_out[0], (double)pixel_out[1], (double)pixel_out[2]); 172 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY ); 173 return; 174 } 175 } 176 } 177 } 178 }; 179 180 TEST(Calib3d_ReprojectImageTo3D, accuracy) { CV_ReprojectImageTo3DTest test; test.safe_run(); } 181