Commit 70a2c8f5 authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

added conversion operators Mat->vector<T>, Mat->Vec<T,n>, Mat->Matx<T,m,n>

parent 8f33e89d
...@@ -1599,6 +1599,10 @@ public: ...@@ -1599,6 +1599,10 @@ public:
//! converts header to IplImage; no data is copied //! converts header to IplImage; no data is copied
operator IplImage() const; operator IplImage() const;
template<typename _Tp> operator vector<_Tp>() const;
template<typename _Tp, int n> operator Vec<_Tp, n>() const;
template<typename _Tp, int m, int n> operator Matx<_Tp, m, n>() const;
//! returns true iff the matrix data is continuous //! returns true iff the matrix data is continuous
// (i.e. when there are no gaps between successive rows). // (i.e. when there are no gaps between successive rows).
// similar to CV_IS_MAT_CONT(cvmat->type) // similar to CV_IS_MAT_CONT(cvmat->type)
...@@ -2372,6 +2376,10 @@ public: ...@@ -2372,6 +2376,10 @@ public:
//! conversion to vector. //! conversion to vector.
operator vector<_Tp>() const; operator vector<_Tp>() const;
//! conversion to Vec
template<int n> operator Vec<_Tp, n>() const;
//! conversion to Matx
template<int m, int n> operator Matx<_Tp, m, n>() const;
}; };
typedef Mat_<uchar> Mat1b; typedef Mat_<uchar> Mat1b;
......
...@@ -595,7 +595,6 @@ template<typename _Tp> inline MatIterator_<_Tp> Mat::end() ...@@ -595,7 +595,6 @@ template<typename _Tp> inline MatIterator_<_Tp> Mat::end()
return it; return it;
} }
static inline void swap( Mat& a, Mat& b ) static inline void swap( Mat& a, Mat& b )
{ {
std::swap( a.flags, b.flags ); std::swap( a.flags, b.flags );
...@@ -606,6 +605,42 @@ static inline void swap( Mat& a, Mat& b ) ...@@ -606,6 +605,42 @@ static inline void swap( Mat& a, Mat& b )
std::swap( a.refcount, b.refcount ); std::swap( a.refcount, b.refcount );
} }
template<typename _Tp> 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<typename _Tp, int n> 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<typename _Tp, int m, int n> 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() {}
inline SVD::SVD( const Mat& m, int flags ) { operator ()(m, flags); } inline SVD::SVD( const Mat& m, int flags ) { operator ()(m, flags); }
inline void SVD::solveZ( const Mat& m, Mat& dst ) inline void SVD::solveZ( const Mat& m, Mat& dst )
...@@ -829,12 +864,20 @@ template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(Point pt) const ...@@ -829,12 +864,20 @@ template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(Point pt) const
template<typename _Tp> inline Mat_<_Tp>::operator vector<_Tp>() const template<typename _Tp> inline Mat_<_Tp>::operator vector<_Tp>() const
{ {
CV_Assert( rows == 1 || cols == 1 ); return this->Mat::operator vector<_Tp>();
return isContinuous() ? }
vector<_Tp>((_Tp*)data,(_Tp*)data + (rows + cols - 1)) :
(vector<_Tp>)((Mat_<_Tp>)this->t()); template<typename _Tp> template<int n> inline Mat_<_Tp>::operator Vec<_Tp, n>() const
{
return this->Mat::operator Vec<_Tp, n>();
} }
template<typename _Tp> template<int m, int n> inline Mat_<_Tp>::operator Matx<_Tp, m, n>() const
{
return this->Mat::operator Matx<_Tp, m, n>();
}
template<typename T1, typename T2, typename Op> inline void template<typename T1, typename T2, typename Op> inline void
process( const Mat_<T1>& m1, Mat_<T2>& m2, Op op ) process( const Mat_<T1>& m1, Mat_<T2>& m2, Op op )
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment