| 178 | 
        kaklik | 
        1 | 
        #ifndef __CAMERAPROJECTORCALIBRATION_HH | 
      
      
         | 
         | 
        2 | 
        #define __CAMERAPROJECTORCALIBRATION_HH | 
      
      
         | 
         | 
        3 | 
          | 
      
      
         | 
         | 
        4 | 
        #include <boost/numeric/ublas/matrix.hpp> | 
      
      
         | 
         | 
        5 | 
        #include <boost/numeric/ublas/vector.hpp> | 
      
      
         | 
         | 
        6 | 
        #include <boost/smart_ptr.hpp> | 
      
      
         | 
         | 
        7 | 
        #include "image.h" | 
      
      
         | 
         | 
        8 | 
        #include "rgba.h" | 
      
      
         | 
         | 
        9 | 
        #include <algorithm> | 
      
      
         | 
         | 
        10 | 
        #include <vector> | 
      
      
         | 
         | 
        11 | 
          | 
      
      
         | 
         | 
        12 | 
        /// Class for calibrating the camera-projector system. | 
      
      
         | 
         | 
        13 | 
        class CameraProjectorCalibration | 
      
      
         | 
         | 
        14 | 
        { | 
      
      
         | 
         | 
        15 | 
        public: | 
      
      
         | 
         | 
        16 | 
          /// | 
      
      
         | 
         | 
        17 | 
          typedef boost::numeric::ublas::vector< double > Vector; | 
      
      
         | 
         | 
        18 | 
          /// | 
      
      
         | 
         | 
        19 | 
          typedef boost::numeric::ublas::matrix | 
      
      
         | 
         | 
        20 | 
             < double, boost::numeric::ublas::column_major > Matrix; | 
      
      
         | 
         | 
        21 | 
          /** Constructor. | 
      
      
         | 
         | 
        22 | 
              @param _searchPattern Pattern used in cross-correlation to locate | 
      
      
         | 
         | 
        23 | 
              projected pattern. */ | 
      
      
         | 
         | 
        24 | 
          CameraProjectorCalibration( const mimas::image< unsigned char > &_searchPattern ); | 
      
      
         | 
         | 
        25 | 
          /// | 
      
      
         | 
         | 
        26 | 
          int getNumCalibFrames(void) const { return patternFrame.size(); } | 
      
      
         | 
         | 
        27 | 
          /** Get currently stored background frame. | 
      
      
         | 
         | 
        28 | 
              Camera image of screen without projected pattern. */ | 
      
      
         | 
         | 
        29 | 
          const mimas::image< unsigned char > &getBackgroundFrame(void) const | 
      
      
         | 
         | 
        30 | 
          { return backgroundFrame; } | 
      
      
         | 
         | 
        31 | 
          /// Get ith stored calibration frame. | 
      
      
         | 
         | 
        32 | 
          const mimas::image< unsigned char > &getCalibFrame( int i ) const | 
      
      
         | 
         | 
        33 | 
          { return patternFrame[i]; } | 
      
      
         | 
         | 
        34 | 
          /// | 
      
      
         | 
         | 
        35 | 
          const std::pair< Vector, Vector > &getCalibPointPair( int i ) const | 
      
      
         | 
         | 
        36 | 
          { return pointPairs[i]; } | 
      
      
         | 
         | 
        37 | 
          /// | 
      
      
         | 
         | 
        38 | 
          void setBackgroundFrame( const mimas::image< unsigned char > &img ) | 
      
      
         | 
         | 
        39 | 
          { backgroundFrame = img; } | 
      
      
         | 
         | 
        40 | 
          /** Add a frame for calibration. | 
      
      
         | 
         | 
        41 | 
              The method \c findPatternImg is called to locate the projected pattern | 
      
      
         | 
         | 
        42 | 
        	      in the camera image. \c pointPairsModified is set to \c true . | 
      
      
         | 
         | 
        43 | 
              @param _frame Frame for calibration (camera image). | 
      
      
         | 
         | 
        44 | 
              @param _pos Screen-coordinates of the projected pattern. | 
      
      
         | 
         | 
        45 | 
              @see findPatternImg */ | 
      
      
         | 
         | 
        46 | 
          void addCalibFrame( const mimas::image< unsigned char > &_frame, | 
      
      
         | 
         | 
        47 | 
                              const Vector &_pos ); | 
      
      
         | 
         | 
        48 | 
          /** Compute calibration-matrix. | 
      
      
         | 
         | 
        49 | 
              The homography is computed if a new camera-image was added using | 
      
      
         | 
         | 
        50 | 
              \c addCalibFrame (i.e. \c pointPairsModified is \c true) using the point | 
      
      
         | 
         | 
        51 | 
              pairs acquired so far (at least 5 point-pairs are required). */ | 
      
      
         | 
         | 
        52 | 
          Matrix getHomography(void); | 
      
      
         | 
         | 
        53 | 
        protected: | 
      
      
         | 
         | 
        54 | 
          /** Compute homography. | 
      
      
         | 
         | 
        55 | 
              See <A HREF="http://research.microsoft.com/%7Ezhang/Papers/TR98-71.pdf">paper by Zhengyou Zhang</A> */ | 
      
      
         | 
         | 
        56 | 
          static Matrix genHomography( const std::vector< std::pair< Vector, Vector > > | 
      
      
         | 
         | 
        57 | 
                                       &pointPairs ); | 
      
      
         | 
         | 
        58 | 
          /// Access a part of a boost::multi_array. | 
      
      
         | 
         | 
        59 | 
          static boost::multi_array< double, 2 >::array_view< 2 >::type view | 
      
      
         | 
         | 
        60 | 
            ( boost::multi_array< double, 2 > &in, int x, int y, int w, int h ); | 
      
      
         | 
         | 
        61 | 
          /** Search a pattern in a difference image. | 
      
      
         | 
         | 
        62 | 
              The search is performed by searching the maximum of the | 
      
      
         | 
         | 
        63 | 
              cross-correlation. */ | 
      
      
         | 
         | 
        64 | 
          static Vector findPatternDiffImg( const mimas::image< double > &diffImg, | 
      
      
         | 
         | 
        65 | 
                                            const mimas::image< double > &tpl ); | 
      
      
         | 
         | 
        66 | 
          /** Search pattern in image. | 
      
      
         | 
         | 
        67 | 
              @see findPattern */ | 
      
      
         | 
         | 
        68 | 
          Vector findPatternImg( const mimas::image< unsigned char > &img ); | 
      
      
         | 
         | 
        69 | 
          /// | 
      
      
         | 
         | 
        70 | 
          mimas::image< unsigned char > searchPattern; | 
      
      
         | 
         | 
        71 | 
          /// Background image to subtract from acquired images. | 
      
      
         | 
         | 
        72 | 
          mimas::image< unsigned char > backgroundFrame; | 
      
      
         | 
         | 
        73 | 
          /// The vector of acquired images. | 
      
      
         | 
         | 
        74 | 
          std::vector< mimas::image< unsigned char > > patternFrame; | 
      
      
         | 
         | 
        75 | 
          /// Vector with estimated locations of points. | 
      
      
         | 
         | 
        76 | 
          std::vector< std::pair< Vector, Vector > > pointPairs; | 
      
      
         | 
         | 
        77 | 
          /// Indication wether the homography needs to be recomputed. | 
      
      
         | 
         | 
        78 | 
          bool pointPairsModified; | 
      
      
         | 
         | 
        79 | 
          /// Storing the homography matrix. | 
      
      
         | 
         | 
        80 | 
          Matrix homography; | 
      
      
         | 
         | 
        81 | 
        }; | 
      
      
         | 
         | 
        82 | 
          | 
      
      
         | 
         | 
        83 | 
        /** Smart-pointer. | 
      
      
         | 
         | 
        84 | 
            @see CameraProjectorCalibration */ | 
      
      
         | 
         | 
        85 | 
        typedef boost::shared_ptr< CameraProjectorCalibration > CameraProjectorCalibrationPtr; | 
      
      
         | 
         | 
        86 | 
          | 
      
      
         | 
         | 
        87 | 
        #endif |