Image Component Library (ICL)
Camera.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLGeom/src/ICLGeom/Camera.h **
10 ** Module : ICLGeom **
11 ** Authors: Erik Weitnauer, Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLUtils/Size.h>
35 #include <ICLUtils/Point32f.h>
36 #include <ICLUtils/Rect.h>
37 #include <ICLUtils/Rect32f.h>
38 #include <ICLUtils/Exception.h>
39 #include <ICLUtils/Array2D.h>
40 #include <ICLGeom/PlaneEquation.h>
41 #include <ICLGeom/ViewRay.h>
42 
43 
44 // the icl namespace
45 namespace icl {
46  namespace geom{
48 
133  public:
134 
137  float clipZNear;
138  float clipZFar;
140  float viewportZMin, viewportZMax;
141 
142  RenderParams(const utils::Size &chipSize=utils::Size::VGA, float clipZNear=1.0, float clipZFar=10000.,
143  const utils::Rect &viewport=utils::Rect(0,0,640,480), float viewportZMin=0.,
144  float viewportZMax=1.):
145  chipSize(chipSize), clipZNear(clipZNear), clipZFar(clipZFar), viewport(viewport),
146  viewportZMin(viewportZMin), viewportZMax(viewportZMax) {}
147  };
148 
152  };
153 
156 
159  Camera(const Vec &pos=Vec(0,0,10,1),
161  const Vec &norm=Vec(0,0,-1,1),
162  const Vec &up=Vec(1,0,0,1),
163  float f=3,
164  const utils::Point32f &principalPointOffset=utils::Point32f(320,240),
165  float sampling_res_x = 200,
166  float sampling_res_y = 200,
167  float skew = 0,
168  const RenderParams &renderParams = RenderParams()): m_pos(pos), m_norm(norm), m_up(up), m_f(f),
169  m_px(principalPointOffset.x), m_py(principalPointOffset.y),
170  m_mx(sampling_res_x), m_my(sampling_res_y), m_skew(skew), m_renderParams(renderParams) {}
171 
173 
177  Camera(const std::string &filename, const std::string &prefix="config.") ;
179 
183  Camera(std::istream &configDataStream, const std::string &prefix="config.") ;
184 
185 
187  static Camera createFromProjectionMatrix(const math::FixedMatrix<icl32f,4,3> &Q, float focalLength=1);
189 
191 
198  static Camera calibrate(std::vector<Vec> Xws, std::vector<utils::Point32f> xis, float focalLength=1,
199  bool performLMAOptimization=true)
200  ;
201 
203 
205  static Camera calibrate_pinv(std::vector<Vec> Xws, std::vector<utils::Point32f> xis, float focalLength=1,
206  bool performLMAOptimization=true)
207  ;
208 
210 
211  static Camera calibrate_extrinsic(const std::vector<Vec> &Xws, const std::vector<utils::Point32f> &xis,
212  const Camera &intrinsicCamValue, const RenderParams &renderParams=RenderParams(),
213  bool performLMAOptimization=true)
214  ;
215 
217 
218  static Camera calibrate_extrinsic(const std::vector<Vec> &Xws, const std::vector<utils::Point32f> &xis,
219  const Mat &camIntrinsicProjectionMatrix, const RenderParams &renderParams=RenderParams(),
220  bool performLMAOptimization=true)
221  ;
222 
224 
321  static Camera calibrate_extrinsic(std::vector<Vec> Xws, std::vector<utils::Point32f> xis,
322  float fx, float fy, float s, float px ,float py,
323  const RenderParams &renderParams=RenderParams(),
324  bool performLMAOptimization=true)
325  ;
326 
328  static Camera optimize_camera_calibration_lma(const std::vector<Vec> &Xws,
329  const std::vector<utils::Point32f> xis,
330  const Camera &init);
331 
334  // projections normal
336  utils::Point32f project(const Vec &Xw) const;
338  void project(const std::vector<Vec> &Xws, std::vector<utils::Point32f> &dst) const;
340  const std::vector<utils::Point32f> project(const std::vector<Vec> &Xws) const;
341 
342 
343 
344  // projections OpenGL
346  Vec projectGL(const Vec &Xw) const;
348  void projectGL(const std::vector<Vec> &Xws, std::vector<Vec> &dst) const;
350  const std::vector<Vec> projectGL(const std::vector<Vec> &Xws) const;
351 
352 
353  // projection magic
355  ViewRay getViewRay(const utils::Point32f &pixel) const;
356 
358 
360  std::vector<ViewRay> getViewRays(const std::vector<utils::Point32f> &pixels) const;
361 
363 
365  utils::Array2D<ViewRay> getAllViewRays() const;
366 
368  ViewRay getViewRay(const Vec &Xw) const;
369 
371  Vec estimate3DPosition(const utils::Point32f &pixel, const PlaneEquation &plane) const ;
373 
382  static Vec getIntersection(const ViewRay &v,
383  const PlaneEquation &plane) ;
384 
387  // setters and getters
389  void setRotation(const Mat3x3 &rot);
391  void setRotation(const Vec &rot);
392 
394 
398  void setTransformation(const Mat &m);
399 
401 
403  void setWorldTransformation(const Mat &m);
404 
405 
407 
409  void setWorldFrame(const Mat &m);
410 
412  Mat getCSTransformationMatrix() const;
413 
415  Mat getInvCSTransformationMatrix() const;
416 
418  Mat getCSTransformationMatrixGL() const;
420  Mat getProjectionMatrix() const;
422  Mat getProjectionMatrixGL() const;
423  Mat getViewportMatrixGL() const;
424 
426  math::FixedMatrix<icl32f,4,3> getQMatrix() const;
427 
429  math::FixedMatrix<icl32f,3,4> getInvQMatrix() const;
430 
432  inline void translate(const Vec &d) { m_pos += d; }
433 
434  std::string toString() const;
435 
438  const std::string &getName() const { return m_name; }
439  const Vec &getPosition() const { return m_pos; }
440  const Vec &getNorm() const { return m_norm; }
441  const Vec &getUp() const { return m_up; }
442  Vec getHoriz() const { return cross(m_up, m_norm); }
443  float getFocalLength() const { return m_f; }
445  float getPrincipalPointOffsetX() const { return m_px; }
446  float getPrincipalPointOffsetY() const { return m_py; }
447  float getSamplingResolutionX() const { return m_mx; }
448  float getSamplingResolutionY() const { return m_my; }
449  float getSkew() const { return m_skew; }
450  const RenderParams &getRenderParams() const { return m_renderParams; }
451  RenderParams &getRenderParams() { return m_renderParams; }
452 
455  void setName(const std::string &name) { m_name = name; }
456  void setPosition(const Vec &pos) { m_pos = pos; }
457  void setNorm(const Vec &norm, bool autoOrthogonalizeRotationMatrix=false) {
458  m_norm = norm; m_norm[3] = 0; m_norm.normalize(); m_norm[3] = 1;
459  if(autoOrthogonalizeRotationMatrix) orthogonalizeRotationMatrix();
460  }
462  void setUp(const Vec &up, bool autoOrthogonalizeRotationMatrix=false) {
463  m_up = up; m_up[3] = 0; m_up.normalize(); m_up[3] = 1;
464  if(autoOrthogonalizeRotationMatrix) orthogonalizeRotationMatrix();
465  }
467  void orthogonalizeRotationMatrix();
468 
469  void setFocalLength(float value) { m_f = value; }
470  void setPrincipalPointOffset(float px, float py) { m_px = px; m_py = py; }
471  void setPrincipalPointOffset(const utils::Point32f &p) { m_px = p.x; m_py = p.y; }
472  void setSamplingResolutionX(float value) { m_mx = value; }
473  void setSamplingResolutionY(float value) { m_my = value; }
474  void setSamplingResolution(float x, float y) { m_mx = x; m_my = y; }
475  void setSkew(float value) { m_skew = value; }
476  void setRenderParams(const RenderParams &rp) { m_renderParams = rp; }
477 
478 
481 
486  void setResolution(const utils::Size &newScreenSize);
487 
489 
492  void setResolution(const utils::Size &newScreenSize, const utils::Point &newPrincipalPointOffset);
493 
495  inline const utils::Size &getResolution() const { return m_renderParams.chipSize; }
498 
577  static Vec estimate_3D(const std::vector<Camera*> cams,
578  const std::vector<utils::Point32f> &UVs,
579  bool removeInvalidPoints=false) ;
580 
582  Mat estimatePose(const std::vector<Vec> &objectCoords,
583  const std::vector<utils::Point32f> &imageCoords,
584  bool performLMAOptimization=true);
585 
587 
589  static Vec estimate_3D_svd(const std::vector<Camera*> cams,
590  const std::vector<utils::Point32f> &UVs);
591 
594 
600  static Camera create_camera_from_calibration_or_udist_file(const std::string &filename) ;
601 
602  protected:
603  static Mat createTransformationMatrix(const Vec &norm, const Vec &up, const Vec &pos);
604 
605  private:
606  // General Parameters
607  std::string m_name;
608 
609  // External Parameters
613 
614  // Internal Parameters
615  float m_f;
616  float m_px, m_py;
617  float m_mx, m_my;
618  float m_skew;
619 
620  // Rendering Parameters
622 
625 
627  static void checkAndFixPoints(std::vector<Vec> &worldPoints, std::vector<utils::Point32f> &imagePoints)
628  ;
630  static void load_camera_from_stream(std::istream &is,
631  const std::string &prefix,
632  Camera &cam);
633  };
634 
636  ICLGeom_API std::ostream &operator<<(std::ostream &os, const Camera &cam);
637 
639  ICLGeom_API std::istream &operator>>(std::istream &is, Camera &cam) ;
640 
641  } // namespace geom
642 } // namespace icl
643 
Utility structure for calculation of view-ray / plane intersections.
Definition: PlaneEquation.h:41
const utils::Size & getResolution() const
returns the current chipSize (camera resolution in pixels)
Definition: Camera.h:495
undocument this line if you encounter any issues!
Definition: Any.h:37
void setName(const std::string &name)
Definition: Camera.h:455
const Vec & getPosition() const
Definition: Camera.h:439
Vec m_pos
center position vector
Definition: Camera.h:610
ICLQt_API core::Img< T > norm(const core::Img< T > &image)
normalize an images range to [0,255]
math::FixedColVector< T, 4 > cross(const math::FixedMatrix< T, 1, 4 > &v1, const math::FixedMatrix< T, 1, 4 > &v2)
homogeneous 3D cross-product
Definition: HomogeneousMath.h:203
float getPrincipalPointOffsetX() const
Definition: Camera.h:445
void normalize(int iChannel, const utils::Range< Type > &srcRange, const utils::Range< Type > &dstRange)
Scales pixel values from given min/max values to new min/max values (for internal use)
float m_f
focal length
Definition: Camera.h:615
float y
y position of this point
Definition: Point32f.h:48
float getPrincipalPointOffsetY() const
Definition: Camera.h:446
utils::Point32f getPrincipalPointOffset() const
Definition: Camera.h:444
void setPrincipalPointOffset(float px, float py)
Definition: Camera.h:470
#define ICLGeom_API
Definition: CompatMacros.h:179
const std::string & getName() const
Definition: Camera.h:438
std::string m_name
name of the camera (visualized in the scene if set)
Definition: Camera.h:607
const Vec & getUp() const
Definition: Camera.h:441
Definition: Camera.h:135
void setUp(const Vec &up, bool autoOrthogonalizeRotationMatrix=false)
gets automatically normalized
Definition: Camera.h:462
Vec getHoriz() const
Definition: Camera.h:442
float getSamplingResolutionX() const
Definition: Camera.h:447
Vec m_up
vector pointing to pos. y axis on image plane
Definition: Camera.h:612
void setPosition(const Vec &pos)
Definition: Camera.h:456
static const Size VGA
Video Graphics Array res. 640x480.
Definition: Size.h:82
void setSamplingResolutionX(float value)
Definition: Camera.h:472
float m_my
sampling resolution
Definition: Camera.h:617
Camera class.
Definition: Camera.h:132
void setSamplingResolutionY(float value)
Definition: Camera.h:473
void setNorm(const Vec &norm, bool autoOrthogonalizeRotationMatrix=false)
Definition: Camera.h:457
utils::Size chipSize
chip size in [pixels] for transformation to chip coordinates
Definition: Camera.h:136
NotEnoughDataPointsException()
Definition: Camera.h:151
float clipZFar
position of the far clipping plane in [mm]
Definition: Camera.h:138
const RenderParams & getRenderParams() const
Definition: Camera.h:450
ICLGeom_API std::ostream & operator<<(std::ostream &os, const Camera &cam)
ostream operator (writes camera in XML core::format)
void normalize(T norm=2)
inplace normalization
Definition: FixedMatrix.h:844
Size class of the ICL.
Definition: Size.h:61
float m_py
principal point offset
Definition: Camera.h:616
RenderParams m_renderParams
Definition: Camera.h:621
void setPrincipalPointOffset(const utils::Point32f &p)
Definition: Camera.h:471
float getFocalLength() const
Definition: Camera.h:443
ICLGeom_API std::istream & operator>>(std::istream &is, Camera &cam)
istream operator parses a camera from an XML-string
Vec4D32f Vec
Short typedef for 4D float vectors.
Definition: GeomDefs.h:87
We need at least 6 Data points in general positions.
Definition: Camera.h:150
float viewportZMin
Definition: Camera.h:140
const Vec & getNorm() const
Definition: Camera.h:440
void translate(const Vec &d)
translates the current position vector
Definition: Camera.h:432
void setFocalLength(float value)
Definition: Camera.h:469
void setSamplingResolution(float x, float y)
Definition: Camera.h:474
This is a view-ray's line equation in parameter form.
Definition: ViewRay.h:49
Single precission 3D Vectors Point class of the ICL.
Definition: Point32f.h:41
float getSkew() const
Definition: Camera.h:449
Simple 2D-Array class that provides shallow copy per default.
Definition: Array2D.h:61
Point class of the ICL used e.g. for the Images ROI offset.
Definition: Point.h:58
float m_skew
skew parameter in the camera projection, should be zero
Definition: Camera.h:618
float x
x position of this point
Definition: Point32f.h:45
Base class for Exception handling in the ICL.
Definition: Exception.h:42
math::FixedColVector< T, 4 > project(math::FixedMatrix< T, 1, 4 > v, T z)
perform perspective projection
Definition: HomogeneousMath.h:192
float clipZNear
position of the near clipping plane in [mm]
Definition: Camera.h:137
Rectangle class of the ICL used e.g. for the Images ROI-rect.
Definition: Rect.h:95
utils::Rect viewport
in [pixel]
Definition: Camera.h:139
math::FixedMatrix< icl32f, 3, 3 > Mat3x3
internal typedef
Definition: Camera.h:155
void setSkew(float value)
Definition: Camera.h:475
void setRenderParams(const RenderParams &rp)
Definition: Camera.h:476
Vec m_norm
normal vector of image plane
Definition: Camera.h:611
RenderParams(const utils::Size &chipSize=utils::Size::VGA, float clipZNear=1.0, float clipZFar=10000., const utils::Rect &viewport=utils::Rect(0, 0, 640, 480), float viewportZMin=0., float viewportZMax=1.)
Definition: Camera.h:142
RenderParams & getRenderParams()
Definition: Camera.h:451
float getSamplingResolutionY() const
Definition: Camera.h:448