Berkeley SfM
camera.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 model implemented using computations and
41 // parameters from the OpenCV camera model:
42 // http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html
43 //
44 // By default, the camera is staring down its +Z axis. +X and +Y are the
45 // camera's right-facing and upward-facing vectors in this coordinate frame.
46 //
47 ///////////////////////////////////////////////////////////////////////////////
48 
49 #ifndef BSFM_CAMERA_CAMERA_H
50 #define BSFM_CAMERA_CAMERA_H
51 
52 #include <matching/feature_match.h>
53 
54 #include "camera_extrinsics.h"
55 #include "camera_intrinsics.h"
56 #include "../util/types.h"
57 
58 namespace bsfm {
59 
60 using Eigen::Matrix3d;
61 
62 class Camera {
63  public:
64 
65  // Constructor and destructor.
66  Camera() { };
67  ~Camera() { };
68 
69  // Constructor given extrinsics and intrinsics.
71 
72  // Set extrinsics.
73  void SetExtrinsics(const CameraExtrinsics&);
74 
75  // Set instrinsics.
76  void SetIntrinsics(const CameraIntrinsics&);
77 
78  // Extract mutable/immutable extrinsics/intrinsics.
81  const CameraExtrinsics& Extrinsics() const;
82  const CameraIntrinsics& Intrinsics() const;
83 
84  // Get the projection matrix by multiplying intrinsics and extrinsics.
85  Matrix34d P() const;
86 
87  // Get the camera intrinsics matrix, K.
88  Matrix3d K() const;
89 
90  // Get the camera extrinsics matrix, [R | t].
91  Matrix34d Rt() const;
92 
93  // Get the camera's world frame translation from extrinsics.
94  Vector3d Translation() const;
95 
96  // Get the camera's world frame rotation from extrinsics.
97  Matrix3d Rotation() const;
98 
99  // Get the camera's world frame rotation in axis angle parameterization.
100  Vector3d AxisAngleRotation() const;
101 
102  // Transform points from world to camera coordinates.
103  void WorldToCamera(double wx, double wy, double wz, double* cx, double* cy,
104  double* cz) const;
105 
106  // Transform points from camera to world coordinates.
107  void CameraToWorld(double cx, double cy, double cz,
108  double* wx, double* wy, double* wz) const;
109 
110  // Transform points from camera to image coordinates. Return whether the input
111  // point is in front of the camera.
112  bool CameraToImage(double cx, double cy, double cz,
113  double* u_distorted, double* v_distorted) const;
114 
115  // Transform points from world to image coordinates. Return whether the input
116  // point is in front of the camera.
117  bool WorldToImage(double wx, double wy, double wz,
118  double* u_distorted, double* v_distorted) const;
119 
120  // Convert a normalized unit direction into the image by distorting it with
121  // the camera's radial distortion parameters.
122  bool DirectionToImage(double u_normalized, double v_normalized,
123  double* u_distorted, double* v_distorted) const;
124 
125  // Convert a distorted image coordinate pair to a normalized direction
126  // vector using the camera's radial distortion parameters.
127  void ImageToDirection(double u_distorted, double v_distorted,
128  double* u_normalized, double* v_normalized) const;
129 
130  // Warp a point into the image.
131  void Distort(double u, double v,
132  double* u_distorted, double* v_distorted) const;
133 
134  // Rectilinearize point.
135  void Undistort(double u_distorted, double v_distorted,
136  double* u, double* v) const;
137 
138 private:
141 }; //\class Camera
142 
143 } //\bsfm
144 #endif
Matrix3d K() const
Definition: camera.cpp:78
bool WorldToImage(double wx, double wy, double wz, double *u_distorted, double *v_distorted) const
Definition: camera.cpp:121
CameraIntrinsics & MutableIntrinsics()
Definition: camera.cpp:68
void Undistort(double u_distorted, double v_distorted, double *u, double *v) const
Definition: camera.cpp:152
Vector3d AxisAngleRotation() const
Definition: camera.cpp:98
void ImageToDirection(double u_distorted, double v_distorted, double *u_normalized, double *v_normalized) const
Definition: camera.cpp:138
void WorldToCamera(double wx, double wy, double wz, double *cx, double *cy, double *cz) const
Definition: camera.cpp:103
void SetIntrinsics(const CameraIntrinsics &)
Definition: camera.cpp:62
CameraIntrinsics intrinsics_
Definition: camera.h:140
CameraExtrinsics & MutableExtrinsics()
Definition: camera.cpp:67
const CameraIntrinsics & Intrinsics() const
Definition: camera.cpp:70
Matrix34d P() const
Definition: camera.cpp:73
void SetExtrinsics(const CameraExtrinsics &)
Definition: camera.cpp:57
CameraExtrinsics extrinsics_
Definition: camera.h:139
void Distort(double u, double v, double *u_distorted, double *v_distorted) const
Definition: camera.cpp:146
void CameraToWorld(double cx, double cy, double cz, double *wx, double *wy, double *wz) const
Definition: camera.cpp:109
Definition: camera.cpp:50
::Eigen::Matrix< double, 3, 4 > Matrix34d
Definition: types.h:87
Vector3d Translation() const
Definition: camera.cpp:88
bool CameraToImage(double cx, double cy, double cz, double *u_distorted, double *v_distorted) const
Definition: camera.cpp:116
const CameraExtrinsics & Extrinsics() const
Definition: camera.cpp:69
Matrix34d Rt() const
Definition: camera.cpp:83
Matrix3d Rotation() const
Definition: camera.cpp:93
bool DirectionToImage(double u_normalized, double v_normalized, double *u_distorted, double *v_distorted) const
Definition: camera.cpp:130