KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
camera_backproject.h
Go to the documentation of this file.
1 /* =========================================================================== *
2  |
3  | Copyright (c) 1994-2011 by Kobus Barnard (author)
4  |
5  | Personal and educational use of this code is granted, provided that this
6  | header is kept intact, and that the authorship is not misrepresented, that
7  | its use is acknowledged in publications, and relevant papers are cited.
8  |
9  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
10  |
11  | Please note that the code in this file has not necessarily been adequately
12  | tested. Naturally, there is no guarantee of performance, support, or fitness
13  | for any particular task. Nonetheless, I am interested in hearing about
14  | problems that you encounter.
15  |
16  | Author: Kyle Simek, Ernesto Brau, Jinyan Guan
17  * =========================================================================== */
18 
19 /* $Id$ */
20 
21 #ifndef KJB_CPP_CAMERA_BACKPROJECT
22 #define KJB_CPP_CAMERA_BACKPROJECT
23 
25 #include <l_cpp/l_algorithm.h>
26 #include <l_cpp/l_util.h>
27 #include <l/l_sys_io.h>
28 #include <l/l_sys_lib.h>
29 #include <m_cpp/m_matrix.h>
30 #include <m_cpp/m_vector.h>
31 #include <g_cpp/g_line.h>
32 #include <g_cpp/g_camera.h>
33 #include <limits>
34 
35 namespace kjb {
36 
43 {
44 public:
46  (
47  const Perspective_camera& cam,
48  const Vector& plane,
49  const Vector& plane_normal
50  ) :
51  ground_plane_(plane),
52  ground_normal_(plane_normal),
54  M(cam.build_camera_matrix())
55  {
57  }
58 
62  Vector operator()(double u, double v) const
63  {
64  Vector intersection;
65  double t;
66 
67  // Init point on screen
68  Vector screen_point(u, v);
69 
70  screen_point.resize(3, 1.0);
71 
72  // Get 3D directional vector from the center of the camera to the point
73  Vector camera_dir = backproject(screen_point, M);
74 
75  // Line-plane intersection in 3D
78  camera_dir,
81 
82  // Check for valid intersection
83  if (t != t)
84  {
85  /* t == NaN */
86  /* line and plane are coincident */
87  return Vector();
88  }
89  else if(t == std::numeric_limits<double>::infinity())
90  {
91  /* t == infinity */
92  /* line and plane are parallel */
93  return Vector();
94  }
95  else
96  {
97  return camera_center_ + t * camera_dir;
98  }
99  }
100 
101 protected:
106 };
107 
114 {
115  typedef Back_projector Base;
116 public:
118  (
119  const Perspective_camera& cam,
120  double ground_height
121  ) :
123  cam,
124  Vector(0.0, ground_height, 0.0),
125  Vector(0.0, 1.0, 0.0)),
126  y_behind_camera_()
127  {
128  init(cam);
129  }
130 
134  Vector operator()(double u, double v) const
135  {
136  Vector result = Base::operator()(u, v);
137  if(result.size() == 0) return result;
138 
139  if(result[2] > y_behind_camera_)
140  {
141  return Vector();
142  }
143 
144  return result;
145  }
146 
147 private:
148  void init(const Perspective_camera& cam)
149  {
151  Matrix R = cam.get_modelview_matrix();
152  R.resize(3,3);
153 
154  // get y-value t
155  Vector cam_down = R.transpose() * Vector(0.0, -1.0, 0.0);
156  double t = intersect_line_with_plane(camera_center_, cam_down,
158 
159  // !nan; i.e. line and plane are coincident
160  assert(!(t != t));
161 
162  // line and plane are parallel
163  assert(t != std::numeric_limits<double>::infinity());
164 
165  y_behind_camera_ = (camera_center_ + t * cam_down)[2];
166  }
167 
168  double y_behind_camera_;
169 };
170 
175 inline
176 double get_3d_height
177 (
178  const Vector& bottom_2d,
179  const Vector& top_2d,
180  const Perspective_camera& camera
181 )
182 {
183  KJB(ASSERT((bottom_2d(1) < top_2d(1))));
184  Ground_back_projector ground_back_projector(camera, 0.0);
185  Vector bottom_3d = ground_back_projector(bottom_2d(0), bottom_2d(1));
186  if(bottom_3d.empty())
187  {
188  return 0.0;
189  }
190 
191  Vector height_normal(0.0, 0.0, -1.0);
192  Back_projector height_back_projector(camera, bottom_3d, height_normal);
193 
194  Vector top_3d = height_back_projector(top_2d(0), top_2d(1));
195  if(top_3d.empty())
196  {
197  return 0.0;
198  }
199 
200  return top_3d(1);
201 }
202 
203 } // namespace kjb
204 
205 #endif /*KJB_CPP_CAMERA_BACKPROJECT */
206 
Vector & resize(int new_length, Value_type pad=Value_type(0))
Resize vector, retaining previous values.
Definition: m_vector.cpp:242
Vector camera_center_
Definition: camera_backproject.h:104
Definition for the Matrix class, a thin wrapper on the KJB Matrix struct and its related functionalit...
size_type size() const
Alias to get_length(). Required to comply with stl Container concept.
Definition: m_vector.h:510
bool empty() const
Returns true iff size is zero. Required to comply with stl Container concept.
Definition: m_vector.h:526
Definition: camera_backproject.h:113
Vector ground_plane_
Definition: camera_backproject.h:102
#define KJB(x)
Definition: l_util.h:9
Vector3 backproject(const Vector3 &homo_screen_coord, const Matrix_d< 3, 4 > &camera_matrix)
Same as backproject(), but using Vector3.
Definition: g_camera.cpp:57
Definition: camera_backproject.h:42
#define ASSERT(condition, message)
Definition: Assert.h:45
Matrix M
Definition: camera_backproject.h:105
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
Declarations for Line class.
Ground_back_projector(const Perspective_camera &cam, double ground_height)
Definition: camera_backproject.h:118
Vector operator()(double u, double v) const
Definition: camera_backproject.h:134
St_perspective_camera for modeling a perspective camera using the classic Forsyth and Ponce parametri...
Vector operator()(double u, double v) const
Definition: camera_backproject.h:62
Definition: perspective_camera.h:93
const kjb::Vector & get_camera_centre() const
returns the camera centre
Definition: perspective_camera.h:194
Matrix & resize(int new_rows, int new_cols, Value_type pad=Value_type(0))
Resize this matrix, retaining previous values. Space is reused if possible. Otherwise requires a new ...
Definition: m_matrix.cpp:457
Matrix transpose() const
Transpose this matrix.
Definition: m_matrix.cpp:395
const Matrix & get_modelview_matrix() const
Definition: perspective_camera.h:340
Vector ground_normal_
Definition: camera_backproject.h:103
double intersect_line_with_plane(const kjb::Vector &line_point, const kjb::Vector &line_direction, const kjb::Vector &plane_point, const kjb::Vector &plane_normal)
Definition: g_line.h:222
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
const Matrix & build_camera_matrix() const
Definition: perspective_camera.h:487
double get_3d_height(const Vector &bottom_2d, const Vector &top_2d, const Perspective_camera &camera)
Back project the 2d points to find the height in 3D. Assume that the bottom is on the ground...
Definition: camera_backproject.h:177
Back_projector(const Perspective_camera &cam, const Vector &plane, const Vector &plane_normal)
Definition: camera_backproject.h:46
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...