00001 #ifndef TRV_UTIL_HPP
00002 #define TRV_UTIL_HPP
00003
00004 #include <image/Image.hpp>
00005 #include "kernel/jafarException.hpp"
00006 #include "jmath/jblas.hpp"
00007 #include "jmath/ublasExtra.hpp"
00008 #include <boost/numeric/ublas/matrix.hpp>
00009 #include <boost/numeric/ublas/io.hpp>
00010 #include <boost/numeric/ublas/matrix_proxy.hpp>
00011 #include <boost/numeric/ublas/operation.hpp>
00012 #include <boost/numeric/bindings/lapack/driver/gesdd.hpp>
00013
00014
00015
00016 #include "traversability/trv_rotations.hpp"
00017
00018 #include "fdetect/DetectionResult.hpp"
00019 #include "gfm/MatchingResult.hpp"
00020 #include "fdetect/InterestFeature.hpp"
00021
00022 # include "klt/klt.hpp"
00023
00024
00025 namespace jafar
00026 {
00027 namespace traversability
00028 {
00039 struct CameraPoseParameters
00040 {
00041
00042 CameraPoseParameters( double _x = 0, double _y = 0, double _z = 0,
00043 double _psi = 0, double _theta = 0, double _phi = 0 ) :
00044 x(_x), y(_y), z(_z), psi(_psi), theta(_theta), phi(_phi) {}
00045
00046
00047 double x;
00048 double y;
00049 double z;
00050 double psi;
00051 double theta;
00052 double phi;
00053 };
00054
00056
00061 struct CameraIntrinsicParameters
00062 {
00063
00071 CameraIntrinsicParameters( double _au = 0, double _av = 0,
00072 double _u0 = 0, double _v0 = 0 ) :
00073 alpha_u(_au), alpha_v(_av), u0(_u0), v0(_v0) {
00074 double karr[9] = { alpha_u, 0.0, u0 , 0.0, alpha_v, v0, 0.0, 0.0, 1.0 };
00075 jafar::jmath::ublasExtra::setMatrixValue(K,karr,3,3);
00076 jmath::ublasExtra::inv(K,Kinv);
00077 }
00078
00079
00080 double alpha_u;
00081 double alpha_v;
00082 double u0;
00083 double v0;
00084 jblas::mat33 K;
00085 jblas::mat33 Kinv;
00086 };
00087
00089
00097 struct PlaneParameters
00098 {
00099 PlaneParameters( double _d = 1.0, double _nx = 0.0, double _ny = 0.0, double _nz = 1.0) :
00100 d(_d), nx(_nx), ny(_ny), nz(_nz) {
00101 double narr[3] = {nx,ny,nz};
00102 jafar::jmath::ublasExtra::setVectorValue(n_W,narr,3);
00103 }
00104 double d;
00105 double nx;
00106 double ny;
00107 double nz;
00108 jblas::vec3 n_W;
00109 };
00110
00112
00116 struct CameraParameters
00117 {
00118 CameraParameters(CameraPoseParameters _pose = CameraPoseParameters(),
00119 CameraIntrinsicParameters _internals = CameraIntrinsicParameters(),
00120 PlaneParameters _plane = PlaneParameters(1.0)) :
00121 pose(_pose), internals(_internals), plane(_plane) {}
00122
00123
00124 CameraPoseParameters pose;
00125
00126
00127 CameraIntrinsicParameters internals;
00128
00129
00130 PlaneParameters plane;
00131
00132
00133
00134
00135
00136
00137 };
00138
00140
00145 struct CameraDeltaParameters
00146 {
00147 CameraDeltaParameters(CameraParameters _cam0 = CameraParameters(),
00148 CameraParameters _cam1 = CameraParameters()) :
00149 cam0(_cam0), cam1(_cam1) {}
00150
00151
00152 CameraParameters cam0;
00153 CameraParameters cam1;
00154
00155
00156 jblas::vec3 rC0_W;
00157 jblas::vec3 rC1_W;
00158 jblas::vec3 tC1C0_W;
00159
00160 jblas::vec4 qC0C1_b;
00161 jblas::vec4 qC0C1;
00162 jblas::vec4 qWC0;
00163 jblas::vec4 qWC1;
00164
00165 jblas::mat33 R_C0C1;
00166 jblas::mat33 R_C1C0;
00167 jblas::mat33 R_WC0;
00168 jblas::mat33 R_C0W;
00169 jblas::mat33 R_C1W;
00170 jblas::mat33 R_WC1;
00171 };
00172
00174
00179 struct FileInfoDataSet
00180 {
00181 FileInfoDataSet( ) :
00182 pathPos(""), pathImg(""), stemImage(""), stemPositionData(""), fileType(""),
00183 nOfDigits(0), startIdx(0), stopIdx(0) {}
00195 FileInfoDataSet( std::string _pathPos, std::string _pathImg,
00196 std::string _stemImage, std::string _stemPositionData, std::string _fileType,
00197 int _nOfDigits, int _startIdx, int _stopIdx ) :
00198 pathPos(_pathPos), pathImg(_pathImg), stemImage(_stemImage), stemPositionData(_stemPositionData),
00199 fileType(_fileType), nOfDigits(_nOfDigits), startIdx(_startIdx), stopIdx(_stopIdx) {}
00200
00201
00202 std::string pathPos;
00203 std::string pathImg;
00204 std::string stemImage;
00205 std::string stemPositionData;
00206 std::string fileType;
00207 int nOfDigits;
00208 int startIdx;
00209 int stopIdx;
00210 };
00211
00213
00219 struct ImageInfo
00220 {
00228 ImageInfo (int _width = 512, int _height = 384, int _depth = CV_8U,
00229 JfrImage_TypeColorSpace _colorSpace = JfrImage_CS_GRAY ) :
00230 width(_width), height(_height), depth(_depth), colorSpace(_colorSpace) {}
00231 int width;
00232 int height;
00233 int depth;
00234 JfrImage_TypeColorSpace colorSpace;
00235 };
00236
00238
00244 class Quaternion
00245 {
00246 private:
00247 jblas::vec4 mq;
00248
00249 public:
00253 Quaternion();
00259 Quaternion( jblas::mat33 R );
00267 Quaternion( jblas::vec3 axis, double phi );
00268 Quaternion( double psi, double theta, double phi );
00269 Quaternion( double q0, double q1, double q2, double q3 );
00270
00271
00272
00273
00274 };
00275
00277
00282 struct MatchesInfo
00283 {
00284 MatchesInfo ( jblas::vec2 _ctr_ref = jblas::zero_vec(2),
00285 jblas::vec2 _ctr_match = jblas::zero_vec(2),
00286 double _md_ref = 0, double _md_match = 0, int _n=0 ):
00287 centroid_ref(_ctr_ref), centroid_match(_ctr_match),
00288 meanDist_ref(_md_ref), meanDist_match(_md_match), nOfMatches(_n) {};
00289 jblas::vec2 centroid_ref;
00290 jblas::vec2 centroid_match;
00291 double meanDist_ref, meanDist_match;
00292 int nOfMatches;
00293 };
00294
00296
00304 class MatchesContainer
00305 {
00306 private:
00307 jblas::mat mPoints_ref;
00308 jblas::mat mPoints_match;
00309 jblas::veci mIdentifier;
00310 jblas::veci mPartition;
00311 jblas::vec mWeight;
00312 MatchesInfo mInfo;
00313 bool mIsNormalized;
00314 bool mIsStaticallyNormalized;
00315 public:
00319 MatchesContainer ();
00331 MatchesContainer ( const jafar::gfm::MatchingResult &rm, int width=-1, int height=-1 );
00332 MatchesContainer ( MatchesContainer* mcBuffer[], int idx0, int idx1, int bufferSize,
00333 int width=-1, int height=-1 );
00334
00343 MatchesContainer ( const KLT_FeatureTable &ft, int idx0, int idx1, int width=-1, int height=-1 );
00351 MatchesContainer ( jblas::mat &points_ref, jblas::mat &points_mat, int width=-1, int height=-1 );
00352
00357 MatchesContainer ( const MatchesContainer &mc );
00358
00359 ~MatchesContainer ( void ) {};
00360 public:
00364 void printInfo ( void );
00372 void normalize ( void );
00376 void denormalize ( void );
00383 jblas::mat33 denormalizeHom ( jblas::mat33 H01 );
00387 jblas::mat* points_ref( void ) { return &mPoints_ref; }
00391 jblas::mat* points_match( void ) { return &mPoints_match; }
00397 double point_ref_u( int idx ) const { return mPoints_ref(0,idx); }
00403 double point_ref_v( int idx ) const { return mPoints_ref(1,idx); }
00409 double point_match_u( int idx ) const { return mPoints_match(0,idx); }
00415 double point_match_v( int idx ) const { return mPoints_match(1,idx); }
00419 int nOfMatches( void ) const { return mInfo.nOfMatches; }
00425 int id ( int idx ) const { return mIdentifier(idx); }
00429 bool isNormalized ( void ) const { return mIsNormalized; }
00430
00431
00432 void setWeight ( const double weight, const int idx ) { mWeight(idx) = weight; }
00433 void setPartition ( const int partition, const int idx ) { mPartition(idx) = partition; }
00434 double getWeight ( const int idx ) const { return mWeight(idx); }
00435 int getPartition ( const int idx ) { return mPartition(idx); }
00436 jblas::vec* weights ( void ) { return &mWeight; }
00437
00438 private:
00442 void calcInfo ( int width=-1, int height=-1 );
00443 int countTrackedFeatures( KLT_FeatureTable ft, int idx0, int idx1);
00444 };
00445
00446
00447
00448
00449 inline int mod ( int a, int b ) { return ( (a%b) >= 0) ? a%b : b+a%b; }
00453 template <class Vec> void printVec ( const Vec &vec )
00454 {
00455 using namespace std;
00456
00457 int n = vec.size();
00458
00459 for (int i=0;i<n;i++)
00460 {
00461 cout << vec(i) << "\t";
00462 }
00463 cout << endl;
00464 }
00465
00469 template <class Mat> void printMat ( const Mat &mat )
00470 {
00471 using namespace std;
00472
00473 int m = mat.size1();
00474 int n = mat.size2();
00475
00476 for (int i=0;i<m;i++)
00477 {
00478 for (int j=0;j<n;j++)
00479 {
00480 cout << mat(i,j) << "\t";
00481 }
00482 cout << endl;
00483 }
00484 }
00485
00486
00487
00488
00489
00490
00491
00493
00495
00496 class HomMat
00497 {
00498 private:
00499 jblas::mat33 mH;
00500
00501 public:
00502 HomMat();
00503 HomMat( jblas::mat33 H );
00504 void set ();
00505 void set ( jblas::mat33 H );
00506 void set ( const double *H );
00507 void get ( jblas::mat33 &H );
00508 jblas::mat33 get ( void );
00509 jblas::mat33 getInv ( void );
00510 void set_ij ( double val, int idx_i, int idx_j );
00511 double get_ij ( int idx_i, int idx_j );
00512 void clear ( void );
00513 void print ( void );
00514 };
00515
00516 class Ellipse
00517 {
00518 private:
00519 jblas::mat *mPoints;
00520 jafar::image::Image *mMask;
00521
00522 private:
00523 void setLine( jblas::mat *points, int counter, int line, int x1, int x2, int width, int height );
00524 void calcIntersections();
00525 void calcEllipseConditionForWin( jblas::mat *ellPoints, jblas::vec2 center, int winX, int winY,
00526 double A, double B, double C, double k2 );
00527 void calcEllipseConditionForWinLbL ( jblas::mat *ellPoints, jblas::vec2 center, int winX, int winY,
00528 double A, double B, double C, double F );
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552 public:
00553 Ellipse( void );
00554 void set( const jblas::vec2 ¢er, const jblas::vec2 &ax1, const double &sigma1,
00555 const jblas::vec2 &ax2, const double &sigma2 );
00556 void set1( const jblas::vec2 ¢er, const jblas::mat22 &Cov, bool DEBUG_FLAG = false );
00557 jblas::mat* get( void );
00558 void setMask( const jblas::mat *points, const int &width, const int &height );
00559 jafar::image::Image* getMask( void );
00560 void draw( jafar::image::Image *img );
00561 };
00562 }
00563 }
00564
00565 #endif