Berkeley SfM
camera.cpp
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 ///////////////////////////////////////////////////////////////////////////////
45 
46 #include "camera.h"
47 
48 #include <Eigen/Core>
49 
50 namespace bsfm {
51 
52 // Constructor given extrinsics and intrinsics.
54  : extrinsics_(extrinsics), intrinsics_(intrinsics) {}
55 
56 // Set extrinsics.
57 void Camera::SetExtrinsics(const CameraExtrinsics &extrinsics) {
58  extrinsics_ = extrinsics;
59 }
60 
61 // Set instrinsics.
62 void Camera::SetIntrinsics(const CameraIntrinsics &intrinsics) {
63  intrinsics_ = intrinsics;
64 }
65 
66 // Extract mutable/immutable extrinsics/intrinsics.
71 
72 // Get the projection matrix by multiplying intrinsics and extrinsics.
74  return intrinsics_.K() * extrinsics_.Rt();
75 }
76 
77 // Get the camera intrinsics matrix, K.
78 Matrix3d Camera::K() const {
79  return intrinsics_.K();
80 }
81 
82 // Get the camera extrinsics matrix, [R | t].
84  return extrinsics_.Rt();
85 }
86 
87 // Get the camera's world frame translation from extrinsics.
88 Vector3d Camera::Translation() const {
89  return extrinsics_.Translation();
90 }
91 
92 // Get the camera's world frame rotation from extrinsics.
93 Matrix3d Camera::Rotation() const {
94  return extrinsics_.Rotation();
95 }
96 
97 // Get the camera's world frame rotation in axis angle parameterization.
98 Vector3d Camera::AxisAngleRotation() const {
100 }
101 
102 // Transform points from world to camera coordinates.
103 void Camera::WorldToCamera(double wx, double wy, double wz, double *cx,
104  double *cy, double *cz) const {
105  extrinsics_.WorldToCamera(wx, wy, wz, cx, cy, cz);
106 }
107 
108 // Transform points from camera to world coordinates.
109 void Camera::CameraToWorld(double cx, double cy, double cz, double *wx,
110  double *wy, double *wz) const {
111  extrinsics_.CameraToWorld(cx, cy, cz, wx, wy, wz);
112 }
113 
114 // Transform points from world to image coordinates. Return whether the point
115 // was visible to the camera.
116 bool Camera::CameraToImage(double cx, double cy, double cz, double *u_distorted,
117  double *v_distorted) const {
118  return intrinsics_.CameraToImage(cx, cy, cz, u_distorted, v_distorted);
119 }
120 
121 bool Camera::WorldToImage(double wx, double wy, double wz, double *u_distorted,
122  double *v_distorted) const {
123  double cx = 0.0, cy = 0.0, cz = 0.0;
124  WorldToCamera(wx, wy, wz, &cx, &cy, &cz);
125  return CameraToImage(cx, cy, cz, u_distorted, v_distorted);
126 }
127 
128 // Convert a normalized unit direction into the image by distorting it with the
129 // camera's radial distortion parameters.
130 bool Camera::DirectionToImage(double u_normalized, double v_normalized,
131  double *u_distorted, double *v_distorted) const {
132  return intrinsics_.DirectionToImage(u_normalized, v_normalized, u_distorted,
133  v_distorted);
134 }
135 
136 // Convert a distorted image coordinate pair to a normalized direction vector
137 // using the camera's radial distortion parameters.
138 void Camera::ImageToDirection(double u_distorted, double v_distorted,
139  double *u_normalized,
140  double *v_normalized) const {
141  intrinsics_.ImageToDirection(u_distorted, v_distorted, u_normalized,
142  v_normalized);
143 }
144 
145 // Warp a point into the image.
146 void Camera::Distort(double u, double v, double *u_distorted,
147  double *v_distorted) const {
148  intrinsics_.Distort(u, v, u_distorted, v_distorted);
149 }
150 
151 // Rectilinearize point.
152 void Camera::Undistort(double u_distorted, double v_distorted, double *u,
153  double *v) const {
154  intrinsics_.Undistort(u_distorted, v_distorted, u, v);
155 }
156 
157 } // namespace bsfm
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
void ImageToDirection(double u_distorted, double v_distorted, double *u_normalized, double *v_normalized) const
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
Vector3d Translation() const
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
bool DirectionToImage(double u_normalized, double v_normalized, double *u_distorted, double *v_distorted) const
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
void Distort(double u, double v, double *u_distorted, double *v_distorted) const
::Eigen::Matrix< double, 3, 4 > Matrix34d
Definition: types.h:87
Vector3d AxisAngle()
Definition: pose.cpp:162
void Undistort(double u_distorted, double v_distorted, double *u, double *v, int iterations=10) const
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
Matrix3d Rotation() const
bool CameraToImage(double cx, double cy, double cz, double *u_distorted, double *v_distorted) const
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