From 70a2c8f50a261c6a6f4c976055f81acb8d4f578e Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Thu, 29 Jul 2010 06:51:19 +0000 Subject: [PATCH] added conversion operators Mat->vector, Mat->Vec, Mat->Matx --- modules/core/include/opencv2/core/core.hpp | 8 ++++ modules/core/include/opencv2/core/mat.hpp | 55 +++++++++++++++++++--- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/modules/core/include/opencv2/core/core.hpp b/modules/core/include/opencv2/core/core.hpp index 26c1e18c84..5bc8f788be 100644 --- a/modules/core/include/opencv2/core/core.hpp +++ b/modules/core/include/opencv2/core/core.hpp @@ -1599,6 +1599,10 @@ public: //! converts header to IplImage; no data is copied operator IplImage() const; + template operator vector<_Tp>() const; + template operator Vec<_Tp, n>() const; + template operator Matx<_Tp, m, n>() const; + //! returns true iff the matrix data is continuous // (i.e. when there are no gaps between successive rows). // similar to CV_IS_MAT_CONT(cvmat->type) @@ -2372,6 +2376,10 @@ public: //! conversion to vector. operator vector<_Tp>() const; + //! conversion to Vec + template operator Vec<_Tp, n>() const; + //! conversion to Matx + template operator Matx<_Tp, m, n>() const; }; typedef Mat_ Mat1b; diff --git a/modules/core/include/opencv2/core/mat.hpp b/modules/core/include/opencv2/core/mat.hpp index e5635b4ebd..de76e97c36 100644 --- a/modules/core/include/opencv2/core/mat.hpp +++ b/modules/core/include/opencv2/core/mat.hpp @@ -594,8 +594,7 @@ template inline MatIterator_<_Tp> Mat::end() it.ptr = it.sliceEnd = (_Tp*)(data + step*(rows-1)) + cols; return it; } - - + static inline void swap( Mat& a, Mat& b ) { std::swap( a.flags, b.flags ); @@ -605,7 +604,43 @@ static inline void swap( Mat& a, Mat& b ) std::swap( a.dataend, b.dataend ); std::swap( a.refcount, b.refcount ); } + +template inline Mat::operator vector<_Tp>() const +{ + CV_Assert( (rows == 1 || cols == 1) && channels() == DataType<_Tp>::channels ); + + int n = rows + cols - 1; + if( isContinuous() && type() == DataType<_Tp>::type ) + return vector<_Tp>((_Tp*)data,(_Tp*)data + n); + vector<_Tp> v(n); Mat tmp(rows, cols, DataType<_Tp>::type, &v[0]); + convertTo(tmp, tmp.type()); + return v; +} +template inline Mat::operator Vec<_Tp, n>() const +{ + CV_Assert( (rows == 1 || cols == 1) && rows + cols - 1 == n && + channels() == DataType<_Tp>::channels ); + + if( isContinuous() && type() == DataType<_Tp>::type ) + return Vec<_Tp, n>((_Tp*)data); + Vec<_Tp, n> v; Mat tmp(rows, cols, DataType<_Tp>::type, v.val); + convertTo(tmp, tmp.type()); + return v; +} + +template inline Mat::operator Matx<_Tp, m, n>() const +{ + CV_Assert( rows == m && cols == n && + channels() == DataType<_Tp>::channels ); + + if( isContinuous() && type() == DataType<_Tp>::type ) + return Matx<_Tp, m, n>((_Tp*)data); + Matx<_Tp, m, n> mtx; Mat tmp(rows, cols, DataType<_Tp>::type, mtx.val); + convertTo(tmp, tmp.type()); + return mtx; +} + inline SVD::SVD() {} inline SVD::SVD( const Mat& m, int flags ) { operator ()(m, flags); } inline void SVD::solveZ( const Mat& m, Mat& dst ) @@ -829,12 +864,20 @@ template inline const _Tp& Mat_<_Tp>::operator ()(Point pt) const template inline Mat_<_Tp>::operator vector<_Tp>() const { - CV_Assert( rows == 1 || cols == 1 ); - return isContinuous() ? - vector<_Tp>((_Tp*)data,(_Tp*)data + (rows + cols - 1)) : - (vector<_Tp>)((Mat_<_Tp>)this->t()); + return this->Mat::operator vector<_Tp>(); } +template template inline Mat_<_Tp>::operator Vec<_Tp, n>() const +{ + return this->Mat::operator Vec<_Tp, n>(); +} + +template template inline Mat_<_Tp>::operator Matx<_Tp, m, n>() const +{ + return this->Mat::operator Matx<_Tp, m, n>(); +} + + template inline void process( const Mat_& m1, Mat_& m2, Op op ) {