Berkeley SfM
camera_intrinsics.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: Erik Nelson ( eanelson@eecs.berkeley.edu )
35  * David Fridovich-Keil ( dfk@eecs.berkeley.edu )
36  */
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 //
40 // This class defines a camera's intrinsic parameters, calibration, and
41 // computations using the OpenCV camera model, with the minor modification that
42 // we will use a 5th-order distortion model rather than a 6th-order model. Math
43 // for the 5th-order distortion model can be found at the caltech vision page:
44 //
45 // http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html
46 // http://www.vision.caltech.edu/bouguetj/calib_doc/htmls/parameters.html
47 //
48 ///////////////////////////////////////////////////////////////////////////////
49 
50 #ifndef BSFM_CAMERA_CAMERA_INTRINSICS_H
51 #define BSFM_CAMERA_CAMERA_INTRINSICS_H
52 
53 #include <Eigen/Dense>
54 
55 namespace bsfm {
56 
57 using Eigen::Matrix3d;
58 using Eigen::Vector3d;
59 
61  public:
62  // Initialize to zero.
64 
65  // Assume no radial distortion, and image left and top are both zero.
66  CameraIntrinsics(const Matrix3d& K, int image_width, int image_height);
67 
68  // Full initialization.
69  CameraIntrinsics(int image_left, int image_top, int image_width,
70  int image_height, double f_u, double f_v, double c_u,
71  double c_v, double k1, double k2, double k3, double k4,
72  double k5);
73 
74  // Set individual parameters.
75  void SetImageLeft(int image_left);
76  void SetImageTop(int image_top);
77  void SetImageWidth(int image_width);
78  void SetImageHeight(int image_height);
79 
80  void SetFU(double f_u);
81  void SetFV(double f_v);
82  void SetCU(double c_u);
83  void SetCV(double c_v);
84 
85  void SetK(double k1, double k2, double k3, double k4, double k5);
86  void SetK1(double k1);
87  void SetK2(double k2);
88  void SetK3(double k3);
89  void SetK4(double k4);
90  void SetK5(double k5);
91 
92  void SetHorizontalFOV(double horizontal_fov);
93  void SetVerticalFOV(double vertical_fov);
94 
95  // Extract parameters.
96  int ImageLeft() const;
97  int ImageTop() const;
98  int ImageWidth() const;
99  int ImageHeight() const;
100  double f_u() const;
101  double f_v() const;
102  double c_u() const;
103  double c_v() const;
104  double k1() const;
105  double k2() const;
106  double k3() const;
107  double k4() const;
108  double k5() const;
109  double HorizontalFOV() const;
110  double VerticalFOV() const;
111 
112  // Get intrinsics matrix.
113  Matrix3d K() const;
114 
115  // Get inverse of intrinsics matrix.
116  Matrix3d Kinv() const;
117 
118  // Test if a point is in the image.
119  bool PointInImage(double u, double v) const;
120 
121  // Check if a point is in front of the camera.
122  bool CameraToImage(double cx, double cy, double cz, double *u_distorted,
123  double *v_distorted) const;
124 
125  bool DirectionToImage(double u_normalized, double v_normalized,
126  double *u_distorted, double *v_distorted) const;
127 
128  void ImageToDirection(double u_distorted, double v_distorted,
129  double *u_normalized, double *v_normalized) const;
130 
131  // Warp a point into the image.
132  void Distort(double u, double v, double *u_distorted,
133  double *v_distorted) const;
134 
135  // Rectilinearize point.
136  void Undistort(double u_distorted, double v_distorted, double *u, double *v,
137  int iterations = 10) const;
138 
139  private:
144  double f_u_;
145  double f_v_;
146  double c_u_;
147  double c_v_;
148  double k1_;
149  double k2_;
150  double k3_;
151  double k4_;
152  double k5_;
155 }; //\class CameraIntrinsics
156 
157 } //\namespace bsfm
158 
159 #endif
void ImageToDirection(double u_distorted, double v_distorted, double *u_normalized, double *v_normalized) const
void SetImageLeft(int image_left)
void SetVerticalFOV(double vertical_fov)
bool DirectionToImage(double u_normalized, double v_normalized, double *u_distorted, double *v_distorted) const
void SetImageHeight(int image_height)
bool PointInImage(double u, double v) const
void SetK(double k1, double k2, double k3, double k4, double k5)
Definition: camera.cpp:50
void SetImageWidth(int image_width)
void Distort(double u, double v, double *u_distorted, double *v_distorted) const
void SetImageTop(int image_top)
void Undistort(double u_distorted, double v_distorted, double *u, double *v, int iterations=10) const
void SetHorizontalFOV(double horizontal_fov)
bool CameraToImage(double cx, double cy, double cz, double *u_distorted, double *v_distorted) const