Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ProjectionParametersProjective.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef __BIAS_ProjectionParametersProjective_hh__
27 #define __BIAS_ProjectionParametersProjective_hh__
28 #include "bias_config.h"
29 
30 #include <Base/Common/BIASpragmaStart.hh>
31 #include <Geometry/ProjectionParametersBase.hh>
32 #include <Geometry/CoordinateTransform3D.hh>
33 #include <Geometry/PMatrix.hh>
34 
35 namespace BIAS {
36 
37  /** @class ProjectionParametersProjective
38  @brief 12 parametric p for completely projective reconstruction
39  scenarios, unknown focal lengths, self calibration, ...
40 
41  The handling of the "pose" in this class is special because we cannot
42  decompose P to get the center and the rotation.
43  The center computation is possible via the nullspace of P, but there is
44  not way to retrieve R. In addition, the local project and upproject methods
45  cannot be used as well a stuff like SetR, SetC, distort, undistort, etc.
46  Therefore a lot of BIASABORTs are return by this class.
47 
48  @author koeser 01/2006 update sedlazeck 08/2009
49  */
50  class BIASGeometry_EXPORT ProjectionParametersProjective:
52  public:
53 
54  ProjectionParametersProjective(const unsigned int width = 0,
55  const unsigned int height = 0)
56  : ProjectionParametersBase(width, height)
57  {
58  }
59 
61  {
62  *this = P;
63  }
64 
68  return *this;
69  }
70 
72 
73  virtual const bool
75  BIASERR("cannot compare extriniscs for this parameter type")
76  BIASABORT
77  return false;
78  }
79 
80  virtual const bool
82  BIASERR("cannot compare intrinsics for this parameter type")
83  BIASABORT
84  return false;
85  }
86 
87 
88  bool DoesPointProjectIntoImageLocal(const Vector3<double>& localX,
89  HomgPoint2D& x,
90  bool IgnoreDistortion = false) const;
91 
92  bool DoesPointProjectIntoImage(const Vector3<double>& X,
93  HomgPoint2D& x,
94  bool IgnoreDistortion) const;
95 
96  bool Distort(BIAS::HomgPoint2D& point2d) const;
97 
98 
99  bool Undistort(BIAS::HomgPoint2D& point2d) const;
100 
101  inline HomgPoint2D Project(const HomgPoint3D &X,
102  bool IgnoreDistortion = false) const {
103  HomgPoint3D X_G(X);
104  HomgPoint3D X_L = Pose_.GlobalToLocal(X_G);
105  X_L.Homogenize();
106  HomgPoint2D x_l(X_L[0], X_L[1], X_L[2]);
107  return x_l;
108  }
109 
110  inline int Project(const HomgPoint3D &X, HomgPoint2D &p2d,
111  bool IgnoreDistortion = false) const {
112  HomgPoint3D X_G(X);
113  HomgPoint3D X_L = Pose_.GlobalToLocal(X_G);
114  X_L.Homogenize();
115  p2d = HomgPoint2D(X_L[0], X_L[1], X_L[2]);
116  return 0;
117  }
118 
119 
120  inline void UnProjectToRay(const HomgPoint2D& pos, Vector3<double>& pointOnRay,
121  Vector3<double>& direction,
122  bool IgnoreDistortion=false) const {
123 
124  HomgPoint3D temp3D = HomgPoint3D(pos);
125  HomgPoint3D unprojected = Pose_.LocalToGlobal(temp3D);
126  unprojected.Homogenize();
127  direction = Vector3<double>(unprojected[0], unprojected[1], unprojected[2]);
128  pointOnRay = GetC();
129  }
130 
131  inline void UnProjectToRay(const HomgPoint2D& pos, Vector3<double>& pointOnRay,
132  Vector3<double>& direction,
133  const ProjectionParametersBase &proj,
134  bool IgnoreDistortion=false) const {
135  BIASERR("R matrix not available in projective case!!!")
136  BIASABORT
137  }
138 
139  virtual Vector3<double> UnProjectToPointLocal(const HomgPoint2D& pos, const double& depth,
140  bool IgnoreDistortion = false) const {
141  BIASERR("Cannot perform local unprojection in projective case!!!")
142  BIASABORT
143  Vector3<double> dummy;
144  return dummy;
145  }
146 
147  /** @brief calculates the projection of a point
148  in the camera coordinate system
149  to a pixel in the image plane of the camera
150 
151  In the simplest case perspective pinhole projection x = K * y
152  where y is the projection of X using y = (R^T | -R^T C) X
153  */
155  bool IgnoreDistortion = false) const {
156 
157  BIASERR("No local projection possible in projective case!!!")
158  BIASABORT
159 
160  HomgPoint2D dummy;
161  return dummy;
162  }
163 
164  /** @brief calculates the projection of a point in the local camera
165  coordinate system to a pixel in the image plane of the camera. The
166  only case when this function may not compute the 2d point is when the
167  camera center and the 3d point coincide. This case must be indicated
168  by a negative return value. In all other cases, a 2d point must be
169  computed, *particularily* when the 3d point is behind the camera
170  or when the resulting 2d point is at infinity.
171 
172  In the simplest case perspective pinhole projection x = K * point
173  where point is transformed of X using point = (R^T | -R^T C) X
174  @author woelk 08/2008 (c) www.vision-n.de */
175  virtual int ProjectLocal(const Vector3<double>& point, HomgPoint2D &p2d,
176  bool IgnoreDistortion = false) const;
177 
178  /** @brief map points from image onto unit diameter image plane in 3D.
179  * Chosen is the image plane with radius of one from the image center.
180  */
181  HomgPoint3D UnProjectToImagePlane(const HomgPoint2D& pos,
182  const double& depth = 1.0,
183  bool IgnoreDistortion = false) const;
184 
185 
186  /** @brief calculates the viewing ray from the camera center (in the
187  camera coordinate system) which belongs to the image position pos
188  e.g. (0,0,1) means optical axis
189  */
190  virtual void UnProjectLocal(const HomgPoint2D& pos, Vector3<double>& pointOnRay,
191  Vector3<double>& direction, bool IgnoreDistortion = false) const
192  {
193 
194  BIASERR("No local unprojection possible in projective case!!!")
195  BIASABORT
196 // HomgPoint3D X_L((Vector3<double>) pos);
197 // HomgPoint3D X_G = Pose_.LocalToGlobal(X_L);
198 // X_G.Homogenize();
199 // Vector3<double> x_g(X_G[0], X_G[1], X_G[2]);
200  }
201 
202  virtual Matrix3x3<double>
203  UnProjectCovLocal(const HomgPoint2D& pos,
204  const Matrix3x3<double>& cov,
205  bool IgnoreDistortion = false,
206  bool Normalize = false) const;
207 
208 
210  Matrix2x2<double>& Jac,
211  const bool homogenized=true) const{
212 
213  BIASERR("Not possible in projective case!!!")
214  BIASABORT
215 
216  return 0;
217  }
218 
219 
220  /** @brief get projection center */
221  virtual Vector3<double> GetC() const;
222 
223  virtual BIAS::Quaternion<double> GetQ() const {
224  BIASERR("PMatrix decomposition not possible in projective case!!!")
225  BIASABORT
226 
228  return dummy;
229  }
230 
231 
232 
233  virtual BIAS::Vector<double> GetCQ() const{
234  BIASERR("PMatrix decomposition not possible in projective case!!!")
235  BIASABORT
236 
237  BIAS::Vector<double> dummy;
238  return dummy;
239  }
240 
241  /** @brief return R (R^T which is in front part of PMatrix ) */
242  virtual RMatrix GetR() const {
243  BIASERR("PMatrix decomposition not possible in projective case!!!")
244  BIASABORT
245 
246  BIAS::RMatrix dummy;
247  return dummy;
248  };
249 
250  virtual const BIAS::Pose& GetPose() const{
251  BIASERR("PMatrix decomposition not possible in projective case!!!")
252  BIASABORT
253 
254  return Pose_;
255  }
256 
257 
259  BIASERR("PMatrix decomposition not possible in projective case!!!")
260  BIASABORT
261 
263  return dummy;
264  }
265 
267  BIASERR("PMatrix decomposition not possible in projective case, you can only use SetP!!!")
268  BIASABORT
269 
270  }
271 
273  BIASERR("PMatrix decomposition not possible in projective case!!!")
274  BIASABORT
275 
277  return dummy;
278  }
279 
280  /** @brief covariant virtual copy constructor for use in Projection */
282  return new ProjectionParametersProjective(*this);
283  };
284 
285 
286 
287 #ifdef BIAS_HAVE_XML2
288  /** @brief specialization of XML block name function */
289  virtual int XMLGetClassName(std::string& TopLevelTag,
290  double& Version) const;
291 
292  /** @brief specialization of XML write function */
293  virtual int XMLOut(const xmlNodePtr Node, XMLIO& XMLObject) const;
294 
295  /** @brief specialization of XML read function */
296  virtual int XMLIn(const xmlNodePtr Node, XMLIO& XMLObject);
297 #endif
298  /** @brief set 12 entries if projective P using a PMatrix */
299  void SetP(const Matrix3x4<double>& P) { Pose_.SetFromMatrix3x4(P);}
300 
301  void SetR(const RMatrix& R) {
302  BIASERR("dont call this for projective !");
303  BIASABORT
304  }
305  void SetC(const Vector3<double>& C) {
306  BIASERR("dont call this for projective !")
307  BIASABORT
308  }
309 
311  BIASERR("dont call this for projective !")
312  BIASABORT
313  }
314 
315  virtual void SetQC(const BIAS::Quaternion<double>& Q,
316  const BIAS::Vector3<double>& C){
317  BIASERR("dont call this for projective !")
318  BIASABORT
319 
320  };
321 
322 
323  virtual void SetPose(const BIAS::Pose pose) {
324  BIASERR("dont call this for projective !")
325  BIASABORT
326  };
327 
328 
329  void ValidatePose(){
330  BIASERR("dont call this for projective !")
331  BIASABORT
332  }
333 
334  virtual BIAS::KMatrix GetFakeKMatrix(double& imgsize, int resolution = 0,
335  const double& maxangle = 1.4) const{
336  BIASERR("dont call this for projective !")
337  BIASABORT
338 
339  BIAS::KMatrix dummy;
340  return dummy;
341 
342  };
343 
344  virtual BIAS::KMatrix GetFakeKMatrix(int resolution = 0,
345  const double& maxangle = 1.4) const{
346  BIASERR("dont call this for projective !")
347  BIASABORT
348 
349  BIAS::KMatrix dummy;
350  return dummy;
351 
352  };
353 
354  double ViewDifference(const ProjectionParametersBase* ppb) const {
355 
356  Vector3<double> C1 = GetC();
357  Vector3<double> C2 = ppb->GetC();
358 
359  double diff = (C1 - C2).NormL2();
360 
361  return diff;
362  }
363 
364 
366  PMatrix P = GetP();
367  P.Normalize();
368  SetP(P);
369  }
370 
371 
372  /** @brief return the complete pmatrix (internals+externals / projective)
373  used for projection */
374  inline virtual PMatrix GetP() const {
375  return PMatrix(Pose_.GetMatrix3x4());
376  }
377  friend std::ostream& operator<<(std::ostream &os,
379  };
380 
381 
382 
383  /////////////// inline code ///////////////////////
384  inline std::ostream& operator<<(std::ostream &os,
386  {
387  os<<"ProjectionParametersProjective:" << std::endl;
388  //todo remove this, is done in base class, output of special projective parameters here
389  os << "width " << p.width_ << " height " << p.height_ << std::endl;
390  os << p.Pose_ <<std::endl;
391  return os;
392  }
393 
394 } // end namespace
395 
396 
397 #include <Base/Common/BIASpragmaEnd.hh>
398 
399 #endif
400 
virtual BIAS::Vector3< double > GetC() const
Get projection center.
void SetP(const Matrix3x4< double > &P)
set 12 entries if projective P using a PMatrix
virtual BIAS::Quaternion< double > GetQ() const
Get orientation as unit quaternion.
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
void SetR(const RMatrix &R)
Set orientation from rotation matrix R.
virtual BIAS::Matrix3x4< double > GetExternals() const
Return cached 3x4 representation of external matrix [R|C].
int GetUnProjectionJacobian(const HomgPoint2D &x, Matrix2x2< double > &Jac, const bool homogenized=true) const
Get warp (local magnification/shear/...) when going from the real image to ideal image with focal len...
void UnProjectToRay(const HomgPoint2D &pos, Vector3< double > &pointOnRay, Vector3< double > &direction, const ProjectionParametersBase &proj, bool IgnoreDistortion=false) const
Calculates the view ray, which belongs to the given position on the image plane, using the given proj...
void Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint3D.hh:308
void ValidatePose()
Validate currently set pose.
void SetC(const Vector3< double > &C)
Set projection center.
virtual HomgPoint2D ProjectLocal(const Vector3< double > &point, bool IgnoreDistortion=false) const
calculates the projection of a point in the camera coordinate system to a pixel in the image plane of...
Slim class bundeling pose parametrization and associated covariance matrix.
virtual const BIAS::Pose & GetPose() const
Return complete pose object.
ProjectionParametersProjective(const ProjectionParametersProjective &P)
void SetPoseParametrization(const BIAS::PoseParametrization &pp)
Set pose from pose parametrization.
virtual BIAS::KMatrix GetFakeKMatrix(double &imgsize, int resolution=0, const double &maxangle=1.4) const
Returns a fake KMatrix for the camera.
virtual void UnProjectLocal(const HomgPoint2D &pos, Vector3< double > &pointOnRay, Vector3< double > &direction, bool IgnoreDistortion=false) const
calculates the viewing ray from the camera center (in the camera coordinate system) which belongs to ...
3D rotation matrix
Definition: RMatrix.hh:49
Represents 3d pose transformations, parametrized as Euclidean translation and unit quaternion orienta...
Definition: Pose.hh:73
virtual ProjectionParametersProjective * Clone() const
covariant virtual copy constructor for use in Projection
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
virtual BIAS::PoseParametrization GetPoseParametrization() const
Return copy of pose parametrization object.
virtual Vector3< double > UnProjectToPointLocal(const HomgPoint2D &pos, const double &depth, bool IgnoreDistortion=false) const
calculates a 3D point in the local camera coordinate system, which belongs to the image position pos ...
12 parametric p for completely projective reconstruction scenarios, unknown focal lengths...
int Project(const HomgPoint3D &X, HomgPoint2D &p2d, bool IgnoreDistortion=false) const
calculates the projection of a point in the world coordinate system to a pixel in the image plane of ...
unsigned int height_
height of image in pixels
ProjectionParametersProjective(const unsigned int width=0, const unsigned int height=0)
class BIASGeometry_EXPORT PMatrix
virtual RMatrix GetR() const
return R (R^T which is in front part of PMatrix )
virtual const bool DoExtrinsicsDiffer(const ProjectionParametersBase *p) const
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260
virtual void SetPose(const BIAS::Pose pose)
Set pose from pose object.
void SetQ(const BIAS::Quaternion< double > &Q)
Set orientation from unit quaternion Q.
unsigned int width_
width of image in pixels
void UnProjectToRay(const HomgPoint2D &pos, Vector3< double > &pointOnRay, Vector3< double > &direction, bool IgnoreDistortion=false) const
Calculates the view ray, which belongs to the given position on the image plane, in global coordinate...
ProjectionParametersProjective & operator=(const ProjectionParametersProjective &P)
virtual PMatrix GetP() const
return the complete pmatrix (internals+externals / projective) used for projection ...
K describes the mapping from world coordinates (wcs) to pixel coordinates (pcs).
Definition: KMatrix.hh:48
virtual void SetQC(const BIAS::Quaternion< double > &Q, const BIAS::Vector3< double > &C)
Set pose from unit quaternion Q and projection center C.
describes a projective 3D -&gt; 2D mapping in homogenous coordinates
Definition: PMatrix.hh:88
virtual const bool DoIntrinsicsDiffer(const ProjectionParametersBase *p) const
double ViewDifference(const ProjectionParametersBase *ppb) const
Returns difference between two projections like |C1-C2|.
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
virtual BIAS::Vector< double > GetCQ() const
Return pose as 7 vector (first projection center C, then unit quaternion Q representing orientation)...
class BIASGeometryBase_EXPORT HomgPoint2D
virtual ProjectionParametersBase & operator=(const ProjectionParametersBase &p)
HomgPoint2D Project(const HomgPoint3D &X, bool IgnoreDistortion=false) const
calculates the projection of a point in the world coordinate system to a pixel in the image plane of ...
class BIASGeometryBase_EXPORT HomgPoint3D
void Normalize()
scale P such that optical axis (first three entries of last row) has unit length
Definition: PMatrix.hh:509
virtual BIAS::KMatrix GetFakeKMatrix(int resolution=0, const double &maxangle=1.4) const