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