Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
trv_core.hpp
00001 #ifndef TRV_CORE_HPP
00002 #define TRV_CORE_HPP
00003 
00004 #include "traversability/trv_homography.hpp"
00005 #include "traversability/trv_homographyGeom.hpp"
00006 #include "traversability/trv_homographyFine.hpp"
00007 #include "traversability/trv_homographyMosaic.hpp"
00008 #include "traversability/trv_util.hpp"
00009 #include "traversability/trv_tracker.hpp"
00010 #include "traversability/trv_densificator.hpp"
00011 
00012 #include <image/Image.hpp>
00013 #include "jmath/jblas.hpp"
00014 #include "jmath/ublasExtra.hpp"
00015 #include "kernel/jafarException.hpp"
00016 
00017 // baustelle!!
00018 
00019 namespace jafar {
00020   namespace traversability {
00021     
00031     class Core 
00032     {
00033       public:
00034         
00035         // image buffer
00036         int imBufferCnt;
00037         int imBufferSize;
00038         jafar::image::Image* imBuffer;
00039         
00040         // pose buffer
00041         int poseBufferCnt;
00042         int poseBufferSize;
00043         CameraPoseParameters* poseBuffer;
00044         
00045         // traversability buffer
00046         int trvBufferCnt;
00047         int trvBufferSize;
00048         jafar::image::Image* trvBuffer;
00049         
00050         // homography buffer (for mosaic update)
00051         // store always homographies HC0C1
00052         int homBufferCnt;
00053         int homBufferSize;
00054         jblas::mat33* homBuffer;
00055         
00056         // homography buffer (for geom precalculation)
00057         // store always homographies HC0C1
00058         int preBufferCnt;
00059         int preBufferSize;
00060         jblas::mat33* preBuffer;
00061         
00062         // pointers and indices to actual object
00063         int actIdx0;
00064         int actIdx1;
00065         jafar::image::Image* actIm0_ptr;
00066         jafar::image::Image* actIm1_ptr;
00067         jafar::image::Image* actMsk1_ptr;
00068         CameraPoseParameters* actPose0_ptr;
00069         CameraPoseParameters* actPose1_ptr;
00070         
00071         // intermediate images
00072         jafar::image::Image im0_1_fine;
00073         jafar::image::Image im0_1_geom;
00074         jafar::image::Image mask1_fine;
00075         jafar::image::Image mask1_geom;
00076         
00077         // homography constructing object
00078         HomographyGeom Hgeom;
00079         HomographyFine Hfine;
00080         HomographyMosaic Hmos;
00081         HomographyFine HmosUpdate;
00082         HomographyGeom HpreCalc;
00083         
00084         // specific homographies, g:geom and f:fine
00085         jblas::mat33 H_C0C1g;
00086         jblas::mat33 H_C1C0g;
00087         jblas::mat33 H_C0C1f;
00088         jblas::mat33 H_C1C0f;
00089         
00090         // homographies to apply on images
00091         jblas::mat33 H_C0C1;
00092         jblas::mat33 H_C1C0;
00093         
00094         Tracker* tracker;
00095         
00096         Densificator* densificator;
00097         
00098         // TODO make use of imageInfo object
00099         ImageInfo imageInfo;
00100         
00101         bool optWithGeomPrecalculation;
00102         
00103       public:
00104         Core ( TrackerType ttype, int bufferSize, int imWidth, int imHeight,
00105                int winHalfWidth, int winHalfHeight, bool withGeomPrecalc = false );
00106         ~Core ( void );
00107         
00108         // a bit clumpsy... TODO find better way
00115         void setupGeometry ( const PlaneParameters &plane, const CameraIntrinsicParameters &internals);
00116 //        void setPose0 ( const CameraPoseParameters &camPose0 ) { Hgeom.setPoseCam0(camPose0); }
00117 //        void setPose1 ( const CameraPoseParameters &camPose1 ) { Hgeom.setPoseCam1(camPose1); }
00118 
00119         TrackerType getTrackerType ( void ) const { return tracker->getTrackerType(); }
00120         
00121         // TODO: set idx to default value and then use poseBufferCounter...
00127         void setPose ( const int idx, const CameraPoseParameters &camPose ) {
00128           ++poseBufferCnt;
00129           
00130           poseBuffer[idx%poseBufferSize] = camPose;
00131         }
00138         void setImage ( const int idx, const jafar::image::Image &_im ) {
00139           ++imBufferCnt;
00140           imBuffer[idx%imBufferSize] = _im;
00141         }
00148         void init ( const int idx0, const int idx1 );
00149         
00150         
00151         jafar::image::Image getImage ( int idx ) const { return imBuffer[idx%imBufferSize]; }
00157         jafar::image::Image getTrv ( unsigned int idx=-1 ) const {
00158           if (trvBufferCnt==-1) JFR_WARNING("There is no traversability information available");
00159           return (idx==-1) ? trvBuffer[(trvBufferCnt)%trvBufferSize] : trvBuffer[idx%trvBufferSize]; }
00160         jafar::image::Image getImage0_1_fine ( void ) const { return im0_1_fine; }
00161         jafar::image::Image getImage0_1_geom ( void ) const { return im0_1_geom; }
00162         jafar::image::Image getMask1_fine ( void ) const { return mask1_fine; }
00163         
00164         // obsolete function
00165         MatchesContainer* getMatches ( void ) { return tracker->matches(); }
00166         
00167         // obsolete function, left in order to use the tracking engine to calc matches
00168         // between two images... (only for development purposes)
00169         void calcTracker ( jafar::image::Image &img0, jafar::image::Image &img1 );
00176         void calcTracker ( int step = 1 );
00177         
00182         void calcHgeom ( void );
00189         void calcHfine ( const jafar::image::Image &prevTravMask );
00193         void calcDensify ( void );
00194         void calcH_mosUpdate ( int idx0, int idx1 );
00195         void applyHomography ( const jblas::mat33 &H_C0C1,
00196                                const jafar::image::Image &img0,
00197                 jafar::image::Image &img0_1 );
00198         
00199       private:
00200         void updateHmosBasic ( void );
00201       public:
00205         jblas::mat33 getH_C0Cn( void );
00206         
00207         
00208         /* //////////////////////////////////////////////////////////////////////////////
00209         this function is optimized.
00210         for images of size 512x384 it needs 7ms
00211         for images of size 640x480 it needs 11ms
00212         those values were measured with the method gettimeofday
00213         measurements taken in ruby have shown much higher values
00214         (up to ten times slower!)
00216         template <typename Type> void applyHomographyO( const jblas::mat33 &H_C0C1,
00217             const jafar::image::Image &img0,jafar::image::Image &img0_1,jafar::image::Image maskCFoV1 )
00218         {
00219           int widthIn     = img0.width();
00220           int heightIn    = img0.height();
00221           int widthOut    = img0_1.width();
00222           int heightOut   = img0_1.height();  
00223             
00224           Type p0_x;
00225           Type p0_y;
00226           Type p0_z;
00227           int x0_u; int x0_v;
00228             
00229           Type h01v1;
00230           Type h11v1;
00231           Type h21v1;
00232                 
00233           Type* h00u = new Type[widthOut];
00234           Type* h10u = new Type[widthOut];
00235           Type* h20u = new Type[widthOut];
00236           
00237           uint8_t const* im0ptr   = reinterpret_cast<uint8_t const*>(img0.data());
00238           uint8_t* im0_1ptr = reinterpret_cast<uint8_t*>(img0_1.data());
00239           uint8_t* msk1ptr  = reinterpret_cast<uint8_t*>(maskCFoV1.data());
00240             
00241           for (int j=0;j<heightOut;j++)
00242           {
00243             h01v1 = static_cast<Type>(H_C0C1(0,1))*j+static_cast<Type>(H_C0C1(0,2));
00244             h11v1 = static_cast<Type>(H_C0C1(1,1))*j+static_cast<Type>(H_C0C1(1,2));
00245             h21v1 = static_cast<Type>(H_C0C1(2,1))*j+static_cast<Type>(H_C0C1(2,2));
00246               
00247             for (int i=0;i<widthOut;i++)
00248             {
00249               if (j==0)
00250               {
00251                 h00u[i] = static_cast<Type>(H_C0C1(0,0))*i;
00252                 h10u[i] = static_cast<Type>(H_C0C1(1,0))*i;
00253                 h20u[i] = static_cast<Type>(H_C0C1(2,0))*i;
00254               }
00255                 
00256               p0_x = h00u[i]+h01v1;
00257               p0_y = h10u[i]+h11v1;
00258               p0_z = h20u[i]+h21v1;
00259       
00260               x0_u  = (int)round((p0_x/p0_z));
00261               x0_v  = (int)round((p0_y/p0_z));
00262       
00263               if (x0_u >= 0 and x0_v >= 0 and x0_u < widthIn and x0_v < heightIn)
00264               {
00265                 *(im0_1ptr++) = im0ptr[x0_v*widthIn+x0_u];
00266 //                img0_1.setPixelValue<uint8_t>(img0.getPixelValue<uint8_t>(x0_u,x0_v),i,j);
00267                 
00268                 // change the if clause!!!
00269 //                if (maskCFoV1)
00270                 if (maskCFoV1.data() and x0_u>=5 and x0_v>=5 and x0_u < (widthIn-5) and x0_v < (heightIn-5))
00271                 {
00272                   *(msk1ptr++) = static_cast<uint8_t>(255);
00273 //                  maskCFoV1.setPixelValue<uint8_t>(255,i,j);
00274                 }
00275                 else
00276                 {
00277                   *(msk1ptr++) = static_cast<uint8_t>(0);
00278                 }
00279               }
00280               else
00281               {
00282                 im0_1ptr++;
00283                 *(msk1ptr++) = static_cast<uint8_t>(0);
00284 //                img0_1.setPixelValue<uint8_t>(0,i,j);
00285               }
00286             }
00287           }
00288           
00289           delete [] h00u;
00290           delete [] h10u;
00291           delete [] h20u;
00292         }
00293       
00294     };
00295   } // namespace traversability
00296 } // namespace jafar
00297 
00298 #endif
00299 
00300 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:29 for Jafar by doxygen 1.7.6.1
LAAS-CNRS