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
int width
int height
double prior(double phi, double theta, double nx, double ny, double nz)
Definition: ccCompass.cpp:1197
void SetWidth(const size_t width)
Definition: camera.h:164
bool VerifyParams() const
Definition: camera.cc:182
void InitializeWithName(const std::string &model_name, const double focal_length, const size_t width, const size_t height)
Definition: camera.cc:212
double FocalLengthY() const
Definition: camera.cc:121
void InitializeWithId(const int model_id, const double focal_length, const size_t width, const size_t height)
Definition: camera.cc:203
Eigen::Vector2d ImageToWorld(const Eigen::Vector2d &image_point) const
Definition: camera.cc:219
void SetPriorFocalLength(const bool prior)
Definition: camera.h:170
double MeanFocalLength() const
Definition: camera.cc:100
void SetModelIdFromName(const std::string &model_name)
Definition: camera.cc:57
const std::vector< double > & Params() const
Definition: camera.h:176
double ImageToWorldThreshold(const double threshold) const
Definition: camera.cc:226
bool operator==(const Camera &other) const
Definition: camera.h:190
bool SetParamsFromString(const std::string &string)
Definition: camera.cc:172
camera_t CameraId() const
Definition: camera.h:154
std::string ModelName() const
Definition: camera.cc:49
int ModelId() const
Definition: camera.h:158
void SetPrincipalPointX(const double ppx)
Definition: camera.cc:158
std::string ParamsToString() const
Definition: camera.cc:170
void SetFocalLengthX(const double focal_length_x)
Definition: camera.cc:134
const std::vector< size_t > & PrincipalPointIdxs() const
Definition: camera.cc:67
void Rescale(const double scale)
Definition: camera.cc:237
void SetParams(const std::vector< double > &params)
Definition: camera.h:188
void SetFocalLengthY(const double focal_length_y)
Definition: camera.cc:140
void SetFocalLength(const double focal_length)
Definition: camera.cc:127
bool HasPriorFocalLength() const
Definition: camera.h:168
bool operator!=(const Camera &other) const
Definition: camera.h:197
size_t NumParams() const
Definition: camera.h:174
bool HasBogusParams(const double min_focal_length_ratio, const double max_focal_length_ratio, const double max_extra_param) const
Definition: camera.cc:186
double FocalLength() const
Definition: camera.cc:109
void SetPrincipalPointY(const double ppy)
Definition: camera.cc:164
void SetCameraId(const camera_t camera_id)
Definition: camera.h:156
std::string ParamsInfo() const
Definition: camera.cc:96
const std::vector< size_t > & ExtraParamsIdxs() const
Definition: camera.cc:71
Eigen::Matrix3d CalibrationMatrix() const
Definition: camera.cc:75
double FocalLengthX() const
Definition: camera.cc:115
const std::vector< size_t > & FocalLengthIdxs() const
Definition: camera.cc:63
double PrincipalPointX() const
Definition: camera.cc:146
Eigen::Vector2d WorldToImage(const Eigen::Vector2d &world_point) const
Definition: camera.cc:230
size_t Width() const
Definition: camera.h:160
double PrincipalPointY() const
Definition: camera.cc:152
size_t Height() const
Definition: camera.h:162
const double * ParamsData() const
Definition: camera.h:184
void SetHeight(const size_t height)
Definition: camera.h:166
void SetModelId(const int model_id)
Definition: camera.cc:51
bool IsUndistorted() const
Definition: camera.cc:194
uint32_t camera_t
Definition: types.h:58