ublascvmatrix.hxx

Go to the documentation of this file.
00001 /************************************************************************/
00002 /*                                                                      */
00003 /*               Copyright 2004-2005 by Hirotaka Niitsuma               */
00004 /*                                                                      */
00005 /*                                                                      */
00006 /*    This file is part of the STLLCV computer vision library.          */
00007 /*    ( Version 0.1, Sep 28 2005 )                                      */
00008 /*    You may use, modify, and distribute this software according       */
00009 /*    to the terms stated in the LICENSE file included in               */
00010 /*    the STLLCV distribution.                                          */
00011 /*                                                                      */
00012 /*    The STLLCV Website is                                             */
00013 /*        http://www2s.biglobe.ne.jp/~niitsuma/STLLCV/                  */
00014 /*    Please direct questions, bug reports, and contributions to        */
00015 /*        niitsuma@mub.biglobe.ne.jp@@@                              */
00016 /*                                                                      */
00017 /*  THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY EXPRESS OR          */
00018 /*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED      */
00019 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
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 /*                    CublasCvMatrix                   */
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 // CvMat::data and c_matrix::data()  conflicted
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 ; //,publas;
00190         CvMat *const  pCvMat; //,pCv ;  
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 /*                   CublasCvVector                     */
00238 /*                                                      */
00239 /********************************************************/
00240 
00290 template < class T,unsigned int N>
00291 class  CublasCvVector: public boost::numeric::ublas::c_vector<T,N>  
00292                                         , public CvMat // CvMat::data and c_matrix::data()  conflicted
00293 {
00294 public:
00295         typedef boost::numeric::ublas::c_vector<T,N> ublas_c_vector_type;
00296         CCvMatDepth<T> cvMatDepth;
00297         CvMat *const pCvMat;//,*pCv; 
00298         ublas_c_vector_type *const p_ublas_vector;//,*publas;
00299         
00300         void initializeData()
00301         {
00302                 pCvMat->data.ptr =reinterpret_cast<uchar*>( p_ublas_vector->data() );
00303                 cvInitMatHeader( pCvMat
00304                         ,N , 1 //collum vector
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                         //reinterpret_cast<ublas_c_vector_type >(inputVector);
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 //The following  codes are only test code
00363 
00364 /********************************************************/
00365 /*                                                      */
00366 /*                    CWrapCvMat                        */
00367 /*                                                      */
00368 /********************************************************/
00369 
00426 template < class WrapMatrixClass = boost::numeric::ublas::matrix<float> >
00427 class  CWrapCvMat: public WrapMatrixClass
00428                                         , public CvMat // CvMat::data and c_matrix::data()  conflicted
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 ; //,publas;
00436         CvMat *const  pCvMat; //,pCv ;  
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                 //*p_wrap_matrix = reinterpret_cast<wrap_matrix_type>(inputMatrix);
00479                 *p_wrap_matrix = inputMatrix;
00480         return *this;
00481     }
00482 };
00483 
00484 
00485 
00486 /*
00487 template < class WrapMatrixClass = boost::numeric::ublas::matrix<float > >
00488 class  CCvMatWrapped: public WrapMatrixClass
00489                                         , public CvMat // CvMat::data and c_matrix::data()  conflicted
00490 {
00491 public:
00492         typedef WrapMatrixClass wrap_matrix_type;
00493         typedef typename WrapMatrixClass::value_type value_type;
00494 
00495         CCvMatDepth< typename WrapMatrixClass::value_type > cvMatDepth;
00496         wrap_matrix_type *const p_wrap_matrix_type ; //,publas;
00497         CvMat *const  pCvMat; //,pCv ;  
00498 
00499         char * initializeData(int n1,int n2)
00500         {
00501                 //pCvMat->data.ptr =reinterpret_cast<uchar*>(&(*p_wrap_matrix_type)(0,0));
00502                 cvInitMatHeader( 
00503                                 //pCvMat
00504                                 (CvMat)this
00505                                 ,Nj, Ni
00506                ,cvMatDepth.depth
00507                            //, reinterpret_cast<uchar*>(&(*p_wrap_matrix_type)(0,0))
00508                            ((CvMat)this)->data.ptr
00509                            , sizeof(T)*Nj);
00510                 cvCreateData( 
00511                         //pCvMat 
00512                         ((CvMat)this)
00513                         );
00514                 //return pCvMat->data.ptr
00515                 return ((CvMat)this)->data.ptr
00516         }
00517         CCvMatWrapped(int n1,int n2):
00518                 CvMat()
00519                 ,WrapMatrixClass(n1,n2
00520                 //,reinterpret_cast< 
00521                         //typename WrapMatrixClass::value_type *
00522                 //      typename WrapMatrixClass::array_type
00523                 >(initializeData(n1,n2)) )
00524                 ,pCvMat(this)
00525                 ,p_wrap_matrix_type(this)
00526         {}
00527 };
00528 */
00529 
00530 
00531 
00532 #endif /*  UBLASCVMATRIX_HXX */

Generated on Thu Oct 27 21:27:43 2005 for STL like OpenCV wrapper (STLLCV) by  doxygen 1.4.5