Basic Image AlgorithmS Library 2.8.0

CameraParam.hh

00001 /*
00002 $Id: CameraParam.hh,v 2.20 2006/07/31 13:47:40 streckel Exp $
00003 */
00004 /* 
00005 This file is part of the BIAS library (Basic ImageAlgorithmS).
00006 
00007 Copyright (C) 2003-2009    (see file CONTACT for details)
00008   Multimediale Systeme der Informationsverarbeitung
00009   Institut fuer Informatik
00010   Christian-Albrechts-Universitaet Kiel
00011 
00012 
00013 BIAS is free software; you can redistribute it and/or modify
00014 it under the terms of the GNU Lesser General Public License as published by
00015 the Free Software Foundation; either version 2.1 of the License, or
00016 (at your option) any later version.
00017 
00018 BIAS is distributed in the hope that it will be useful,
00019 but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021 GNU Lesser General Public License for more details.
00022 
00023 You should have received a copy of the GNU Lesser General Public License
00024 along with BIAS; if not, write to the Free Software
00025 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026 */
00027 #ifndef __CameraParameter_inc__
00028 #define __CameraParameter_inc__
00029 #include <bias_config.h>
00030 #include <Base/Common/BIASpragmaStart.hh>
00031 #include <Geometry/ProjectionParametersIO.hh>
00032 
00033 #include <vector>
00034 #include <string>
00035 #include <Base/Debug/Error.hh>
00036 #ifdef BIAS_HAVE_XML2
00037 #  include <Base/Common/XMLIO.hh>
00038 #endif
00039 
00040 #include <Base/Math/Vector2.hh>
00041 #include <Base/Geometry/KMatrix.hh>
00042 
00043 #define CAMERA_PARAMETER_CURRENT_XML_VERSION 2.0
00044 
00045 namespace BIAS{
00046 
00047 
00048   class BIASImage_EXPORT CameraParam{
00049 
00050     //    static std::string CameraModelName[];
00051     // static std::string LensModelName[];
00052 
00053   public:
00054     CameraParam();
00055     CameraParam(const CameraParam &cp);
00056     /** @brief pass K and image dims to construct a simple parameter file */
00057     CameraParam(const KMatrix& K, const int w, const int h);
00058 #ifdef BIAS_HAVE_XML2
00059     CameraParam(const std::string &filename);
00060     //    CameraParam(XMLIO &myXML, xmlNodePtr &cameraNode);
00061 #endif //BIAS_HAVE_XML2
00062 
00063     ~CameraParam();
00064 
00065     // @brief default initialization, ends up with _INVALID_ object
00066     void Init();
00067     void Init(const CameraParam &cp);
00068 
00069     void InitPerspective(const std::string CamModel, const int CamID, 
00070                          const std::string LensModel,
00071                          const int imgWidth, const int imgHeight, 
00072                          const double principalX, const double principalY, 
00073                          const double focallength,
00074                          const double aspectratio,
00075                          const double k1, const double k2, 
00076                          const double k3, const double k4,
00077                          const std::vector<double> &IlluCorrX, 
00078                          const std::vector<double> &IlluCorrY);
00079     void InitSpherical(const std::string CamModel, const int CamID, 
00080                        const std::string LensModel,
00081                        const int imgWidth, const int imgHeight, 
00082                        const double principalX, const double principalY, 
00083                        const double radius,
00084                        const std::vector<double> &AngleCorrX,
00085                        const std::vector<double> &AngleCorrY,
00086                        const std::vector<double> &IlluCorrX, 
00087                        const std::vector<double> &IlluCorrY);
00088 
00089 #ifdef BIAS_HAVE_XML2
00090     // @brief initialization with camera data file
00091     int Init(const std::string &filename);
00092     // @brief initialization with camera node from pre-parsed data file
00093     int Init(XMLIO &myXML, xmlNodePtr &cameraNode);
00094 #endif //BIAS_HAVE_
00095 
00096      /** @brief Set to 'true', if camera has a zoom lens.
00097          @author streckel 06/2004*/
00098      void SetZoomCamera(const bool zoomCam);
00099 
00100      /** @brief True, if camera has a zoom lens.
00101          @author streckel 06/2004*/
00102      bool IsZoomCamera() const;
00103 
00104      /** @brief Set the current camera zoom, the focal length will be 
00105          calculated.
00106          @author buck */
00107      void SetZoom(const double abszoom);
00108 
00109      /** @brief Set the current camera focal length, the zoom will be 
00110          calculated.
00111          @author buck */
00112      void SetFocalLength(const double f);
00113 
00114      /** @brief Set the zoom and focal length of a zoom camera to the 
00115          default settings.
00116          @author streckel 06/2004*/
00117      void SetUnzoomed();
00118 
00119      /** @brief Set the current camera zoom, the focal length will be 
00120          calculated.
00121          @author buck */
00122      double GetZoom() const;
00123 
00124      /** @brief Get the current camera focal length.
00125          @author buck */
00126      double GetFocalLength() const;
00127 
00128     /** @brief Set CCD cell-size in meter. 
00129         AspectRatio is CS_Y/CS_X
00130         @author streckel */
00131     void SetCellSize(const double CellSizeX, const double AspectRatio);
00132 
00133     double GetCellSizeX() {return cd_.cellSizeX_;}
00134     double GetCellSizeY() {return cd_.cellSizeY_;}
00135 
00136     /** @brief Set principal point in pixels relative to top left corner
00137         @author streckel */
00138     void SetPrincipal(const double x, const double y);
00139 
00140     /** @brief Set downsampling factor  - 
00141         all get functions will return transformed values
00142 
00143         E.g. if your image size(width and height) is half the original
00144         and the field of view did stay the same, then set factor to 2. <br>
00145         CAUTION: The Set-functions still require the original values
00146         @author streckel */
00147     void SetDownsampling(const double factor);
00148 
00149         double GetDownsamplingFactor();
00150 
00151     /** @brief get camera image width.
00152         @author buck */
00153     unsigned int GetWidth() const; 
00154     /** @brief get camera image height.
00155         @author buck */
00156     unsigned int GetHeight() const;
00157 
00158     inline double GetAspectratio() const {
00159       return cd_.aspectratio_;
00160     }
00161 
00162     /** set size of image seperatly */
00163     void SetSize(unsigned int w,unsigned int h) {
00164       cd_.width_ =w; cd_.height_ = h; }
00165 
00166     /** @brief get principal point, x  in pixels relative to top left corner
00167         @author buck */
00168     double GetPrincipalX() const; 
00169     /** @brief get principal point, y  in pixels relative to top left corner
00170         @author buck */
00171     double GetPrincipalY() const; 
00172 
00173     /* @brief Get the Cameras KMatrix corresponding to the current 
00174        zoom setting
00175        @author streckel */
00176     BIAS::KMatrix GetKMatrix() const;
00177 
00178     inline int GetCamID() const {
00179       return cd_.CamID_;
00180     }
00181 
00182     inline double GetRadius() const{
00183       if (IsSpherical()) {
00184         return cd_. radius_/downsamplingFactor_+0.000000001;
00185       } else {
00186         return -1;
00187       }
00188     }
00189 
00190     inline double SetRadius(double r) {
00191       if (!IsSpherical()) {
00192         BIASERR("Can notset radius for perspective camera");
00193         return -1;
00194       }
00195       cd_.radius_ = r;
00196       // should reinit now
00197       return 0;
00198     }
00199 
00200 
00201     inline void GetDefaultUndistortion(double& f,
00202                                        double& kc1, double& kc2,
00203                                        double& kc3, double& kc4) const 
00204     {
00205       f   = cd_.focallengthDef_/downsamplingFactor_+0.000000001;
00206       kc1 = cd_.kc1Def_;
00207       kc2 = cd_.kc2Def_;
00208       kc3 = cd_.kc3Def_;
00209       kc4 = cd_.kc4Def_;
00210     }
00211 
00212     inline void GetZoomUndistortion(std::vector<CPDiscreteParam>& vp) const {
00213       std::vector<CPDiscreteParam>::const_iterator it;
00214       vp.clear();
00215       vp.reserve(cd_.knownparams_vect_.size());
00216       for (it=cd_.knownparams_vect_.begin();
00217            it!=cd_.knownparams_vect_.end();it++) {
00218         CPDiscreteParam cpp=*it;
00219         cpp.focallength = cpp.focallength/downsamplingFactor_+0.000000001;
00220         vp.push_back(cpp);
00221       }
00222     }
00223 
00224     inline double GetAspectRatio() const {
00225       return cd_.aspectratio_;
00226     }
00227 
00228     inline  bool IsSpherical() const { return cd_.LensIsSpherical_;}
00229 
00230     inline  bool IsPerspective() const {return !cd_.LensIsSpherical_;}  
00231 
00232     inline std::vector<double> GetAngleCorrX() const {
00233       return cd_.AngleCorrX_;
00234     }
00235     inline std::vector<double> GetAngleCorrY() const {
00236       return cd_.AngleCorrY_;
00237     }
00238     inline std::vector<double> GetIlluCorrX() const {
00239       return cd_.IlluCorrX_;
00240     }
00241     inline std::vector<double> GetIlluCorrY() const {
00242       return cd_.IlluCorrY_;
00243     }
00244 
00245 #ifdef BIAS_HAVE_XML2
00246     /** @brief Write all data to a camera data parameter file )
00247         @param Filename Name of the XML-camera-data-file, 
00248         document tag will be to be "Camera"
00249         @author streckel 06/2004 */
00250     int WriteCameraData(const std::string& Filename) const;
00251     /** @brief Add a camera sub-tree-structure to an allready created 
00252         XML-tree and write the data from (*this) into the tree - 
00253         called from the rig parameter class.
00254         @param myXML points to the XML-Tree
00255         @param cameraDataNode points to a "CameraData"-node to append the 
00256         "Camera"-node
00257         @author streckel 06/2004 */
00258     //int AddCameraDataToRig(XMLIO &myXML, xmlNodePtr &cameraDataNode) const;
00259 
00260     /** @brief Read a camera data parameter file and store the data in (*this)
00261         @param Filename Name of the XML-camera-data-file, 
00262         document tag has to be "Camera"
00263         @author streckel 06/2004 */
00264     int ReadCameraData(const std::string& Filename);
00265     /** @brief Read a camera data form a precreated XML-tree and store 
00266         the data in (*this)  - called from the rig parameter class.
00267         @param myXML points to the XML-Tree
00268         @param rigNode points to a "Camera"-node to evaluate
00269         @author streckel 06/2004 */
00270     //int ReadCameraData(XMLIO &myXML, xmlNodePtr &cameraNode);
00271 #endif //BIAS_HAVE_XML2
00272 
00273     // Settings for specific cameras
00274     void SetData_EVID31_147094();
00275     void SetData_OMNITECH_ORIFL_190_3(); 
00276 
00277     /** @brief read jpeg EXIF header and set camera parameter 
00278         @return 0 on success 
00279         @author koeser 07/2005 */
00280     int ReadFromEXIF(const std::string& filename);
00281 
00282     void DebugOut() const;   
00283 
00284     const CameraData GetCameraData() const {
00285       return cd_;
00286     }
00287 
00288     void SetCameraData(const CameraData &cd) {
00289       cd_ = cd;
00290     }
00291 
00292   protected:    
00293     CameraData cd_;
00294 
00295     //0.5 means upsample, 2 means half size, 4 one forth ....
00296     double downsamplingFactor_;
00297     
00298     // calibration settings for the current zoom setting of a zoom camera
00299     // zoom and focallengthZoom are dependend:
00300     // if zoom is set, focallengthZoom will be calculated and vice versa
00301     // focal length is stored in pixel
00302     double zoom_;
00303     double focallengthZoom_;
00304     
00305     double InterpolateFocalLength_(); // w/ given abszoom
00306     double InterpolateAbsZoom_(); // w/ given focal length
00307 
00308 // #ifdef BIAS_HAVE_XML2
00309 //     int ReadCameraDataV0_8(XMLIO &myXML, xmlNodePtr &cameraNode);
00310 //     int ReadCameraDataV1_0(XMLIO &myXML, xmlNodePtr &cameraNode);
00311 //     int AddCameraDataToXMLTree(XMLIO &myXML, xmlNodePtr &rootNode) const;
00312 // #endif //BIAS_HAVE_XML2
00313 
00314   };
00315 
00316 }
00317 
00318 #include <Base/Common/BIASpragmaEnd.hh>
00319 #endif
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends