]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/tests/cv/src/areprojectImageTo3D.cpp
fixed a few warnings + false fail in reprojectImageTo3D
[opencv.git] / opencv / tests / cv / src / areprojectImageTo3D.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
4 //\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
8 //\r
9 //\r
10 //                           License Agreement\r
11 //                For Open Source Computer Vision Library\r
12 //\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
16 //\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
19 //\r
20 //   * Redistribution's of source code must retain the above copyright notice,\r
21 //     this list of conditions and the following disclaimer.\r
22 //\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
26 //\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
29 //\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
40 //\r
41 //M*/\r
42 \r
43 #include "cvtest.h"\r
44 #include <string>\r
45 #include <limits>\r
46 \r
47 using namespace cv;\r
48 using namespace std;\r
49 \r
50 template<class T> double thres() { return 1.0; }\r
51 template<> double thres<float>() { return 1e-5; }   \r
52 \r
53 class CV_ReprojectImageTo3DTest : public CvTest\r
54 {\r
55 public:\r
56     CV_ReprojectImageTo3DTest()\r
57         : CvTest( "reprojectImageTo3D", "cvReprojectImageTo3D")\r
58     {\r
59         support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;\r
60     }\r
61     ~CV_ReprojectImageTo3DTest() {}\r
62 protected: \r
63 \r
64     \r
65     void run(int)\r
66     {        \r
67         ts->set_failed_test_info(CvTS::OK);\r
68         int progress = 0;\r
69         int caseId = 0;\r
70 \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
80 \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
89 \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
98     }\r
99 \r
100     template<class U, class V> double error(const Vec<U, 3>& v1, const Vec<V, 3>& v2) const\r
101     {\r
102         double tmp, sum = 0;\r
103         double nsum = 0;\r
104         for(size_t i = 0; i < 3; ++i)\r
105         {\r
106             tmp = static_cast<double>(v1[i]);\r
107             nsum +=  tmp * tmp;            \r
108 \r
109             tmp = tmp - static_cast<double>(v2[i]);\r
110             sum += tmp * tmp;\r
111             \r
112         }        \r
113         return sqrt(sum)/(sqrt(nsum)+1.);\r
114     }\r
115 \r
116     template<class InT, class OutT> void runCase(int caseId, InT min, InT max)\r
117     {                     \r
118         typedef Vec<OutT, 3> out3d_t;\r
119 \r
120         bool handleMissingValues = (int)theRNG() % 2 == 0;                   \r
121 \r
122         Mat_<InT> disp(Size(320, 240));\r
123         randu(disp, Scalar(min), Scalar(max));\r
124 \r
125         if (handleMissingValues)\r
126             disp(disp.rows/2, disp.cols/2) = min - 1;\r
127         \r
128         Mat_<double> Q(4, 4);\r
129         randu(Q, Scalar(-5), Scalar(5));\r
130 \r
131         Mat_<out3d_t> _3dImg(disp.size());\r
132                        \r
133         CvMat cvdisp = disp; CvMat cv_3dImg = _3dImg; CvMat cvQ = Q;\r
134         cvReprojectImageTo3D( &cvdisp, &cv_3dImg, &cvQ, handleMissingValues );\r
135 \r
136         if (numeric_limits<OutT>::max() == numeric_limits<float>::max())\r
137             reprojectImageTo3D(disp, _3dImg, Q, handleMissingValues);\r
138                         \r
139         for(int y = 0; y < disp.rows; ++y)\r
140             for(int x = 0; x < disp.cols; ++x)\r
141             {\r
142                 InT d = disp(y, x);                \r
143 \r
144                 double from[4] = { x, y, d, 1 };\r
145                 Mat_<double> res = Q * Mat_<double>(4, 1, from);\r
146                 res /= res(3, 0);\r
147 \r
148                 out3d_t pixel_exp = *res.ptr<Vec3d>();\r
149                 out3d_t pixel_out = _3dImg(y, x);\r
150 \r
151                 const int largeZValue = 10000; /* see documentation */ \r
152 \r
153                 if (handleMissingValues && y == disp.rows/2 && x == disp.cols/2)                    \r
154                 {   \r
155                     if (pixel_out[2] == largeZValue)\r
156                         continue;\r
157 \r
158                     ts->printf(CvTS::LOG, "Missing values are handled improperly\n");\r
159                     ts->set_failed_test_info( CvTS::FAIL_BAD_ACCURACY );                       \r
160                     return;\r
161                 }\r
162                 else\r
163                 {\r
164                     double err = error(pixel_out, pixel_exp), t = thres<OutT>();\r
165                     if ( err > t )\r
166                     {\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
171                         return;\r
172                     }\r
173                 }\r
174             }    \r
175     }\r
176 };   \r
177     \r
178 CV_ReprojectImageTo3DTest reprojectImageTo3D_test;\r