NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
camera_params.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef CAMERA_CAMERA_PARAMS_H_
20 #define CAMERA_CAMERA_PARAMS_H_
21 
23 #include <Eigen/Core>
24 
25 #include <string>
26 #include <vector>
27 #include <algorithm>
28 
29 // Forward declare mat type so that we don't have to include OpenCV if we're
30 // not going to use it.
31 namespace cv {
32  class Mat;
33 } // end namespace cv
34 
35 // Functionality for undistorting and re-distorting images
36 namespace camera {
37  const char distortion_msg[] = "Tangent distortion constant for this "
38  "camera. Use 0.923169 for ISS imagery, and 0.925417 for images "
39  "from the tango device.";
40 
41  // Definition of camera frames
42  //
43  // RAW - This is how the image came to us.
44  // DISTORTED - Similar to RAW, but we've cropped out everything that is
45  // non-image
46  // DISTORTED_C - Same as DISTORTED, but the origin is the center of the image
47  // UNDISTORTED - A new camera frame that is create by remove lens distortion
48  // and placing the optical center directly center of the image.
49  // UNDISTORTED_C - Same as UNDISTORTED, but the origin is the center of the image
50  enum {
51  RAW,
56  };
57 
59  public:
61 
62  // Please load from file or specify all variables
63  CameraParameters() = delete;
64  explicit CameraParameters(std::string const& filename, std::string const& base_dir = "");
65  explicit CameraParameters(config_reader::ConfigReader* config, const char* name);
66  CameraParameters(Eigen::Vector2i const& image_size,
67  Eigen::Vector2d const& focal_length,
68  Eigen::Vector2d const& optical_center,
69  Eigen::VectorXd const& distortion = Eigen::VectorXd());
70 
71  // Used to create a remap table. This table is the same size as the
72  // UNDISTORTED image, where every pixel's value is the cooresponding pixel
73  // location in the DISTORTED image.
74  void GenerateRemapMaps(cv::Mat* remap_map, double scale = 1.0);
75 
76  // Conversion utilities
77  template <int SRC, int DEST>
78  void Convert(Eigen::Vector2d const& input, Eigen::Vector2d *output) const {
79  throw("Please use the explicitly specified conversions by using the correct enum.");
80  }
81 
82  // Utility to create intrinsic matrix for the correct coordinate frame
83  template <int FRAME>
84  Eigen::Matrix3d GetIntrinsicMatrix() const {
85  throw("Please use the explicitly specified frame by using the correct enum.");
86  }
87 
88  // Image size info. We don't give direct access because if the size
89  // changes, we need to recompute some temporary variables.
90  // These apply to coordinate frames DISTORTED, DISTORTED_C
91  void SetDistortedSize(Eigen::Vector2i const& image_size);
92  const Eigen::Vector2i& GetDistortedSize() const;
93  const Eigen::Vector2d& GetDistortedHalfSize() const;
94 
95  // These apply to UNDISTORTED, UNDISTORTED_C
96  void SetUndistortedSize(Eigen::Vector2i const& image_size);
97  const Eigen::Vector2i& GetUndistortedSize() const;
98  const Eigen::Vector2d& GetUndistortedHalfSize() const;
99 
100  // Start of DISTORTED inside RAW frame
101  void SetCropOffset(Eigen::Vector2i const& crop);
102  const Eigen::Vector2i& GetCropOffset() const;
103  // Optical offset in DISTORTED frame
104  void SetOpticalOffset(Eigen::Vector2d const& offset);
105  const Eigen::Vector2d& GetOpticalOffset() const;
106 
107  // Helper functions for focal length. Unless pixels are square,
108  // the focal length in x may differ than the one in y. Note that
109  // they are equal for undistorted images. Note that
110  // GetFocalLength() returns the mean of the two focal lengths
111  // while GetFocalVector() returns the vector.
112  void SetFocalLength(Eigen::Vector2d const&);
113  double GetFocalLength() const;
114  const Eigen::Vector2d& GetFocalVector() const;
115 
116  // This will change the lens distortion type based on the size of the
117  // vector. Size 0, no lens distortion, Size 1, FOV, Size 4 or 5, TSAI.
118  void SetDistortion(Eigen::VectorXd const& distortion);
119  const Eigen::VectorXd& GetDistortion() const;
120 
121  // Comparison operator
122  friend bool operator== (CameraParameters const& A, CameraParameters const& B) {
123  return (A.crop_offset_ == B.crop_offset_ &&
124  A.distorted_image_size_ == B.distorted_image_size_ &&
125  A.undistorted_image_size_ == B.undistorted_image_size_ &&
126  A.distorted_half_size_ == B.distorted_half_size_ &&
127  A.focal_length_ == B.focal_length_ &&
128  A.optical_offset_ == B.optical_offset_ &&
129  A.distortion_coeffs_ == B.distortion_coeffs_ &&
130  A.distortion_precalc1_ == B.distortion_precalc1_ &&
131  A.distortion_precalc2_ == B.distortion_precalc2_ &&
132  A.distortion_precalc3_ == B.distortion_precalc3_);
133  }
134 
135  private:
136  // Converts UNDISTORTED_C to DISTORTED_C
137  void DistortCentered(Eigen::Vector2d const& undistorted_c,
138  Eigen::Vector2d* distorted_c) const;
139  // Converts DISTORTED_C to UNDISTORTED_C
140  void UndistortCentered(Eigen::Vector2d const& distorted_c,
141  Eigen::Vector2d* undistorted_c) const;
142 
143  // Members
144  Eigen::Vector2i
145  crop_offset_, // Start of DISTORTED in RAW frame
146  distorted_image_size_, // Applies to DISTORTED, DISTORTED_C
147  undistorted_image_size_; // Applies to UNDISTORTED, UNDISTORTED_C,
148  // this is bigger than distorted image size
149  // because we need to expand the sensor to
150  // capture the unravelling from lens distortion
151  // removal.
152  Eigen::Vector2d distorted_half_size_, undistorted_half_size_;
153  Eigen::Vector2d
154  focal_length_, // In pixels, applies to DISTORTED, UNDISTORTED,
155  // DISTORTED_C, UNDISTORTED_C
156  optical_offset_; // Optical offset in DISTORTED frame
157 
158  // Distortion coefficients are in an arbitrary sized vector. The length of
159  // the vector tells us what lens distortion model we are using. Length 0 =
160  // No lens distortion, ideal camera. Length 1 = FOV/Tangent model, Length 4
161  // or 5 = TSAI/OpenCV model.
162  Eigen::VectorXd distortion_coeffs_;
163  double distortion_precalc1_, distortion_precalc2_, distortion_precalc3_;
164  };
165 
166 #define DECLARE_CONVERSION(TYPEA, TYPEB) \
167  template <> \
168  void CameraParameters::Convert<TYPEA, TYPEB>(Eigen::Vector2d const& input, Eigen::Vector2d *output) const
179 #undef DECLARE_CONVERSION
180 
181 #define DECLARE_INTRINSIC(TYPE) \
182  template <> \
183  Eigen::Matrix3d CameraParameters::GetIntrinsicMatrix<TYPE>() const
189 #undef DECLARE_INTRINSIC
190 } // namespace camera
191 
192 #endif // CAMERA_CAMERA_PARAMS_H_
camera::CameraParameters::GetOpticalOffset
const Eigen::Vector2d & GetOpticalOffset() const
Definition: camera_params.cc:197
camera::CameraParameters::Convert
void Convert(Eigen::Vector2d const &input, Eigen::Vector2d *output) const
Definition: camera_params.h:78
camera::CameraParameters::GenerateRemapMaps
void GenerateRemapMaps(cv::Mat *remap_map, double scale=1.0)
Definition: camera_params.cc:375
camera::CameraParameters::CameraParameters
CameraParameters()=delete
camera::DECLARE_INTRINSIC
DECLARE_INTRINSIC(RAW)
camera::UNDISTORTED
@ UNDISTORTED
Definition: camera_params.h:54
camera::CameraParameters::operator==
friend bool operator==(CameraParameters const &A, CameraParameters const &B)
Definition: camera_params.h:122
camera::CameraParameters::SetOpticalOffset
void SetOpticalOffset(Eigen::Vector2d const &offset)
Definition: camera_params.cc:193
camera
Definition: camera_model.h:26
camera::DECLARE_CONVERSION
DECLARE_CONVERSION(RAW, DISTORTED)
camera::CameraParameters::SetDistortedSize
void SetDistortedSize(Eigen::Vector2i const &image_size)
Definition: camera_params.cc:159
camera::CameraParameters
Definition: camera_params.h:58
camera::CameraParameters::GetFocalVector
const Eigen::Vector2d & GetFocalVector() const
Definition: camera_params.cc:209
camera::CameraParameters::GetDistortion
const Eigen::VectorXd & GetDistortion() const
Definition: camera_params.cc:245
camera::CameraParameters::GetDistortedSize
const Eigen::Vector2i & GetDistortedSize() const
Definition: camera_params.cc:164
camera::CameraParameters::GetCropOffset
const Eigen::Vector2i & GetCropOffset() const
Definition: camera_params.cc:189
camera::CameraParameters::GetIntrinsicMatrix
Eigen::Matrix3d GetIntrinsicMatrix() const
Definition: camera_params.h:84
name
std::string name
Definition: eps_simulator.cc:48
camera::CameraParameters::SetUndistortedSize
void SetUndistortedSize(Eigen::Vector2i const &image_size)
Definition: camera_params.cc:172
camera::DISTORTED
@ DISTORTED
Definition: camera_params.h:52
camera::CameraParameters::SetFocalLength
void SetFocalLength(Eigen::Vector2d const &)
Definition: camera_params.cc:201
camera::CameraParameters::GetDistortedHalfSize
const Eigen::Vector2d & GetDistortedHalfSize() const
Definition: camera_params.cc:168
camera::CameraParameters::GetFocalLength
double GetFocalLength() const
Definition: camera_params.cc:205
config_reader::ConfigReader
Definition: config_reader.h:48
camera::CameraParameters::GetUndistortedHalfSize
const Eigen::Vector2d & GetUndistortedHalfSize() const
Definition: camera_params.cc:181
camera::distortion_msg
const char distortion_msg[]
Definition: camera_params.h:37
camera::CameraParameters::GetUndistortedSize
const Eigen::Vector2i & GetUndistortedSize() const
Definition: camera_params.cc:177
config_reader.h
camera::CameraParameters::EIGEN_MAKE_ALIGNED_OPERATOR_NEW
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: camera_params.h:60
camera::UNDISTORTED_C
@ UNDISTORTED_C
Definition: camera_params.h:55
cv
Definition: camera_params.h:31
camera::DISTORTED_C
@ DISTORTED_C
Definition: camera_params.h:53
camera::RAW
@ RAW
Definition: camera_params.h:51
camera::CameraParameters::SetCropOffset
void SetCropOffset(Eigen::Vector2i const &crop)
Definition: camera_params.cc:185
camera::CameraParameters::SetDistortion
void SetDistortion(Eigen::VectorXd const &distortion)
Definition: camera_params.cc:213