-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.h
366 lines (294 loc) · 12.5 KB
/
Camera.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* This file is part of ALVAR, A Library for Virtual and Augmented Reality.
*
* Copyright 2007-2012 VTT Technical Research Centre of Finland
*
* Contact: VTT Augmented Reality Team <[email protected]>
* <http://www.vtt.fi/multimedia/alvar.html>
*
* ALVAR is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ALVAR; if not, see
* <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
*/
#ifndef CAMERA_H
#define CAMERA_H
/**
* \file Camera.h
*
* \brief This file implements a camera used for projecting points and
* computing homographies.
*/
/*****************************************************************************
****************************** I N C L U D E ******************************
****************************************************************************/
#include "Alvar.h"
#include "cv.h"
#include "Pose.h"
#include "Util.h"
#include <vector>
namespace alvar {
/*****************************************************************************
*
*** class ProjPoints
*
* Simple structure for collecting 2D and 3D points e.g. for camera calibration
*
*****************************************************************************/
class ALVAR_EXPORT ProjPoints
{
public:
int width;
int height;
ProjPoints() = default;
ProjPoints(const ProjPoints& src) = delete;
~ProjPoints() = default;
ProjPoints& operator=(const ProjPoints& rhs) = delete;
/** \brief 3D object points corresponding with the detected 2D image points. */
std::vector<cv::Point3d> object_points;
/** \brief Detected 2D object points
* If point_counts[0] == 10, then the
* first 10 points are detected in the first frame. If
* point_counts[1] == 6, then the next 6 of these points are
* detected in the next frame... etc.
*/
std::vector<cv::Point2d> image_points;
/** \brief Vector indicating how many points are detected for each frame */
std::vector<int> point_counts;
/** \brief Reset \e object_points , \e image_points and \e point_counts */
void Reset();
/** \brief Add elements to \e object_points , \e image_points and \e point_counts using Chessboard pattern */
bool AddPointsUsingChessboard(cv::Mat& image, double etalon_square_size, int etalon_rows, int etalon_columns,
bool visualize);
/** \brief Add elements to \e object_points , \e image_points and \e point_counts using detected markers */
bool AddPointsUsingMarkers(std::vector<PointDouble>& marker_corners, std::vector<PointDouble>& marker_corners_img,
cv::Mat& image);
}; // end of class ProjPoints
/*****************************************************************************
*
*** class Camera
*
* Simple \e Camera class for calculating distortions, orientation or
* projections with pre-calibrated camera
*
*****************************************************************************/
class ALVAR_EXPORT Camera
{
public:
enum class FILE_FORMAT
{
/**
* \brief Default file format.
*
* Format is either OPENCV, TEXT or XML depending on load/store function used.
*/
FILE_FORMAT_DEFAULT,
/**
* \brief File format written with cvWrite.
*
* File contents depend on the specific load/store function used.
*/
FILE_FORMAT_OPENCV,
/**
* \brief Plain ASCII text file format.
*
* File contents depend on the specific load/store function used.
*/
// FILE_FORMAT_TEXT,
/**
* \brief XML file format.
*
* XML schema depends on the specific load/store function used.
*/
// FILE_FORMAT_XML
};
/** \brief Constructor */
Camera();
Camera(const Camera& src) = delete;
~Camera() = default;
Camera& operator=(const Camera& rhs) = delete;
/** \brief One of the two methods to make this class serializable by \e Serialization class */
std::string SerializeId()
{
return ("camera");
};
/** \brief One of the two methods to make this class serializable by \e Serialization class
*
* You can serialize the \e Camera class using filename or any std::iostream
* as follows:
* \code
* alvar::Camera cam;
* cam.SetCalib("calib.xml", 320, 240);
* Serialization sero("calib1.xml");
* sero<<cam;
* \endcode
* \code
* alvar::Camera cam;
* Serialization seri("calib1.xml");
* seri>>cam;
* cam.SetRes(320, 240);
* \endcode
* \code
* std::stringstream ss;
* Serialization sero(ss);
* sero<<cam;
* std::cout<<ss.str()<<std::endl;
* // ...
* Serialization seri(ss);
* seri>>cam;
* \endcode
*/
/** \brief Get x-direction FOV in radians */
double GetFovX() const
{
return (2.0 * atan2((x_res / 2.0), calib_K.at<double>(0, 0)));
}
/** \brief Get y-direction FOV in radians */
double GetFovY() const
{
return (2.0 * atan2((y_res / 2.0), calib_K.at<double>(1, 1)));
}
void SetSimpleCalib(int _x_res, int _y_res, double f_fac = 1.0);
/** \brief Set the calibration file and the current resolution for which the calibration is adjusted to
* \param calibfile File to load.
* \param _x_res Width of images captured from the real camera.
* \param _y_res Height of images captured from the real camera.
* \param format FILE_FORMAT_OPENCV (default) or FILE_FORMAT_XML (see doc/Camera.xsd).
*/
bool SetCalib(const char* calibfile, int _x_res, int _y_res,
FILE_FORMAT format = FILE_FORMAT::FILE_FORMAT_DEFAULT);
/** \brief Save the current calibration information to a file
* \param calibfile File to save.
* \param format FILE_FORMAT_OPENCV (default) or FILE_FORMAT_XML (see doc/Camera.xsd).
*/
bool SaveCalib(const char* calibfile, FILE_FORMAT format = FILE_FORMAT::FILE_FORMAT_DEFAULT) const;
/** \brief Calibrate using the collected \e ProjPoints */
void Calibrate(ProjPoints &pp);
/** \brief If we have no calibration file we can still adjust the default calibration to current resolution */
void SetRes(int _x_res, int _y_res);
/** \brief Get OpenGL matrix
* Generates the OpenGL projection matrix based on OpenCV intrinsic camera matrix K.
* \code
* 2*K[0][0]/width 2*K[0][1]/width -(2*K[0][2]/width+1) 0
* 0 2*K[1][1]/height 2*K[1][2]/height-1 0
* 0 0 -(f+n)/(f-n) -2*f*n/(f-n)
* 0 0 -1 0
* \endcode
*
* Note, that the sign of the element [2][0] is changed. It should be
* \code
* 2*K[0][2]/width+1
* \endcode
*
* The sign change is due to the fact that with OpenCV and OpenGL projection
* matrices both y and z should be mirrored. With other matrix elements
* the sign changes eliminate each other, but with principal point
* in x-direction we need to make the change by hand.
*/
void GetOpenglProjectionMatrix(double proj_matrix[16], const int width, const int height,
const float far_clip = 1000.0f, const float near_clip = 0.1f) const;
/** \brief Invert operation for \e GetOpenglProjectionMatrix */
void SetOpenglProjectionMatrix(double proj_matrix[16], const int width, const int height);
/** \brief Unapplys the lens distortion for points on image plane. */
void Undistort(std::vector<PointDouble>& points) const;
/** \brief Unapplys the lens distortion for one point on an image plane. */
void Undistort(PointDouble& point) const;
/** \brief Unapplys the lens distortion for one point on an image plane. */
void Undistort(cv::Point2f& point) const;
/** \brief Applys the lens distortion for one point on an image plane. */
void Distort(cv::Point2f& point) const;
/** \brief Applys the lens distortion for points on image plane. */
void Distort(std::vector<PointDouble>& points) const;
/** \brief Applys the lens distortion for points on image plane. */
void Distort(PointDouble& point) const;
/** \brief Calculate exterior orientation */
bool CalcExteriorOrientation(std::vector<cv::Point3d>& pw, std::vector<cv::Point2d>& pi, Pose& pose) const;
/** \brief Calculate exterior orientation
*/
bool CalcExteriorOrientation(std::vector<cv::Point3d>& pw, std::vector<cv::Point2d>& pi,
cv::Mat& rodrigues, cv::Mat& tra) const
{
return (cv::solvePnP(pw, pi, calib_K, calib_D, rodrigues, tra));
}
/** \brief Calculate exterior orientation
*/
bool CalcExteriorOrientation(std::vector<PointDouble>& pw, std::vector<PointDouble>& pi,
cv::Mat& rodrigues, cv::Mat& tra) const;
/** \brief Calculate exterior orientation
*/
bool CalcExteriorOrientation(std::vector<PointDouble>& pw, std::vector<PointDouble>& pi, Pose& pose) const;
/** \brief Update existing pose based on new observations. Use (CV_32FC3 and CV_32FC2) for matrices. */
bool CalcExteriorOrientation(const cv::Mat& object_points, cv::Mat& image_points, Pose& pose) const;
/** \brief Update existing pose (in rodrigues&tra) based on new observations.
* Use (CV_32FC3 and CV_32FC2) for matrices. */
bool CalcExteriorOrientation(const cv::Mat& object_points, cv::Mat& image_points, cv::Mat& rodrigues,
cv::Mat& tra) const
{
return (cv::solvePnP(object_points, image_points, calib_K, calib_D, rodrigues, tra));
}
/** \brief Project one point */
void ProjectPoint(const cv::Point3d& pw, const Pose& pose, cv::Point2d& pi) const;
/** \brief Project one point */
// void ProjectPoint(const cv::Point3f& pw, const Pose& pose, cv::Point2f& pi) const;
/** \brief Project points */
void ProjectPoints(const std::vector<cv::Point3d>& pw, const Pose& pose, std::vector<cv::Point2d>& pi) const;
/** \brief Project points
*/
void ProjectPoints(const cv::Mat& object_points, const cv::Mat& rotation_vector,
const cv::Mat& translation_vector, cv::Mat& image_points) const
{
cv::projectPoints(object_points, rotation_vector, translation_vector, calib_K, calib_D, image_points);
return;
}
/** \brief Project points
*/
void ProjectPoints(const cv::Mat& object_points, double gl[16], cv::Mat& image_points) const;
/** \brief Project points */
void ProjectPoints(const cv::Mat& object_points, const Pose& pose, cv::Mat& image_points) const;
protected:
int calib_x_res;
int calib_y_res;
int x_res;
int y_res;
cv::Mat_<double> calib_K;
cv::Mat_<double> calib_D;
private:
bool LoadCalibOpenCV(const char* calibfile);
bool SaveCalibOpenCV(const char* calibfile) const;
}; // end of class Camera
/*****************************************************************************
*
*** class Homography
*
* Simple Homography class for finding and projecting points between two planes.
*
*****************************************************************************/
class ALVAR_EXPORT Homography
{
public:
/** \brief Constructor */
Homography() : H(3, 3)
{
return;
}
Homography(const Homography& src) = delete;
~Homography() = default;
Homography& operator=(const Homography& rhs) = delete;
/** \brief Find Homography for two point-sets */
void Find(const std::vector<PointDouble>& pw, const std::vector<PointDouble>& pi);
/** \brief Project points using the Homography */
void ProjectPoints(const std::vector<PointDouble>& from, std::vector<PointDouble>& to) const;
protected:
cv::Mat_<double> H;
}; // end of class Homography
} // namespace alvar
#endif