ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
camera.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <vector>
11 
12 #include "util/types.h"
13 
14 namespace colmap {
15 
16 // Camera class that holds the intrinsic parameters. Cameras may be shared
17 // between multiple images, e.g., if the same "physical" camera took multiple
18 // pictures with the exact same lens and intrinsics (focal length, etc.).
19 // This class has a specific distortion model defined by a camera model class.
20 class Camera {
21 public:
22  Camera();
23 
24  // Access the unique identifier of the camera.
25  inline camera_t CameraId() const;
26  inline void SetCameraId(const camera_t camera_id);
27 
28  // Access the camera model.
29  inline int ModelId() const;
30  std::string ModelName() const;
31  void SetModelId(const int model_id);
32  void SetModelIdFromName(const std::string& model_name);
33 
34  // Access dimensions of the camera sensor.
35  inline size_t Width() const;
36  inline size_t Height() const;
37  inline void SetWidth(const size_t width);
38  inline void SetHeight(const size_t height);
39 
40  // Access focal length parameters.
41  double MeanFocalLength() const;
42  double FocalLength() const;
43  double FocalLengthX() const;
44  double FocalLengthY() const;
45  void SetFocalLength(const double focal_length);
46  void SetFocalLengthX(const double focal_length_x);
47  void SetFocalLengthY(const double focal_length_y);
48 
49  // Check if camera has prior focal length.
50  inline bool HasPriorFocalLength() const;
51  inline void SetPriorFocalLength(const bool prior);
52 
53  // Access principal point parameters. Only works if there are two
54  // principal point parameters.
55  double PrincipalPointX() const;
56  double PrincipalPointY() const;
57  void SetPrincipalPointX(const double ppx);
58  void SetPrincipalPointY(const double ppy);
59 
60  // Get the indices of the parameter groups in the parameter vector.
61  const std::vector<size_t>& FocalLengthIdxs() const;
62  const std::vector<size_t>& PrincipalPointIdxs() const;
63  const std::vector<size_t>& ExtraParamsIdxs() const;
64 
65  // Get intrinsic calibration matrix composed from focal length and principal
66  // point parameters, excluding distortion parameters.
67  Eigen::Matrix3d CalibrationMatrix() const;
68 
69  // Get human-readable information about the parameter vector ordering.
70  std::string ParamsInfo() const;
71 
72  // Access the raw parameter vector.
73  inline size_t NumParams() const;
74  inline const std::vector<double>& Params() const;
75  inline std::vector<double>& Params();
76  inline double Params(const size_t idx) const;
77  inline double& Params(const size_t idx);
78  inline const double* ParamsData() const;
79  inline double* ParamsData();
80  inline void SetParams(const std::vector<double>& params);
81 
82  // Concatenate parameters as comma-separated list.
83  std::string ParamsToString() const;
84 
85  // Set camera parameters from comma-separated list.
86  bool SetParamsFromString(const std::string& string);
87 
88  // Check whether parameters are valid, i.e. the parameter vector has
89  // the correct dimensions that match the specified camera model.
90  bool VerifyParams() const;
91 
92  // Check whether camera is already undistorted
93  bool IsUndistorted() const;
94 
95  // Check whether camera has bogus parameters.
96  bool HasBogusParams(const double min_focal_length_ratio,
97  const double max_focal_length_ratio,
98  const double max_extra_param) const;
99 
100  // Initialize parameters for given camera model and focal length, and set
101  // the principal point to be the image center.
102  void InitializeWithId(const int model_id,
103  const double focal_length,
104  const size_t width,
105  const size_t height);
106  void InitializeWithName(const std::string& model_name,
107  const double focal_length,
108  const size_t width,
109  const size_t height);
110 
111  // Project point in image plane to world / infinity.
112  Eigen::Vector2d ImageToWorld(const Eigen::Vector2d& image_point) const;
113 
114  // Convert pixel threshold in image plane to world space.
115  double ImageToWorldThreshold(const double threshold) const;
116 
117  // Project point from world / infinity to image plane.
118  Eigen::Vector2d WorldToImage(const Eigen::Vector2d& world_point) const;
119 
120  // Rescale camera dimensions and accordingly the focal length and
121  // and the principal point.
122  void Rescale(const double scale);
123  void Rescale(const size_t width, const size_t height);
124 
125  inline bool operator==(const Camera& other) const;
126  inline bool operator!=(const Camera& other) const;
127 
128 private:
129  // The unique identifier of the camera. If the identifier is not specified
130  // it is set to `kInvalidCameraId`.
131  camera_t camera_id_;
132 
133  // The identifier of the camera model. If the camera model is not specified
134  // the identifier is `kInvalidCameraModelId`.
135  int model_id_;
136 
137  // The dimensions of the image, 0 if not initialized.
138  size_t width_;
139  size_t height_;
140 
141  // The focal length, principal point, and extra parameters. If the camera
142  // model is not specified, this vector is empty.
143  std::vector<double> params_;
144 
145  // Whether there is a safe prior for the focal length,
146  // e.g. manually provided or extracted from EXIF
147  bool prior_focal_length_;
148 };
149 
151 // Implementation
153 
154 camera_t Camera::CameraId() const { return camera_id_; }
155 
156 void Camera::SetCameraId(const camera_t camera_id) { camera_id_ = camera_id; }
157 
158 int Camera::ModelId() const { return model_id_; }
159 
160 size_t Camera::Width() const { return width_; }
161 
162 size_t Camera::Height() const { return height_; }
163 
164 void Camera::SetWidth(const size_t width) { width_ = width; }
165 
166 void Camera::SetHeight(const size_t height) { height_ = height; }
167 
168 bool Camera::HasPriorFocalLength() const { return prior_focal_length_; }
169 
171  prior_focal_length_ = prior;
172 }
173 
174 size_t Camera::NumParams() const { return params_.size(); }
175 
176 const std::vector<double>& Camera::Params() const { return params_; }
177 
178 std::vector<double>& Camera::Params() { return params_; }
179 
180 double Camera::Params(const size_t idx) const { return params_[idx]; }
181 
182 double& Camera::Params(const size_t idx) { return params_[idx]; }
183 
184 const double* Camera::ParamsData() const { return params_.data(); }
185 
186 double* Camera::ParamsData() { return params_.data(); }
187 
188 void Camera::SetParams(const std::vector<double>& params) { params_ = params; }
189 
190 bool Camera::operator==(const Camera& other) const {
191  return CameraId() == other.CameraId() && ModelId() == other.ModelId() &&
192  Width() == other.Width() && Height() == other.Height() &&
193  Params() == other.Params() &&
195 }
196 
197 bool Camera::operator!=(const Camera& other) const { return !(*this == other); }
198 
199 } // namespace colmap
colmap::Camera::Camera
Camera()
Definition: camera.cc:42
colmap::Camera::FocalLengthY
double FocalLengthY() const
Definition: camera.cc:121
types.h
colmap::Camera::SetFocalLengthX
void SetFocalLengthX(const double focal_length_x)
Definition: camera.cc:134
camera_models.h
colmap::Camera::PrincipalPointX
double PrincipalPointX() const
Definition: camera.cc:146
colmap::CameraModelImageToWorld
void CameraModelImageToWorld(const int model_id, const std::vector< double > &params, const double x, const double y, double *u, double *v)
Definition: camera_models.h:1497
colmap::Camera::HasBogusParams
bool HasBogusParams(const double min_focal_length_ratio, const double max_focal_length_ratio, const double max_extra_param) const
Definition: camera.cc:186
colmap::CameraModelParamsInfo
std::string CameraModelParamsInfo(const int model_id)
Definition: camera_models.cc:139
colmap::Camera::WorldToImage
Eigen::Vector2d WorldToImage(const Eigen::Vector2d &world_point) const
Definition: camera.cc:230
colmap::ExistsCameraModelWithId
bool ExistsCameraModelWithId(const int model_id)
Definition: camera_models.cc:98
colmap::Camera::ParamsToString
std::string ParamsToString() const
Definition: camera.cc:170
colmap::Camera::ParamsInfo
std::string ParamsInfo() const
Definition: camera.cc:96
colmap::Camera::SetFocalLength
void SetFocalLength(const double focal_length)
Definition: camera.cc:127
colmap::Camera::ModelName
std::string ModelName() const
Definition: camera.cc:49
colmap::Camera::Params
const std::vector< double > & Params() const
Definition: camera.h:176
colmap::Camera::InitializeWithName
void InitializeWithName(const std::string &model_name, const double focal_length, const size_t width, const size_t height)
Definition: camera.cc:212
colmap::Camera::operator==
bool operator==(const Camera &other) const
Definition: camera.h:190
colmap::Camera::SetModelIdFromName
void SetModelIdFromName(const std::string &model_name)
Definition: camera.cc:57
colmap::Camera::PrincipalPointY
double PrincipalPointY() const
Definition: camera.cc:152
colmap::kInvalidCameraModelId
static const int kInvalidCameraModelId
Definition: camera_models.h:55
colmap::Camera::SetPrincipalPointY
void SetPrincipalPointY(const double ppy)
Definition: camera.cc:164
colmap::CameraModelFocalLengthIdxs
const std::vector< size_t > & CameraModelFocalLengthIdxs(const int model_id)
Definition: camera_models.cc:156
colmap::Camera::SetPrincipalPointX
void SetPrincipalPointX(const double ppx)
Definition: camera.cc:158
colmap::Camera::CameraId
camera_t CameraId() const
Definition: camera.h:154
colmap::CameraModelExtraParamsIdxs
const std::vector< size_t > & CameraModelExtraParamsIdxs(const int model_id)
Definition: camera_models.cc:186
colmap::ExistsCameraModelWithName
bool ExistsCameraModelWithName(const std::string &model_name)
Definition: camera_models.cc:94
colmap::Camera::ImageToWorldThreshold
double ImageToWorldThreshold(const double threshold) const
Definition: camera.cc:226
width
int width
Definition: FileIOFactory.cpp:142
colmap::CameraModelIdToName
std::string CameraModelIdToName(const int model_id)
Definition: camera_models.cc:111
colmap::Camera::SetPriorFocalLength
void SetPriorFocalLength(const bool prior)
Definition: camera.h:170
colmap::Camera::ImageToWorld
Eigen::Vector2d ImageToWorld(const Eigen::Vector2d &image_point) const
Definition: camera.cc:219
size
int size
Definition: FileIOFactory.cpp:130
height
int height
Definition: FileIOFactory.cpp:143
colmap::CameraModelHasBogusParams
bool CameraModelHasBogusParams(const int model_id, const std::vector< double > &params, const size_t width, const size_t height, const double min_focal_length_ratio, const double max_focal_length_ratio, const double max_extra_param)
Definition: camera_models.cc:233
colmap::Camera::FocalLengthX
double FocalLengthX() const
Definition: camera.cc:115
colmap::Camera::ExtraParamsIdxs
const std::vector< size_t > & ExtraParamsIdxs() const
Definition: camera.cc:71
colmap::Camera::CalibrationMatrix
Eigen::Matrix3d CalibrationMatrix() const
Definition: camera.cc:75
colmap::CameraModelWorldToImage
void CameraModelWorldToImage(const int model_id, const std::vector< double > &params, const double u, const double v, double *x, double *y)
Definition: camera_models.h:1479
e
const double * e
Definition: essential_matrix_poly.h:10
misc.h
colmap::CameraModelPrincipalPointIdxs
const std::vector< size_t > & CameraModelPrincipalPointIdxs(const int model_id)
Definition: camera_models.cc:171
colmap::Camera::operator!=
bool operator!=(const Camera &other) const
Definition: camera.h:197
colmap::CameraModelVerifyParams
bool CameraModelVerifyParams(const int model_id, const std::vector< double > &params)
Definition: camera_models.cc:215
colmap::Camera::Width
size_t Width() const
Definition: camera.h:160
colmap::Camera::SetHeight
void SetHeight(const size_t height)
Definition: camera.h:166
camera.h
colmap::Camera::ParamsData
const double * ParamsData() const
Definition: camera.h:184
colmap::kInvalidCameraId
const camera_t kInvalidCameraId
Definition: types.h:75
colmap::Camera::ModelId
int ModelId() const
Definition: camera.h:158
colmap::Camera::Rescale
void Rescale(const double scale)
Definition: camera.cc:237
colmap::Camera::HasPriorFocalLength
bool HasPriorFocalLength() const
Definition: camera.h:168
colmap::Camera::PrincipalPointIdxs
const std::vector< size_t > & PrincipalPointIdxs() const
Definition: camera.cc:67
colmap::Camera::MeanFocalLength
double MeanFocalLength() const
Definition: camera.cc:100
colmap::CameraModelImageToWorldThreshold
double CameraModelImageToWorldThreshold(const int model_id, const std::vector< double > &params, const double threshold)
Definition: camera_models.h:1515
colmap::Camera::FocalLength
double FocalLength() const
Definition: camera.cc:109
colmap::CameraModelNameToId
int CameraModelNameToId(const std::string &model_name)
Definition: camera_models.cc:102
logging.h
colmap::Camera::FocalLengthIdxs
const std::vector< size_t > & FocalLengthIdxs() const
Definition: camera.cc:63
prior
double prior(double phi, double theta, double nx, double ny, double nz)
Definition: ccCompass.cpp:1197
colmap::Camera::SetFocalLengthY
void SetFocalLengthY(const double focal_length_y)
Definition: camera.cc:140
colmap::CameraModelNumParams
size_t CameraModelNumParams(const int model_id)
Definition: camera_models.cc:201
colmap::Camera::SetModelId
void SetModelId(const int model_id)
Definition: camera.cc:51
colmap
Definition: AutomaticReconstructionController.h:17
colmap::Camera::IsUndistorted
bool IsUndistorted() const
Definition: camera.cc:194
colmap::Camera::Height
size_t Height() const
Definition: camera.h:162
colmap::Camera::SetWidth
void SetWidth(const size_t width)
Definition: camera.h:164
colmap::Camera::NumParams
size_t NumParams() const
Definition: camera.h:174
colmap::camera_t
uint32_t camera_t
Definition: types.h:58
colmap::VectorToCSV
std::string VectorToCSV(const std::vector< T > &values)
Definition: misc.h:150
colmap::Camera::SetParams
void SetParams(const std::vector< double > &params)
Definition: camera.h:188
colmap::Camera
Definition: camera.h:20
colmap::Camera::SetCameraId
void SetCameraId(const camera_t camera_id)
Definition: camera.h:156
colmap::Camera::InitializeWithId
void InitializeWithId(const int model_id, const double focal_length, const size_t width, const size_t height)
Definition: camera.cc:203
colmap::CameraModelInitializeParams
std::vector< double > CameraModelInitializeParams(const int model_id, const double focal_length, const size_t width, const size_t height)
Definition: camera_models.cc:120
colmap::Camera::VerifyParams
bool VerifyParams() const
Definition: camera.cc:182
colmap::Camera::SetParamsFromString
bool SetParamsFromString(const std::string &string)
Definition: camera.cc:172