LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiDistanceFromPixel.h

00001 /*
00002  * Copyright (C) 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*--------------------------------------------------------------------
00025  * project ....: LTI-Lib: Image Processing and Computer Vision Library
00026  * file .......: ltiDistanceFromPixel.h
00027  * authors ....: Peter Hosten, Florian Bley
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 30.9.2004
00030  * revisions ..: $Id: ltiDistanceFromPixel.h,v 1.4 2006/02/08 12:17:32 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_DISTANCE_FROM_PIXEL_H_
00034 #define _LTI_DISTANCE_FROM_PIXEL_H_
00035 
00036 #include "ltiImage.h"
00037 #include "ltiVector.h"
00038 #include "ltiMath.h"
00039 #include "ltiFunctor.h"
00040 
00041 namespace lti {
00042   /**
00043    * This class derives the 3D position of an object from a 2D image point.  
00044    * Obviously we have to make some assumptions at first to get this to work.<br>
00045    * First of all we are calculating the distance between a camera positioned at
00046    * an arbitrary height and an object standing on a flat floor.
00047    * We also assume that the optical axis of the camera is parallel to the floor.
00048    * 
00049    * Different parameters of the camera have to be known:
00050    * - the picture size,
00051    * - the focal length d, 
00052    * - the height of the camera and 
00053    * - the camera constants kx/ky, which are necessary to calculate the equivalent 
00054    *   height in cm on the CCD chip if the pixel height is known.<br>
00055    * Note: As standard unit, this documentation uses cm. Of course you can substitute
00056    *       this with any other metric or non-metric unit as long as you subsitute them all.
00057    *
00058    * So here is some example code. The camera axis is not directly aligned
00059    * along the axis on which the distance should be calculated. It is rotated a 
00060    * little bit down to the floor. Thus, we also have to use the class
00061    * lti::coordinateTransformation:
00062    * \code
00063    * lti::coordinateTransformation coordTransform;
00064    * lti::coordinateTransformation::parameters transformParam;
00065    * float b;
00066    * float fDataDisplacement[3] =  { 0, 0, -7};
00067    * lti::vector<float> vDisplace(3,fDataDisplacement);
00068    * 
00069    * std::cout << "y angle of cam (beta):" << std::endl; 
00070    * std::cin >> b;
00071    * transformParam.initParameters(0,b,0,vDisplace);
00072    * coordTransform.setParameters(transformParam);
00073    *    
00074    * lti::DistanceFromPixel distance;
00075    * lti::DistanceFromPixel::parameters distanceParam;      
00076    * lti::vector<int> imgCoordinate(2,0); 
00077    * lti::vector<float> spaceCoordinate(3,0);
00078    * 
00079    * // parameters for the Sony EVI D100P
00080    * distanceParam.fHeight=41.0f;
00081    * distanceParam.iPictureSizeX=379;   // x
00082    * distanceParam.iPictureSizeY=262;   // y
00083    * distanceParam.fD=19.07f;           // focal length in cm
00084    * distanceParam.fKx=0.0555f;         // camera const in x-direction cm/pixel
00085    * distanceParam.fKy=0.0555f;         // camera const in y-direction cm/pixel
00086    * 
00087    * std::cout << "x coordinate of object on floor in pixel (integer) :";
00088    * std::cin  >> imgCoordinate[0];
00089    * std::cout << std::endl;
00090    * std::cout << "y coordinate of object on floor in pixel (integer) :";
00091    * std::cin  >> imgCoordinate[1];
00092    * std::cout << std::endl;
00093    *
00094    * distance.setParameters(distanceParam);
00095    * distance.apply(imgCoordinate,spaceCoordinate);
00096    * std::cout << "3D coordinates of object in camera system: " << spaceCoordinate << std::endl;
00097    *
00098    * float eucliddist=0;
00099    * distance.apply(imgCoordinate,eucliddist);
00100    * std::cout << "Euclidian distance between object and camera: " << eucliddist << std::endl;
00101    *
00102    * std::cout << "Point " << spaceCoordinate << " in the camera coordinate system" << std::endl;
00103    * coordTransform.apply(spaceCoordinate);
00104    * std::cout << "has position " << spaceCoordinate << " in the global coordinate system." << std::endl;
00105    * \endcode
00106    */
00107    
00108    
00109   class DistanceFromPixel : public functor {
00110   public:
00111     /**
00112      * The parameters for the class DistanceFromPixel
00113      */
00114     class parameters : public functor::parameters {
00115     public:
00116       /**
00117        * Default constructor
00118        */
00119       parameters();
00120 
00121       /**
00122        * Copy constructor
00123        * @param other the parameters object to be copied
00124        */
00125       parameters(const parameters& other);
00126 
00127       /**
00128        * Destructor
00129        */
00130       ~parameters();
00131 
00132       /**
00133        * Returns name of this type
00134        */
00135       const char* getTypeName() const;
00136 
00137       /**
00138        * Copy the contents of a parameters object
00139        * @param other the parameters object to be copied
00140        * @return a reference to this parameters object
00141        */
00142       parameters& copy(const parameters& other);
00143 
00144       /**
00145        * Copy the contents of a parameters object
00146        * @param other the parameters object to be copied
00147        * @return a reference to this parameters object
00148        */
00149       parameters& operator=(const parameters& other);
00150 
00151 
00152       /**
00153        * Returns a pointer to a clone of the parameters
00154        */
00155       virtual functor::parameters* clone() const;
00156 
00157       /**
00158        * Write the parameters in the given ioHandler
00159        * @param handler the ioHandler to be used
00160        * @param complete if true (the default) the enclosing begin/end will
00161        *        be also written, otherwise only the data block will be written.
00162        * @return true if write was successful
00163        */
00164       virtual bool write(ioHandler& handler,const bool complete=true) const;
00165 
00166       /**
00167        * Read the parameters from the given ioHandler
00168        * @param handler the ioHandler to be used
00169        * @param complete if true (the default) the enclosing begin/end will
00170        *        be also written, otherwise only the data block will be written.
00171        * @return true if write was successful
00172        */
00173       virtual bool read(ioHandler& handler,const bool complete=true);
00174 
00175 #     ifdef _LTI_MSC_6
00176       /**
00177        * This function is required by MSVC only, as a workaround for a
00178        * very awful bug, which exists since MSVC V.4.0, and still by
00179        * V.6.0 with all bugfixes (so called "service packs") remains
00180        * there...  This method is also public due to another bug, so please
00181        * NEVER EVER call this method directly: use read() instead
00182        */
00183       bool readMS(ioHandler& handler,const bool complete=true);
00184 
00185       /**
00186        * This function is required by MSVC only, as a workaround for a
00187        * very awful bug, which exists since MSVC V.4.0, and still by
00188        * V.6.0 with all bugfixes (so called "service packs") remains
00189        * there...  This method is also public due to another bug, so please
00190        * NEVER EVER call this method directly: use write() instead
00191        */
00192       bool writeMS(ioHandler& handler,const bool complete=true) const;
00193 #     endif
00194       
00195       // 
00196      
00197       // ------------------------------------------------
00198       // the parameters
00199       // ------------------------------------------------
00200     
00201 
00202       /**
00203        * Height of camera over the ground (cm)
00204        */
00205       float fHeight;
00206       /**
00207        * Focal length (cm)
00208        */
00209       
00210       float fD;       
00211       /**
00212        * Camera const in x-direction (cm/pixel)
00213        */
00214       float fKx;      
00215       /**
00216        * Camera const in y-direction (cm/pixel)
00217        */
00218       float fKy;
00219       /**
00220        * Size of the picture in x (pixel)
00221        */
00222       int iPictureSizeX;//      
00223       /**
00224        * Size of the picture in y (pixel)
00225        */
00226       int iPictureSizeY;     
00227     };
00228 
00229     /**
00230      * Default constructor
00231      */
00232     DistanceFromPixel();
00233 
00234     /**
00235      * Construct a functor using the given parameters
00236      */
00237     DistanceFromPixel(const parameters& par);
00238 
00239     /**
00240      * Copy constructor
00241      * @param other the object to be copied
00242      */
00243     DistanceFromPixel(const DistanceFromPixel& other);
00244 
00245     /**
00246      * Destructor
00247      */
00248     virtual ~DistanceFromPixel();
00249 
00250     /**
00251      * Returns the name of this type ("DistanceFromPixel")
00252      */
00253     virtual const char* getTypeName() const;
00254 
00255     
00256     /**
00257      * This method calculates the corresponding 3D space point from an 2D image point 
00258      * The requirements needed for this to work 
00259      * are described above in the detailed description of this class.  
00260      * @param src  lti::vector<int> src(2,0)
00261      * @param dest lti::vector<int> dest(3,0)
00262      * @return true if apply successful or false otherwise.
00263      */
00264     bool apply(const vector<int>& src,vector<float>& dest) const;
00265 
00266     /**
00267      * This method calculates the euclidian distance between the 3D space position point 
00268      * of an image point and the camera. The requirements needed for this to work 
00269      * are described above in the detailed description of this class.
00270      * @param src  lti::vector<int> src(2,0)
00271      * @param dest float dest
00272      * @return true if apply successful or false otherwise.
00273      */
00274     bool apply(const vector<int>& src,float& dest) const;
00275 
00276 
00277     /**
00278      * Copy data of "other" functor.
00279      * @param other the functor to be copied
00280      * @return a reference to this functor object
00281      */
00282     DistanceFromPixel& copy(const DistanceFromPixel& other);
00283 
00284     /**
00285      * Alias for copy member
00286      * @param other the functor to be copied
00287      * @return a reference to this functor object
00288      */
00289     DistanceFromPixel& operator=(const DistanceFromPixel& other);
00290 
00291     /**
00292      * Returns a pointer to a clone of this functor.
00293      */
00294     virtual functor* clone() const;
00295 
00296     /**
00297      * Returns used parameters
00298      */
00299     const parameters& getParameters() const;
00300  
00301  private:
00302    
00303    float DeltaY(const int SizeY,const int PointY, const float Ky) const; // Returns relative pixeldistance between the 
00304    float DeltaX(const int SizeX,const int PointX,const float Kx) const; // middle of the picture and the object's picturepoint   
00305    float CalcZDistance(const float relPointY,const float Height, const float D) const  ;
00306    float CalcXDistance(const float relPointX,const float ZDistance, const float D) const;
00307    
00308    
00309    float EuclidDistance(const float XDistance,const float Height,const float ZDistance) const ;
00310   };
00311 }
00312 
00313 #endif

Generated on Sat Apr 10 15:25:22 2010 for LTI-Lib by Doxygen 1.6.1