00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00110 #ifndef UBLASCVMATRIX_HXX
00111 #define UBLASCVMATRIX_HXX
00112
00113
00114 #include <boost/numeric/ublas/matrix.hpp>
00115 #include <boost/numeric/ublas/vector.hpp>
00116 #include <boost/numeric/ublas/lu.hpp>
00117 #include <boost/numeric/ublas/io.hpp>
00118
00119 #include "cv.h"
00120
00121 template<class PIXELTYPE>class CCvMatDepth {public: int depth;CCvMatDepth(){depth= sizeof(PIXELTYPE);}};
00122 template<>class CCvMatDepth<unsigned char> {public: int depth;CCvMatDepth(){depth= CV_8U ;}};
00123 template<>class CCvMatDepth<char> {public: int depth;CCvMatDepth(){depth= CV_8S;}};
00124 template<>class CCvMatDepth<unsigned short> {public: int depth;CCvMatDepth(){depth= CV_16U;}};
00125 template<>class CCvMatDepth<short> {public: int depth;CCvMatDepth(){depth= CV_16S;}};
00126 template<>class CCvMatDepth<int> {public: int depth;CCvMatDepth(){depth= CV_32S;}};
00127 template<>class CCvMatDepth<float> {public: int depth;CCvMatDepth(){depth= CV_32F;}};
00128 template<>class CCvMatDepth<double> {public: int depth;CCvMatDepth(){depth= CV_64F;}};
00129
00130
00131
00132
00133
00134
00135
00182 template < class T,unsigned int Ni,unsigned int Nj >
00183 class CublasCvMatrix: public boost::numeric::ublas::c_matrix<T,Ni,Nj>
00184 , public CvMat
00185 {
00186 public:
00187 typedef boost::numeric::ublas::c_matrix<T,Ni,Nj> ublas_c_matrix_type;
00188 CCvMatDepth<T> cvMatDepth;
00189 ublas_c_matrix_type *const p_ublas_matrix ;
00190 CvMat *const pCvMat;
00191
00192 void initializeData()
00193 {
00194 pCvMat->data.ptr =reinterpret_cast<uchar*>(&(*p_ublas_matrix)(0,0));
00195 cvInitMatHeader( pCvMat,Nj, Ni
00196 ,cvMatDepth.depth
00197 , reinterpret_cast<uchar*>(&(*p_ublas_matrix)(0,0))
00198 , sizeof(T)*Nj);
00199 }
00200 CublasCvMatrix():p_ublas_matrix(this),pCvMat(this)
00201 {
00202 initializeData();
00203 }
00204 CublasCvMatrix(const CublasCvMatrix<T,Ni,Nj> &inputMatrix ):p_ublas_matrix(this),pCvMat(this)
00205 {
00206 initializeData();
00207 *p_ublas_matrix = ( ublas_c_matrix_type)inputMatrix;
00208 }
00209 CublasCvMatrix(const ublas_c_matrix_type &inputMatrix ):p_ublas_matrix(this),pCvMat(this)
00210 {
00211 initializeData();
00212 *p_ublas_matrix = ( ublas_c_matrix_type)inputMatrix;
00213 }
00214 CublasCvMatrix<T,Ni,Nj>& operator=(ublas_c_matrix_type inputMatrix)
00215 {
00216 *p_ublas_matrix = inputMatrix;
00217 return *this;
00218 }
00219 CublasCvMatrix<T,Ni,Nj>& operator=(CvMat inputMatrix)
00220 {
00221 T * pValue = reinterpret_cast<T*>(inputMatrix.data.ptr);
00222 std::copy(pValue ,pValue +Nj*Nj,p_ublas_matrix->data());
00223 return *this;
00224 }
00225
00226 CublasCvMatrix<T,Ni,Nj>& operator=(CublasCvMatrix<T,Ni,Nj> inputMatrix)
00227 {
00228 *p_ublas_matrix = inputMatrix;
00229 return *this;
00230 }
00231
00232 };
00233
00234
00235
00236
00237
00238
00239
00240
00290 template < class T,unsigned int N>
00291 class CublasCvVector: public boost::numeric::ublas::c_vector<T,N>
00292 , public CvMat
00293 {
00294 public:
00295 typedef boost::numeric::ublas::c_vector<T,N> ublas_c_vector_type;
00296 CCvMatDepth<T> cvMatDepth;
00297 CvMat *const pCvMat;
00298 ublas_c_vector_type *const p_ublas_vector;
00299
00300 void initializeData()
00301 {
00302 pCvMat->data.ptr =reinterpret_cast<uchar*>( p_ublas_vector->data() );
00303 cvInitMatHeader( pCvMat
00304 ,N , 1
00305 ,cvMatDepth.depth
00306 , reinterpret_cast<uchar*>(p_ublas_vector->data())
00307 , sizeof(T));
00308 }
00309 CublasCvVector():p_ublas_vector(this),pCvMat(this)
00310 {
00311 initializeData();
00312 *p_ublas_vector = boost::numeric::ublas::zero_vector<T>(N);
00313 }
00314 CublasCvVector( const CublasCvVector<T,N> &inputVector):p_ublas_vector(this),pCvMat(this)
00315 {
00316 initializeData();
00317 *p_ublas_vector = (ublas_c_vector_type)inputVector;
00318 }
00319 CublasCvVector( const ublas_c_vector_type &inputVector):p_ublas_vector(this),pCvMat(this)
00320 {
00321 initializeData();
00322 *p_ublas_vector = (ublas_c_vector_type)inputVector;
00323 }
00324
00325 CublasCvVector<T,N>& operator=(ublas_c_vector_type inputVector)
00326 {
00327 *p_ublas_vector = inputVector;
00328 return *this;
00329 }
00330 CublasCvVector<T,N>& operator=(CvMat inputMatrix)
00331 {
00332 T * pValue = reinterpret_cast<T*>(inputMatrix.data.ptr);
00333 std::copy(pValue ,pValue +Nj*Nj,p_ublas_vector->data());
00334 return *this;
00335 }
00336 CublasCvVector<T,N>& operator=(CublasCvVector<T,N> inputVector)
00337 {
00338 *p_ublas_vector = inputVector ;
00339
00340 return *this;
00341 }
00342 };
00343
00344 template < class T,unsigned int N>
00345 inverseUblasCvMatrix(CublasCvMatrix<T,N,N> &matInput, CublasCvMatrix<T,N,N> &matOut){
00346 cvmInvert( &(CvMat)matInput, &(CvMat)matOut );
00347 }
00348
00349 template < class T,unsigned int N>
00350 struct inverseUblasCvMatrixFunctor{
00351 CublasCvMatrix<T,N,N> operator ()(CublasCvMatrix<T,N,N> &matInput)
00352 {
00353 CublasCvMatrix<T,N,N> matOut;
00354 inverseUblasCvMatrix<T,N>(matInput, matOut){
00355 return matOut;
00356 }
00357 }
00358 };
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00426 template < class WrapMatrixClass = boost::numeric::ublas::matrix<float> >
00427 class CWrapCvMat: public WrapMatrixClass
00428 , public CvMat
00429 {
00430 public:
00431 typedef WrapMatrixClass wrap_matrix_type;
00432 typedef typename WrapMatrixClass::value_type value_type;
00433
00434 CCvMatDepth< typename WrapMatrixClass::value_type > cvMatDepth;
00435 wrap_matrix_type *const p_wrap_matrix ;
00436 CvMat *const pCvMat;
00437
00438 void initializeData()
00439 {
00440 pCvMat->data.ptr =reinterpret_cast<uchar*>(&(*p_wrap_matrix)[0,0]);
00441 cvInitMatHeader( pCvMat,p_wrap_matrix->size1(), p_wrap_matrix->size2()
00442 ,cvMatDepth.depth
00443 , reinterpret_cast<uchar*>(&(*p_wrap_matrix)[0,0])
00444 , sizeof(value_type)*p_wrap_matrix->size2());
00445 }
00446 #if 0
00447 CWrapCvMat():p_wrap_matrix(this),pCvMat(this)
00448 {
00449 initializeData();
00450 }
00451 #endif
00452 CWrapCvMat(int n1, int n2):WrapMatrixClass(n1,n2), p_wrap_matrix(this),pCvMat(this)
00453 {
00454 initializeData();
00455 }
00456 CWrapCvMat(const CWrapCvMat<WrapMatrixClass> &inputMatrix ):WrapMatrixClass(inputMatrix),p_wrap_matrix(this),pCvMat(this)
00457 {
00458 initializeData();
00459 }
00460 CWrapCvMat(const WrapMatrixClass &inputMatrix ):WrapMatrixClass(inputMatrix),p_wrap_matrix(this),pCvMat(this)
00461 {
00462 initializeData();
00463 }
00464 CWrapCvMat<WrapMatrixClass>& operator=(wrap_matrix_type inputMatrix)
00465 {
00466 *p_wrap_matrix = inputMatrix;
00467 return *this;
00468 }
00469 CWrapCvMat<WrapMatrixClass>& operator=(CvMat inputMatrix)
00470 {
00471 T * pValue = reinterpret_cast<value_type*>(inputMatrix.data.ptr);
00472 std::copy(pValue ,pValue +p_wrap_matrix->size1()*p_wrap_matrix->size2()
00473 ,p_wrap_matrix->data());
00474 return *this;
00475 }
00476 CWrapCvMat<WrapMatrixClass>& operator=(CWrapCvMat<WrapMatrixClass> inputMatrix)
00477 {
00478
00479 *p_wrap_matrix = inputMatrix;
00480 return *this;
00481 }
00482 };
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532 #endif