Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/calib3d/test/test_chessboardgenerator.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 "test_chessboardgenerator.hpp"
45
46
namespace cv {
47
48
ChessBoardGenerator::ChessBoardGenerator(const Size& _patternSize) : sensorWidth(32), sensorHeight(24),
49
squareEdgePointsNum(200), min_cos(std::sqrt(3.f)*0.5f), cov(0.5),
50
patternSize(_patternSize), rendererResolutionMultiplier(4), tvec(Mat::zeros(1, 3, CV_32F))
51
{
52
rvec.create(3, 1, CV_32F);
53
cvtest::Rodrigues(Mat::eye(3, 3, CV_32F), rvec);
54
}
55
56
void ChessBoardGenerator::generateEdge(const Point3f& p1, const Point3f& p2, vector<Point3f>& out) const
57
{
58
Point3f step = (p2 - p1) * (1.f/squareEdgePointsNum);
59
for(size_t n = 0; n < squareEdgePointsNum; ++n)
60
out.push_back( p1 + step * (float)n);
61
}
62
63
Size ChessBoardGenerator::cornersSize() const
64
{
65
return Size(patternSize.width-1, patternSize.height-1);
66
}
67
68
struct Mult
69
{
70
float m;
71
Mult(int mult) : m((float)mult) {}
72
Point2f operator()(const Point2f& p)const { return p * m; }
73
};
74
75
void ChessBoardGenerator::generateBasis(Point3f& pb1, Point3f& pb2) const
76
{
77
RNG& rng = theRNG();
78
79
Vec3f n;
80
for(;;)
81
{
82
n[0] = rng.uniform(-1.f, 1.f);
83
n[1] = rng.uniform(-1.f, 1.f);
84
n[2] = rng.uniform(0.0f, 1.f);
85
float len = (float)norm(n);
86
if (len < 1e-3)
87
continue;
88
n[0]/=len;
89
n[1]/=len;
90
n[2]/=len;
91
92
if (n[2] > min_cos)
93
break;
94
}
95
96
Vec3f n_temp = n; n_temp[0] += 100;
97
Vec3f b1 = n.cross(n_temp);
98
Vec3f b2 = n.cross(b1);
99
float len_b1 = (float)norm(b1);
100
float len_b2 = (float)norm(b2);
101
102
pb1 = Point3f(b1[0]/len_b1, b1[1]/len_b1, b1[2]/len_b1);
103
pb2 = Point3f(b2[0]/len_b1, b2[1]/len_b2, b2[2]/len_b2);
104
}
105
106
107
Mat ChessBoardGenerator::generateChessBoard(const Mat& bg, const Mat& camMat, const Mat& distCoeffs,
108
const Point3f& zero, const Point3f& pb1, const Point3f& pb2,
109
float sqWidth, float sqHeight, const vector<Point3f>& whole,
110
vector<Point2f>& corners) const
111
{
112
vector< vector<Point> > squares_black;
113
for(int i = 0; i < patternSize.width; ++i)
114
for(int j = 0; j < patternSize.height; ++j)
115
if ( (i % 2 == 0 && j % 2 == 0) || (i % 2 != 0 && j % 2 != 0) )
116
{
117
vector<Point3f> pts_square3d;
118
vector<Point2f> pts_square2d;
119
120
Point3f p1 = zero + (i + 0) * sqWidth * pb1 + (j + 0) * sqHeight * pb2;
121
Point3f p2 = zero + (i + 1) * sqWidth * pb1 + (j + 0) * sqHeight * pb2;
122
Point3f p3 = zero + (i + 1) * sqWidth * pb1 + (j + 1) * sqHeight * pb2;
123
Point3f p4 = zero + (i + 0) * sqWidth * pb1 + (j + 1) * sqHeight * pb2;
124
generateEdge(p1, p2, pts_square3d);
125
generateEdge(p2, p3, pts_square3d);
126
generateEdge(p3, p4, pts_square3d);
127
generateEdge(p4, p1, pts_square3d);
128
129
projectPoints(Mat(pts_square3d), rvec, tvec, camMat, distCoeffs, pts_square2d);
130
squares_black.resize(squares_black.size() + 1);
131
vector<Point2f> temp;
132
approxPolyDP(Mat(pts_square2d), temp, 1.0, true);
133
transform(temp.begin(), temp.end(), back_inserter(squares_black.back()), Mult(rendererResolutionMultiplier));
134
}
135
136
/* calculate corners */
137
corners3d.clear();
138
for(int j = 0; j < patternSize.height - 1; ++j)
139
for(int i = 0; i < patternSize.width - 1; ++i)
140
corners3d.push_back(zero + (i + 1) * sqWidth * pb1 + (j + 1) * sqHeight * pb2);
141
corners.clear();
142
projectPoints(Mat(corners3d), rvec, tvec, camMat, distCoeffs, corners);
143
144
vector<Point3f> whole3d;
145
vector<Point2f> whole2d;
146
generateEdge(whole[0], whole[1], whole3d);
147
generateEdge(whole[1], whole[2], whole3d);
148
generateEdge(whole[2], whole[3], whole3d);
149
generateEdge(whole[3], whole[0], whole3d);
150
projectPoints(Mat(whole3d), rvec, tvec, camMat, distCoeffs, whole2d);
151
vector<Point2f> temp_whole2d;
152
approxPolyDP(Mat(whole2d), temp_whole2d, 1.0, true);
153
154
vector< vector<Point > > whole_contour(1);
155
transform(temp_whole2d.begin(), temp_whole2d.end(),
156
back_inserter(whole_contour.front()), Mult(rendererResolutionMultiplier));
157
158
Mat result;
159
if (rendererResolutionMultiplier == 1)
160
{
161
result = bg.clone();
162
drawContours(result, whole_contour, -1, Scalar::all(255), FILLED, LINE_AA);
163
drawContours(result, squares_black, -1, Scalar::all(0), FILLED, LINE_AA);
164
}
165
else
166
{
167
Mat tmp;
168
resize(bg, tmp, bg.size() * rendererResolutionMultiplier, 0, 0, INTER_LINEAR_EXACT);
169
drawContours(tmp, whole_contour, -1, Scalar::all(255), FILLED, LINE_AA);
170
drawContours(tmp, squares_black, -1, Scalar::all(0), FILLED, LINE_AA);
171
resize(tmp, result, bg.size(), 0, 0, INTER_AREA);
172
}
173
174
return result;
175
}
176
177
Mat ChessBoardGenerator::operator ()(const Mat& bg, const Mat& camMat, const Mat& distCoeffs, vector<Point2f>& corners) const
178
{
179
cov = std::min(cov, 0.8);
180
double fovx, fovy, focalLen;
181
Point2d principalPoint;
182
double aspect;
183
calibrationMatrixValues( camMat, bg.size(), sensorWidth, sensorHeight,
184
fovx, fovy, focalLen, principalPoint, aspect);
185
186
RNG& rng = theRNG();
187
188
float d1 = static_cast<float>(rng.uniform(0.1, 10.0));
189
float ah = static_cast<float>(rng.uniform(-fovx/2 * cov, fovx/2 * cov) * CV_PI / 180);
190
float av = static_cast<float>(rng.uniform(-fovy/2 * cov, fovy/2 * cov) * CV_PI / 180);
191
192
Point3f p;
193
p.z = cos(ah) * d1;
194
p.x = sin(ah) * d1;
195
p.y = p.z * tan(av);
196
197
Point3f pb1, pb2;
198
generateBasis(pb1, pb2);
199
200
float cbHalfWidth = static_cast<float>(norm(p) * sin( std::min(fovx, fovy) * 0.5 * CV_PI / 180));
201
float cbHalfHeight = cbHalfWidth * patternSize.height / patternSize.width;
202
203
float cbHalfWidthEx = cbHalfWidth * ( patternSize.width + 1) / patternSize.width;
204
float cbHalfHeightEx = cbHalfHeight * (patternSize.height + 1) / patternSize.height;
205
206
vector<Point3f> pts3d(4);
207
vector<Point2f> pts2d(4);
208
for(;;)
209
{
210
pts3d[0] = p + pb1 * cbHalfWidthEx + cbHalfHeightEx * pb2;
211
pts3d[1] = p + pb1 * cbHalfWidthEx - cbHalfHeightEx * pb2;
212
pts3d[2] = p - pb1 * cbHalfWidthEx - cbHalfHeightEx * pb2;
213
pts3d[3] = p - pb1 * cbHalfWidthEx + cbHalfHeightEx * pb2;
214
215
/* can remake with better perf */
216
projectPoints(Mat(pts3d), rvec, tvec, camMat, distCoeffs, pts2d);
217
218
bool inrect1 = pts2d[0].x < bg.cols && pts2d[0].y < bg.rows && pts2d[0].x > 0 && pts2d[0].y > 0;
219
bool inrect2 = pts2d[1].x < bg.cols && pts2d[1].y < bg.rows && pts2d[1].x > 0 && pts2d[1].y > 0;
220
bool inrect3 = pts2d[2].x < bg.cols && pts2d[2].y < bg.rows && pts2d[2].x > 0 && pts2d[2].y > 0;
221
bool inrect4 = pts2d[3].x < bg.cols && pts2d[3].y < bg.rows && pts2d[3].x > 0 && pts2d[3].y > 0;
222
223
if (inrect1 && inrect2 && inrect3 && inrect4)
224
break;
225
226
cbHalfWidth*=0.8f;
227
cbHalfHeight = cbHalfWidth * patternSize.height / patternSize.width;
228
229
cbHalfWidthEx = cbHalfWidth * ( patternSize.width + 1) / patternSize.width;
230
cbHalfHeightEx = cbHalfHeight * (patternSize.height + 1) / patternSize.height;
231
}
232
233
Point3f zero = p - pb1 * cbHalfWidth - cbHalfHeight * pb2;
234
float sqWidth = 2 * cbHalfWidth/patternSize.width;
235
float sqHeight = 2 * cbHalfHeight/patternSize.height;
236
237
return generateChessBoard(bg, camMat, distCoeffs, zero, pb1, pb2, sqWidth, sqHeight, pts3d, corners);
238
}
239
240
241
Mat ChessBoardGenerator::operator ()(const Mat& bg, const Mat& camMat, const Mat& distCoeffs,
242
const Size2f& squareSize, vector<Point2f>& corners) const
243
{
244
cov = std::min(cov, 0.8);
245
double fovx, fovy, focalLen;
246
Point2d principalPoint;
247
double aspect;
248
calibrationMatrixValues( camMat, bg.size(), sensorWidth, sensorHeight,
249
fovx, fovy, focalLen, principalPoint, aspect);
250
251
RNG& rng = theRNG();
252
253
float d1 = static_cast<float>(rng.uniform(0.1, 10.0));
254
float ah = static_cast<float>(rng.uniform(-fovx/2 * cov, fovx/2 * cov) * CV_PI / 180);
255
float av = static_cast<float>(rng.uniform(-fovy/2 * cov, fovy/2 * cov) * CV_PI / 180);
256
257
Point3f p;
258
p.z = cos(ah) * d1;
259
p.x = sin(ah) * d1;
260
p.y = p.z * tan(av);
261
262
Point3f pb1, pb2;
263
generateBasis(pb1, pb2);
264
265
float cbHalfWidth = squareSize.width * patternSize.width * 0.5f;
266
float cbHalfHeight = squareSize.height * patternSize.height * 0.5f;
267
268
float cbHalfWidthEx = cbHalfWidth * ( patternSize.width + 1) / patternSize.width;
269
float cbHalfHeightEx = cbHalfHeight * (patternSize.height + 1) / patternSize.height;
270
271
vector<Point3f> pts3d(4);
272
vector<Point2f> pts2d(4);
273
for(;;)
274
{
275
pts3d[0] = p + pb1 * cbHalfWidthEx + cbHalfHeightEx * pb2;
276
pts3d[1] = p + pb1 * cbHalfWidthEx - cbHalfHeightEx * pb2;
277
pts3d[2] = p - pb1 * cbHalfWidthEx - cbHalfHeightEx * pb2;
278
pts3d[3] = p - pb1 * cbHalfWidthEx + cbHalfHeightEx * pb2;
279
280
/* can remake with better perf */
281
projectPoints(Mat(pts3d), rvec, tvec, camMat, distCoeffs, pts2d);
282
283
bool inrect1 = pts2d[0].x < bg.cols && pts2d[0].y < bg.rows && pts2d[0].x > 0 && pts2d[0].y > 0;
284
bool inrect2 = pts2d[1].x < bg.cols && pts2d[1].y < bg.rows && pts2d[1].x > 0 && pts2d[1].y > 0;
285
bool inrect3 = pts2d[2].x < bg.cols && pts2d[2].y < bg.rows && pts2d[2].x > 0 && pts2d[2].y > 0;
286
bool inrect4 = pts2d[3].x < bg.cols && pts2d[3].y < bg.rows && pts2d[3].x > 0 && pts2d[3].y > 0;
287
288
if ( inrect1 && inrect2 && inrect3 && inrect4)
289
break;
290
291
p.z *= 1.1f;
292
}
293
294
Point3f zero = p - pb1 * cbHalfWidth - cbHalfHeight * pb2;
295
296
return generateChessBoard(bg, camMat, distCoeffs, zero, pb1, pb2,
297
squareSize.width, squareSize.height, pts3d, corners);
298
}
299
300
Mat ChessBoardGenerator::operator ()(const Mat& bg, const Mat& camMat, const Mat& distCoeffs,
301
const Size2f& squareSize, const Point3f& pos, vector<Point2f>& corners) const
302
{
303
cov = std::min(cov, 0.8);
304
Point3f p = pos;
305
Point3f pb1, pb2;
306
generateBasis(pb1, pb2);
307
308
float cbHalfWidth = squareSize.width * patternSize.width * 0.5f;
309
float cbHalfHeight = squareSize.height * patternSize.height * 0.5f;
310
311
float cbHalfWidthEx = cbHalfWidth * ( patternSize.width + 1) / patternSize.width;
312
float cbHalfHeightEx = cbHalfHeight * (patternSize.height + 1) / patternSize.height;
313
314
vector<Point3f> pts3d(4);
315
vector<Point2f> pts2d(4);
316
317
pts3d[0] = p + pb1 * cbHalfWidthEx + cbHalfHeightEx * pb2;
318
pts3d[1] = p + pb1 * cbHalfWidthEx - cbHalfHeightEx * pb2;
319
pts3d[2] = p - pb1 * cbHalfWidthEx - cbHalfHeightEx * pb2;
320
pts3d[3] = p - pb1 * cbHalfWidthEx + cbHalfHeightEx * pb2;
321
322
/* can remake with better perf */
323
projectPoints(Mat(pts3d), rvec, tvec, camMat, distCoeffs, pts2d);
324
325
Point3f zero = p - pb1 * cbHalfWidth - cbHalfHeight * pb2;
326
327
return generateChessBoard(bg, camMat, distCoeffs, zero, pb1, pb2,
328
squareSize.width, squareSize.height, pts3d, corners);
329
}
330
331
} // namespace
332
333