1 /*M///////////////////////////////////////////////////////////////////////////////////////
\r
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
\r
5 // By downloading, copying, installing or using the software you agree to this license.
\r
6 // If you do not agree to this license, do not download, install,
\r
7 // copy or use the software.
\r
10 // License Agreement
\r
11 // For Open Source Computer Vision Library
\r
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
\r
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
\r
15 // Third party copyrights are property of their respective owners.
\r
17 // Redistribution and use in source and binary forms, with or without modification,
\r
18 // are permitted provided that the following conditions are met:
\r
20 // * Redistribution's of source code must retain the above copyright notice,
\r
21 // this list of conditions and the following disclaimer.
\r
23 // * Redistribution's in binary form must reproduce the above copyright notice,
\r
24 // this list of conditions and the following disclaimer in the documentation
\r
25 // and/or other materials provided with the distribution.
\r
27 // * The name of the copyright holders may not be used to endorse or promote products
\r
28 // derived from this software without specific prior written permission.
\r
30 // This software is provided by the copyright holders and contributors "as is" and
\r
31 // any express or implied warranties, including, but not limited to, the implied
\r
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
\r
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
\r
34 // indirect, incidental, special, exemplary, or consequential damages
\r
35 // (including, but not limited to, procurement of substitute goods or services;
\r
36 // loss of use, data, or profits; or business interruption) however caused
\r
37 // and on any theory of liability, whether in contract, strict liability,
\r
38 // or tort (including negligence or otherwise) arising in any way out of
\r
39 // the use of this software, even if advised of the possibility of such damage.
\r
48 using namespace std;
\r
50 template<class T> double thres() { return 1.0; }
\r
51 template<> double thres<float>() { return 1e-5; }
\r
53 class CV_ReprojectImageTo3DTest : public CvTest
\r
56 CV_ReprojectImageTo3DTest()
\r
57 : CvTest( "reprojectImageTo3D", "cvReprojectImageTo3D")
\r
59 support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
\r
61 ~CV_ReprojectImageTo3DTest() {}
\r
67 ts->set_failed_test_info(CvTS::OK);
\r
71 progress = update_progress( progress, 1, 14, 0 );
\r
72 runCase<float, float>(++caseId, -100.f, 100.f);
\r
73 progress = update_progress( progress, 2, 14, 0 );
\r
74 runCase<int, float>(++caseId, -100, 100);
\r
75 progress = update_progress( progress, 3, 14, 0 );
\r
76 runCase<short, float>(++caseId, -100, 100);
\r
77 progress = update_progress( progress, 4, 14, 0 );
\r
78 runCase<unsigned char, float>(++caseId, 10, 100);
\r
79 progress = update_progress( progress, 5, 14, 0 );
\r
81 runCase<float, int>(++caseId, -100.f, 100.f);
\r
82 progress = update_progress( progress, 6, 14, 0 );
\r
83 runCase<int, int>(++caseId, -100, 100);
\r
84 progress = update_progress( progress, 7, 14, 0 );
\r
85 runCase<short, int>(++caseId, -100, 100);
\r
86 progress = update_progress( progress, 8, 14, 0 );
\r
87 runCase<unsigned char, int>(++caseId, 10, 100);
\r
88 progress = update_progress( progress, 10, 14, 0 );
\r
90 runCase<float, short>(++caseId, -100.f, 100.f);
\r
91 progress = update_progress( progress, 11, 14, 0 );
\r
92 runCase<int, short>(++caseId, -100, 100);
\r
93 progress = update_progress( progress, 12, 14, 0 );
\r
94 runCase<short, short>(++caseId, -100, 100);
\r
95 progress = update_progress( progress, 13, 14, 0 );
\r
96 runCase<unsigned char, short>(++caseId, 10, 100);
\r
97 progress = update_progress( progress, 14, 14, 0 );
\r
100 template<class U, class V> double error(const Vec<U, 3>& v1, const Vec<V, 3>& v2) const
\r
102 double tmp, sum = 0;
\r
104 for(size_t i = 0; i < 3; ++i)
\r
106 tmp = static_cast<double>(v1[i]);
\r
107 nsum += tmp * tmp;
\r
109 tmp = tmp - static_cast<double>(v2[i]);
\r
113 return sqrt(sum)/(sqrt(nsum)+1.);
\r
116 template<class InT, class OutT> void runCase(int caseId, InT min, InT max)
\r
118 typedef Vec<OutT, 3> out3d_t;
\r
120 bool handleMissingValues = (int)theRNG() % 2 == 0;
\r
122 Mat_<InT> disp(Size(320, 240));
\r
123 randu(disp, Scalar(min), Scalar(max));
\r
125 if (handleMissingValues)
\r
126 disp(disp.rows/2, disp.cols/2) = min - 1;
\r
128 Mat_<double> Q(4, 4);
\r
129 randu(Q, Scalar(-5), Scalar(5));
\r
131 Mat_<out3d_t> _3dImg(disp.size());
\r
133 CvMat cvdisp = disp; CvMat cv_3dImg = _3dImg; CvMat cvQ = Q;
\r
134 cvReprojectImageTo3D( &cvdisp, &cv_3dImg, &cvQ, handleMissingValues );
\r
136 if (numeric_limits<OutT>::max() == numeric_limits<float>::max())
\r
137 reprojectImageTo3D(disp, _3dImg, Q, handleMissingValues);
\r
139 for(int y = 0; y < disp.rows; ++y)
\r
140 for(int x = 0; x < disp.cols; ++x)
\r
142 InT d = disp(y, x);
\r
144 double from[4] = { x, y, d, 1 };
\r
145 Mat_<double> res = Q * Mat_<double>(4, 1, from);
\r
148 out3d_t pixel_exp = *res.ptr<Vec3d>();
\r
149 out3d_t pixel_out = _3dImg(y, x);
\r
151 const int largeZValue = 10000; /* see documentation */
\r
153 if (handleMissingValues && y == disp.rows/2 && x == disp.cols/2)
\r
155 if (pixel_out[2] == largeZValue)
\r
158 ts->printf(CvTS::LOG, "Missing values are handled improperly\n");
\r
159 ts->set_failed_test_info( CvTS::FAIL_BAD_ACCURACY );
\r
164 double err = error(pixel_out, pixel_exp), t = thres<OutT>();
\r
167 ts->printf(CvTS::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",
\r
168 caseId, x, y, err, t, res(0,0), res(1,0), res(2,0), res(3,0),
\r
169 (double)pixel_out[0], (double)pixel_out[1], (double)pixel_out[2]);
\r
170 ts->set_failed_test_info( CvTS::FAIL_BAD_ACCURACY );
\r
178 CV_ReprojectImageTo3DTest reprojectImageTo3D_test;
\r