Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ProjectionParametersZoom.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_ProjectionParametersZoom_hh__
27 #define __BIAS_ProjectionParametersZoom_hh__
28 #include "bias_config.h"
29 
30 #include <Base/Common/BIASpragmaStart.hh>
31 #include <Geometry/ProjectionParametersBase.hh>
32 #include <Base/Geometry/KMatrix.hh>
33 
34 namespace BIAS {
35 
36 #ifndef __BIAS_CPDiscreteParam__
37 #define __BIAS_CPDiscreteParam__
38 
39  // distortion parameters at different zoom levels
40  struct BIASImage_EXPORT CPDiscreteParam{
41  double zoom;
42  double focallength;
43  // radial/tangential distortion parameters.
44  // 4th order model of brown.
45  double kc1,kc2,kc3,kc4;
46  };
47 
48 #endif
49 
50  /** @class ProjectionParametersZoom
51  @brief camera parameters which define the mapping between rays in the
52  camera coordinate system and pixels in the image as well as external pose
53  @author streckel
54  */
55  class BIASGeometry_EXPORT ProjectionParametersZoom:
57  public:
58 
59  ProjectionParametersZoom(const unsigned int width = 0,
60  const unsigned int height = 0)
61  : ProjectionParametersBase(width, height)
62  {
63  K_.SetIdentity();
64  invK_.SetIdentity();
65  zoom_=0;
66  focallengthZoom_=0;
67  kc1Zoom_=0; kc2Zoom_=0; kc3Zoom_=0; kc4Zoom_=0;
68  };
69 
70  virtual const bool
73  return true;
75  if (ppz) {
76  if (kc1Zoom_ != ppz->kc1Zoom_ || kc2Zoom_ != ppz->kc2Zoom_ ||
77  kc3Zoom_ != ppz->kc3Zoom_ || kc4Zoom_ != ppz->kc4Zoom_ ||
78  focallengthZoom_ != ppz->focallengthZoom_ || zoom_ != ppz->zoom_)
79  return true;
80  }
81  return false;
82  }
83 
85  *this = P;
86  K_ = P.K_;
87  invK_ = P.invK_;
88  knownparams_vect_ = P.knownparams_vect_;
89  zoom_ = P.zoom_;
90  focallengthZoom_ = P.focallengthZoom_;
91  kc1Zoom_ = P.kc1Zoom_;
92  kc2Zoom_ = P.kc2Zoom_;
93  kc3Zoom_ = P.kc3Zoom_;
94  kc4Zoom_ = P.kc4Zoom_;
95  };
96 
98 
99  /** @brief calculates the projection of a point
100  in the camera coordinate system
101  to a pixel in the image plane of the camera
102 
103  In the simplest case perspective pinhole projection x = K * y
104  where y is the projection of X using y = (R^T | -R^T C) X
105  */
106  virtual HomgPoint2D ProjectLocal(const Vector3<double>& point,
107  bool IgnoreDistortion = false) const;
108 
109  /** @brief calculates the projection of a point in the local camera
110  coordinate system to a pixel in the image plane of the camera. The
111  only case when this function may not compute the 2d point is when the
112  camera center and the 3d point coincide. This case must be indicated
113  by a negative return value. In all other cases, a 2d point must be
114  computed, *particularily* when the 3d point is behind the camera
115  or when the resulting 2d point is at infinity.
116 
117  In the simplest case perspective pinhole projection x = K * point
118  where point is transformed of X using point = (R^T | -R^T C) X
119  @author woelk 08/2008 (c) www.vision-n.de */
120  virtual int ProjectLocal(const Vector3<double>& point, HomgPoint2D &p2d,
121  bool IgnoreDistortion = false) const;
122 
123  /** @brief map points from image onto unit diameter image plane in 3D.
124  * Chosen is the image plane with distance of one from the image center.
125  */
126  HomgPoint3D UnProjectToImagePlane(const HomgPoint2D& pos,
127  const double& depth = 1.0,
128  bool IgnoreDistortion = false) const;
129 
130 
131  /** @brief calculates the viewing ray from the camera center (in the
132  camera coordinate system) which belongs to the image position pos
133  e.g. (0,0,1) means optical axis
134  */
135  virtual void UnProjectLocal(const HomgPoint2D& pos, Vector3<double>& pointOnRay,
136  Vector3<double>& direction,
137  bool IgnoreDistortion = false) const;
138 
139  /** @brief Using the Matlab camera calibration toolbox parameters
140  for an undistortion of distorted coordinates.
141  TODO: faster implementation via Lookup-table
142  @author streckel 06/2004 */
143  virtual bool Undistort(BIAS::HomgPoint2D& point2d) const;
144 
145  /** @brief Using the Matlab camera calibration toolbox parameters
146  for a distortion of undistorted coordinates. Inverse to the
147  undistort function.
148  TODO: faster implementation via Lookup-table
149  @author streckel 06/2004 */
150  virtual bool Distort(BIAS::HomgPoint2D& point2d) const;
151 
152  /** @brief covariant virtual copy constructor for use in Projection */
153  virtual ProjectionParametersZoom* Clone() const {
154  return new ProjectionParametersZoom(*this);
155  };
156 
157 
158  inline void Clear(){
159  knownparams_vect_.clear();
160  }
161 
162  int AddZoomStep(double zoom, double focallength,
163  double kc1, double kc2, double kc3, double kc4);
164 
165  /* @brief get cached KMatrix
166  */
167  inline void GetK(KMatrix& K) const {
168  K = K_;
169  };
170 
171  /* @brief get cached inverted KMatrix
172  */
173  inline void GetKinv(KMatrix& invK) const {
174  invK = invK_;
175  };
176 
177  /* @brief get the PMatrix composed from the projection object
178  */
179  inline virtual BIAS::PMatrix GetP() const {
180  return BIAS::PMatrix(K_,GetR(),GetC());
181  }
182 
183  /** @brief Get the current camera zoom.
184  @author buck */
185  inline int GetZoom(double& zoom) const {
186  zoom = zoom_;
187  return 0;
188  }
189 
190  /** @brief Get the current camera focal length.
191  @author buck */
192  inline int GetFocalLength(double& f) const {
193  f = focallengthZoom_;
194  return 0;
195  }
196 
197  /** @brief Get the current camera Undistortion.
198  @author buck */
199  inline void GetUndistortion(double& kc1, double& kc2,
200  double& kc3, double& kc4) const
201  {
202  kc1 = kc1Zoom_;
203  kc2 = kc2Zoom_;
204  kc3 = kc3Zoom_;
205  kc4 = kc4Zoom_;
206  }
207 
208 
209  /** @brief Set CCD cell-size in meter, virtual overload to
210  recalculate K_.
211  @author streckel */
212  virtual inline void SetAspectratio(const double CellSizeX,
213  const double AspectRatio) {
215  ComputeK_();
216  }
217 
218  /** @brief Set principal point in pixels relative to top left corner,
219  virtual overload to recalculate K_
220  @author streckel */
221  virtual inline void SetPrincipal(const double x, const double y) {
223  ComputeK_();
224  }
225 
226  /** @brief Set the current camera zoom, the focal length will be
227  calculated.
228  @author buck */
229  inline int SetZoom(const double abszoom) {
230  zoom_=abszoom;
231  focallengthZoom_ = InterpolateFocalLength_();
232  ComputeK_();
233  return 0;
234  }
235 
236  /** @brief Set the current camera focal length, the zoom will be
237  calculated.
238  @author buck */
239  inline int SetFocalLength(const double f) {
240  focallengthZoom_=f;
241  zoom_=InterpolateAbsZoom_();
242  ComputeK_();
243  return 0;
244  }
245 
246  /** @brief Set the zoom and focal length of a zoom camera to the
247  default settings.
248  @author streckel 06/2004*/
249  inline void SetUnzoomed() {
250  if (knownparams_vect_.size()>0) {
251  SetZoom(knownparams_vect_[0].zoom);
252  }
253  }
254 
255 
256  /** @brief adapt internal params to resampled image
257  @param ratio 2.0 = downsample, 0.5 = upsample
258  */
259  virtual inline void Rescale(double ratio, const double offset) {
260  BIASERR("unimplemented");
261  BIASABORT;
262  }
263 
264  virtual inline void Rescale(unsigned int width, unsigned int height){
265  BIASERR("unimplemented");
266  BIASABORT;
267  }
268 
269 #ifdef BIAS_HAVE_XML2
270  /** @brief specialization of XML block name function */
271  virtual int XMLGetClassName(std::string& TopLevelTag,
272  double& Version) const;
273 
274  /** @brief specialization of XML write function */
275  virtual int XMLOut(const xmlNodePtr Node, XMLIO& XMLObject) const;
276 
277  /** @brief specialization of XML read function */
278  virtual int XMLIn(const xmlNodePtr Node, XMLIO& XMLObject);
279 #endif
280 
281  /// friend functions
282  friend std::ostream& operator<<(std::ostream &os,
283  const ProjectionParametersZoom& p);
284  protected:
285  // focal length, aspect ratio, principal point in K_, inverse in invK_
286  KMatrix K_, invK_;
287 
288  // array of camera calibration constants for zoom cameras
289  std::vector<CPDiscreteParam> knownparams_vect_;
290 
291  // calibration settings for the current zoom setting of a zoom camera
292  // zoom and focallengthZoom are dependend:
293  // if zoom is set, focallengthZoom will be calculated and vice versa
294  // focal length is stored in pixel
295  double zoom_;
297  double kc1Zoom_, kc2Zoom_, kc3Zoom_, kc4Zoom_;
298 
299  void InterpolateDistortionFromIndex_(unsigned int i,double relnorm);
300  double InterpolateFocalLength_(); // w/ given abszoom
301  double InterpolateAbsZoom_(); // w/ given focal length
302 
303  /** @brief computes the cached KMatrix and its inverse
304  call after every parameter change */
305  void ComputeK_();
306 
307 
308 
309  };
310 
311  inline std::ostream& operator<<(std::ostream &os,
312  const ProjectionParametersZoom& p)
313  {
314  os << "ProjectionParametersZoom:" << std::endl;
315  return os;
316  }
317 
318 } // end namespace
319 
320 
321 #include <Base/Common/BIASpragmaEnd.hh>
322 
323 #endif
324 
virtual void SetPrincipal(const double x, const double y)
Set principal point (in pixels relative to top left corner).
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
int SetFocalLength(const double f)
Set the current camera focal length, the zoom will be calculated.
ProjectionParametersZoom(const ProjectionParametersZoom &P)
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
virtual const bool DoIntrinsicsDiffer(const ProjectionParametersBase *p) const
ProjectionParametersZoom(const unsigned int width=0, const unsigned int height=0)
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
virtual void SetPrincipal(const double x, const double y)
Set principal point in pixels relative to top left corner, virtual overload to recalculate K_...
virtual ProjectionParametersZoom * Clone() const
covariant virtual copy constructor for use in Projection
virtual const bool DoIntrinsicsDiffer(const ProjectionParametersBase *p) const
class BIASGeometry_EXPORT PMatrix
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 Rescale(double ratio, const double offset)
adapt internal params to resampled image
std::vector< CPDiscreteParam > knownparams_vect_
virtual BIAS::PMatrix GetP() const
int SetZoom(const double abszoom)
Set the current camera zoom, the focal length will be calculated.
K describes the mapping from world coordinates (wcs) to pixel coordinates (pcs).
Definition: KMatrix.hh:48
virtual void SetAspectratio(const double AspectRatio)
Set CCD aspect ratio (i.e.
void GetUndistortion(double &kc1, double &kc2, double &kc3, double &kc4) const
Get the current camera Undistortion.
describes a projective 3D -&gt; 2D mapping in homogenous coordinates
Definition: PMatrix.hh:88
void SetUnzoomed()
Set the zoom and focal length of a zoom camera to the default settings.
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
virtual void SetAspectratio(const double CellSizeX, const double AspectRatio)
Set CCD cell-size in meter, virtual overload to recalculate K_.
int GetZoom(double &zoom) const
Get the current camera zoom.
virtual void Rescale(unsigned int width, unsigned int height)
adapt internal parameters to new image size
int GetFocalLength(double &f) const
Get the current camera focal length.