Basic Image AlgorithmS Library 2.8.0

ProjectionParametersZoom.hh

00001 /*
00002 This file is part of the BIAS library (Basic ImageAlgorithmS).
00003 
00004 Copyright (C) 2003-2009    (see file CONTACT for details)
00005   Multimediale Systeme der Informationsverarbeitung
00006   Institut fuer Informatik
00007   Christian-Albrechts-Universitaet Kiel
00008 
00009 
00010 BIAS is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU Lesser General Public License as published by
00012 the Free Software Foundation; either version 2.1 of the License, or
00013 (at your option) any later version.
00014 
00015 BIAS is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU Lesser General Public License for more details.
00019 
00020 You should have received a copy of the GNU Lesser General Public License
00021 along with BIAS; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 */
00024 
00025 
00026 #ifndef __BIAS_ProjectionParametersZoom_hh__
00027 #define __BIAS_ProjectionParametersZoom_hh__
00028 #include "bias_config.h"
00029 
00030 #include <Base/Common/BIASpragmaStart.hh>
00031 #include <Geometry/ProjectionParametersBase.hh>
00032 #include <Base/Geometry/KMatrix.hh>
00033 
00034 namespace BIAS {
00035 
00036 #ifndef __BIAS_CPDiscreteParam__
00037 #define __BIAS_CPDiscreteParam__
00038 
00039   // distortion parameters at different zoom levels
00040   struct BIASImage_EXPORT CPDiscreteParam{
00041     double zoom;
00042     double focallength;
00043     // radial/tangential distortion parameters.
00044     // 4th order model of brown.
00045     double kc1,kc2,kc3,kc4;
00046   };
00047 
00048 #endif
00049 
00050  /** @class ProjectionParametersZoom
00051      @brief camera parameters which define the mapping between rays in the
00052      camera coordinate system and pixels in the image as well as external pose
00053      @author streckel
00054  */
00055   class BIASGeometry_EXPORT ProjectionParametersZoom:
00056     public  ProjectionParametersBase {
00057   public:
00058 
00059     ProjectionParametersZoom(const unsigned int width = 0,
00060                              const unsigned int height = 0)
00061       : ProjectionParametersBase(width, height)
00062     {
00063       K_.SetIdentity();
00064       invK_.SetIdentity();
00065       zoom_=0;
00066       focallengthZoom_=0;
00067       kc1Zoom_=0; kc2Zoom_=0; kc3Zoom_=0; kc4Zoom_=0;
00068     };
00069 
00070     virtual const bool
00071     DoIntrinsicsDiffer(const ProjectionParametersBase* p) const {
00072       if (p->DoIntrinsicsDiffer((ProjectionParametersBase*)this))
00073         return true;
00074       ProjectionParametersZoom* ppz = (ProjectionParametersZoom*)p;
00075       if (ppz) {
00076         if (kc1Zoom_ != ppz->kc1Zoom_ || kc2Zoom_ != ppz->kc2Zoom_ ||
00077             kc3Zoom_ != ppz->kc3Zoom_ || kc4Zoom_ != ppz->kc4Zoom_ ||
00078             focallengthZoom_ != ppz->focallengthZoom_ || zoom_ != ppz->zoom_)
00079           return true;
00080       }
00081       return false;
00082     }
00083 
00084     ProjectionParametersZoom(const ProjectionParametersZoom & P){
00085       *this = P;
00086       K_ = P.K_;
00087       invK_ = P.invK_;
00088       knownparams_vect_ = P.knownparams_vect_;
00089       zoom_ = P.zoom_;
00090       focallengthZoom_ = P.focallengthZoom_;
00091       kc1Zoom_ = P.kc1Zoom_;
00092       kc2Zoom_ = P.kc2Zoom_;
00093       kc3Zoom_ = P.kc3Zoom_;
00094       kc4Zoom_ = P.kc4Zoom_;
00095     };
00096 
00097     virtual ~ProjectionParametersZoom(){};
00098 
00099     /** @brief calculates the projection of a point
00100         in the camera coordinate system
00101         to a pixel in the image plane of the camera
00102 
00103         In the simplest case perspective pinhole projection x = K * y
00104         where y is the projection of X using  y = (R^T | -R^T C) X
00105     */
00106     virtual HomgPoint2D  ProjectLocal(const Vector3<double>& point,
00107                                       bool IgnoreDistortion = false) const;
00108 
00109     /** @brief calculates the projection of a point in the local camera
00110         coordinate system to a pixel in the image plane of the camera. The
00111         only case when this function may not compute the 2d point is when the
00112         camera center and the 3d point coincide. This case must be indicated
00113         by a negative return value. In all other cases, a 2d point must be
00114         computed, *particularily* when the 3d point is behind the camera
00115         or when the resulting 2d point is at infinity.
00116 
00117         In the simplest case perspective pinhole projection x = K * point
00118         where point is transformed of X using  point = (R^T | -R^T C) X
00119         @author woelk 08/2008 (c) www.vision-n.de */
00120     virtual int ProjectLocal(const Vector3<double>& point, HomgPoint2D &p2d,
00121                              bool IgnoreDistortion = false) const;
00122 
00123      /** @brief map points from image onto unit diameter image plane in 3D.
00124      * Chosen is the image plane with distance of one from the image center.
00125      */
00126     HomgPoint3D UnProjectToImagePlane(const HomgPoint2D& pos,
00127                                       const double& depth = 1.0,
00128                                       bool IgnoreDistortion = false) const;
00129 
00130 
00131     /** @brief calculates the viewing ray from the camera center (in the
00132         camera coordinate system) which belongs to the image position pos
00133         e.g. (0,0,1) means optical axis
00134     */
00135     virtual void UnProjectLocal(const HomgPoint2D& pos, Vector3<double>& pointOnRay,
00136                                 Vector3<double>& direction,
00137                                 bool IgnoreDistortion = false) const;
00138 
00139     /** @brief Using the Matlab camera calibration toolbox parameters
00140         for an undistortion of distorted coordinates.
00141         TODO: faster implementation via Lookup-table
00142         @author streckel 06/2004 */
00143     virtual bool Undistort(BIAS::HomgPoint2D& point2d) const;
00144 
00145     /** @brief Using the Matlab camera calibration toolbox parameters
00146         for a distortion of undistorted coordinates. Inverse to the
00147         undistort function.
00148         TODO: faster implementation via Lookup-table
00149         @author streckel 06/2004 */
00150     virtual bool Distort(BIAS::HomgPoint2D& point2d) const;
00151 
00152     /** @brief covariant virtual copy constructor for use in Projection */
00153     virtual ProjectionParametersZoom* Clone() const {
00154       return new ProjectionParametersZoom(*this);
00155     };
00156 
00157 
00158     inline void Clear(){
00159       knownparams_vect_.clear();
00160     }
00161 
00162     int AddZoomStep(double zoom, double focallength,
00163                     double kc1, double kc2, double kc3, double kc4);
00164 
00165     /* @brief get cached KMatrix
00166      */
00167     inline void GetK(KMatrix& K) const {
00168       K = K_;
00169     };
00170 
00171     /* @brief get cached inverted KMatrix
00172      */
00173     inline void GetKinv(KMatrix& invK) const {
00174       invK = invK_;
00175     };
00176 
00177     /* @brief get the PMatrix composed from the projection object
00178      */
00179     inline virtual BIAS::PMatrix GetP() const {
00180       return BIAS::PMatrix(K_,GetR(),GetC());
00181     }
00182 
00183     /** @brief Get the current camera zoom.
00184         @author buck */
00185     inline int GetZoom(double& zoom) const {
00186         zoom = zoom_;
00187         return 0;
00188     }
00189 
00190     /** @brief Get the current camera focal length.
00191         @author buck */
00192     inline int GetFocalLength(double& f) const {
00193       f = focallengthZoom_;
00194       return 0;
00195     }
00196 
00197     /** @brief Get the current camera Undistortion.
00198         @author buck */
00199     inline void GetUndistortion(double& kc1, double& kc2,
00200                                 double& kc3, double& kc4) const
00201     {
00202       kc1 = kc1Zoom_;
00203       kc2 = kc2Zoom_;
00204       kc3 = kc3Zoom_;
00205       kc4 = kc4Zoom_;
00206     }
00207 
00208 
00209     /** @brief Set CCD cell-size in meter, virtual overload to
00210         recalculate K_.
00211         @author streckel */
00212     virtual inline void SetAspectratio(const double CellSizeX,
00213                                     const double AspectRatio) {
00214       ProjectionParametersBase::SetAspectratio(AspectRatio);
00215       ComputeK_();
00216     }
00217 
00218     /** @brief Set principal point in pixels relative to top left corner,
00219         virtual overload to recalculate K_
00220         @author streckel */
00221     virtual inline void SetPrincipal(const double x, const double y) {
00222       ProjectionParametersBase::SetPrincipal(x, y);
00223       ComputeK_();
00224     }
00225 
00226     /** @brief Set the current camera zoom, the focal length will be
00227         calculated.
00228         @author buck */
00229     inline int SetZoom(const double abszoom) {
00230       zoom_=abszoom;
00231       focallengthZoom_ = InterpolateFocalLength_();
00232       ComputeK_();
00233       return 0;
00234     }
00235 
00236     /** @brief Set the current camera focal length, the zoom will be
00237         calculated.
00238         @author buck */
00239     inline int SetFocalLength(const double f) {
00240       focallengthZoom_=f;
00241       zoom_=InterpolateAbsZoom_();
00242       ComputeK_();
00243       return 0;
00244     }
00245 
00246     /** @brief Set the zoom and focal length of a zoom camera to the
00247         default settings.
00248         @author streckel 06/2004*/
00249     inline void SetUnzoomed() {
00250       if (knownparams_vect_.size()>0) {
00251         SetZoom(knownparams_vect_[0].zoom);
00252       }
00253     }
00254 
00255 
00256     /** @brief adapt internal params to resampled image
00257         @param ratio 2.0 = downsample, 0.5 = upsample
00258     */
00259     virtual inline void Rescale(double ratio, const double offset) {
00260       BIASERR("unimplemented");
00261       BIASABORT;
00262     }
00263 
00264     virtual inline void Rescale(unsigned int width, unsigned int height){
00265       BIASERR("unimplemented");
00266       BIASABORT;
00267     }
00268 
00269 #ifdef BIAS_HAVE_XML2
00270     /** @brief specialization of XML block name function */
00271     virtual int XMLGetClassName(std::string& TopLevelTag,
00272                                 double& Version) const;
00273 
00274     /** @brief specialization of XML write function */
00275     virtual int XMLOut(const xmlNodePtr Node, XMLIO& XMLObject) const;
00276 
00277     /** @brief specialization of XML read function */
00278     virtual int XMLIn(const xmlNodePtr Node, XMLIO& XMLObject);
00279 #endif
00280 
00281     /// friend functions
00282     friend std::ostream& operator<<(std::ostream &os,
00283                                     const ProjectionParametersZoom& p);
00284   protected:
00285     // focal length, aspect ratio, principal point in K_, inverse in invK_
00286     KMatrix K_, invK_;
00287 
00288    // array of camera calibration constants for zoom cameras
00289     std::vector<CPDiscreteParam> knownparams_vect_;
00290 
00291     // calibration settings for the current zoom setting of a zoom camera
00292     // zoom and focallengthZoom are dependend:
00293     // if zoom is set, focallengthZoom will be calculated and vice versa
00294     // focal length is stored in pixel
00295     double zoom_;
00296     double focallengthZoom_;
00297     double kc1Zoom_, kc2Zoom_, kc3Zoom_, kc4Zoom_;
00298 
00299     void InterpolateDistortionFromIndex_(unsigned int i,double relnorm);
00300     double InterpolateFocalLength_(); // w/ given abszoom
00301     double InterpolateAbsZoom_(); // w/ given focal length
00302 
00303     /** @brief computes the cached KMatrix and its inverse
00304         call after every parameter change */
00305     void ComputeK_();
00306 
00307 
00308 
00309   };
00310 
00311   inline std::ostream& operator<<(std::ostream &os,
00312                                   const ProjectionParametersZoom& p)
00313   { 
00314     os << "ProjectionParametersZoom:" << std::endl;
00315     return os;
00316   }
00317 
00318 } // end namespace
00319 
00320 
00321 #include <Base/Common/BIASpragmaEnd.hh>
00322 
00323 #endif
00324 
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends