diff --git a/modules/contrib/src/quadsubpix.cpp b/modules/contrib/src/quadsubpix.cpp index 46f6cde400..d995bf773b 100644 --- a/modules/contrib/src/quadsubpix.cpp +++ b/modules/contrib/src/quadsubpix.cpp @@ -89,16 +89,17 @@ bool is_smaller(const std::pair& p1, const std::pair& p2 void orderContours(const vector >& contours, Point2f point, vector >& order) { order.clear(); - int i, j, n = (int)contours.size(); + size_t i, j, n = contours.size(); for(i = 0; i < n; i++) { + size_t ni = contours[i].size(); double min_dist = std::numeric_limits::max(); - for(j = 0; j < n; j++) + for(j = 0; j < ni; j++) { double dist = norm(Point2f((float)contours[i][j].x, (float)contours[i][j].y) - point); min_dist = MIN(min_dist, dist); } - order.push_back(std::pair(i, (float)min_dist)); + order.push_back(std::pair((int)i, (float)min_dist)); } std::sort(order.begin(), order.end(), is_smaller); diff --git a/modules/core/include/opencv2/core/core.hpp b/modules/core/include/opencv2/core/core.hpp index 48ced29af7..456b46aa0b 100644 --- a/modules/core/include/opencv2/core/core.hpp +++ b/modules/core/include/opencv2/core/core.hpp @@ -399,104 +399,6 @@ template<> class DataDepth { public: enum { value = CV_32F, fmt=(int)'f' template<> class DataDepth { public: enum { value = CV_64F, fmt=(int)'d' }; }; template class DataDepth<_Tp*> { public: enum { value = CV_USRTYPE1, fmt=(int)'r' }; }; -/*! - A short numerical vector. - - This template class represents short numerical vectors (of 1, 2, 3, 4 ... elements) - on which you can perform basic arithmetical operations, access individual elements using [] operator etc. - The vectors are allocated on stack, as opposite to std::valarray, std::vector, cv::Mat etc., - which elements are dynamically allocated in the heap. - - The template takes 2 parameters: - -# _Tp element type - -# cn the number of elements - - In addition to the universal notation like Vec, you can use shorter aliases - for the most popular specialized variants of Vec, e.g. Vec3f ~ Vec. -*/ -template class CV_EXPORTS Vec -{ -public: - typedef _Tp value_type; - enum { depth = DataDepth<_Tp>::value, channels = cn, type = CV_MAKETYPE(depth, channels) }; - - //! default constructor - Vec(); - - Vec(_Tp v0); //!< 1-element vector constructor - Vec(_Tp v0, _Tp v1); //!< 2-element vector constructor - Vec(_Tp v0, _Tp v1, _Tp v2); //!< 3-element vector constructor - Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3); //!< 4-element vector constructor - Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4); //!< 5-element vector constructor - Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5); //!< 6-element vector constructor - Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6); //!< 7-element vector constructor - Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7); //!< 8-element vector constructor - Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8); //!< 9-element vector constructor - Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9); //!< 10-element vector constructor - explicit Vec(const _Tp* values); - - Vec(const Vec<_Tp, cn>& v); - static Vec all(_Tp alpha); - //! dot product - _Tp dot(const Vec& v) const; - //! dot product computed in double-precision arithmetics - double ddot(const Vec& v) const; - //! per-element multiplication - Vec mul(const Vec<_Tp, cn>& v) const; - - /*! - cross product of the two 3D vectors. - - For other dimensionalities the exception is raised - */ - Vec cross(const Vec& v) const; - //! convertion to another data type - template operator Vec() const; - //! conversion to 4-element CvScalar. - operator CvScalar() const; - - Matx<_Tp, 1, cn> t() const; - - /*! element access */ - const _Tp& operator [](int i) const; - _Tp& operator[](int i); - const _Tp& operator ()(int i) const; - _Tp& operator ()(int i); - - _Tp val[cn]; //< vector elements -}; - - -/* \typedef - - Shorter aliases for the most popular specializations of Vec -*/ -typedef Vec Vec2b; -typedef Vec Vec3b; -typedef Vec Vec4b; - -typedef Vec Vec2s; -typedef Vec Vec3s; -typedef Vec Vec4s; - -typedef Vec Vec2w; -typedef Vec Vec3w; -typedef Vec Vec4w; - -typedef Vec Vec2i; -typedef Vec Vec3i; -typedef Vec Vec4i; - -typedef Vec Vec2f; -typedef Vec Vec3f; -typedef Vec Vec4f; -typedef Vec Vec6f; - -typedef Vec Vec2d; -typedef Vec Vec3d; -typedef Vec Vec4d; -typedef Vec Vec6d; - ////////////////////////////// Small Matrix /////////////////////////// @@ -523,12 +425,11 @@ struct CV_EXPORTS Matx_MulOp {}; struct CV_EXPORTS Matx_MatMulOp {}; struct CV_EXPORTS Matx_TOp {}; -template class CV_EXPORTS Matx : public Vec<_Tp, m*n> +template class CV_EXPORTS Matx { public: typedef _Tp value_type; - typedef Vec<_Tp, m*n> base_type; - typedef Vec<_Tp, MIN(m, n)> diag_type; + typedef Matx<_Tp, MIN(m, n), 1> diag_type; typedef Matx<_Tp, m, n> mat_type; enum { depth = DataDepth<_Tp>::value, rows = m, cols = n, channels = rows*cols, type = CV_MAKETYPE(depth, channels) }; @@ -555,15 +456,20 @@ public: _Tp v12, _Tp v13, _Tp v14, _Tp v15); //!< 1x16, 4x4 or 16x1 matrix explicit Matx(const _Tp* vals); //!< initialize from a plain array - Matx(const base_type& v); static Matx all(_Tp alpha); static Matx zeros(); static Matx ones(); static Matx eye(); - static Matx diag(const Vec<_Tp, MIN(m,n)>& d); + static Matx diag(const diag_type& d); static Matx randu(_Tp a, _Tp b); static Matx randn(_Tp a, _Tp b); + //! dot product computed with the default precision + _Tp dot(const Matx<_Tp, m, n>& v) const; + + //! dot product computed in double-precision arithmetics + double ddot(const Matx<_Tp, m, n>& v) const; + //! convertion to another data type template operator Matx() const; @@ -577,10 +483,10 @@ public: Matx<_Tp, 1, n> row(int i) const; //! extract the matrix column - Vec<_Tp, m> col(int i) const; + Matx<_Tp, m, 1> col(int i) const; //! extract the matrix diagonal - Vec<_Tp, MIN(m,n)> diag() const; + Matx<_Tp, MIN(m,n), 1> diag() const; //! transpose the matrix Matx<_Tp, n, m> t() const; @@ -590,7 +496,7 @@ public: //! solve linear system template Matx<_Tp, n, l> solve(const Matx<_Tp, m, l>& rhs, int flags=DECOMP_LU) const; - Vec<_Tp, n> solve(const Vec<_Tp, m>& rhs, int method) const; + Matx<_Tp, n, 1> solve(const Matx<_Tp, m, 1>& rhs, int method) const; //! multiply two matrices element-wise Matx<_Tp, m, n> mul(const Matx<_Tp, m, n>& a) const; @@ -609,6 +515,8 @@ public: Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp); template Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_MatMulOp); Matx(const Matx<_Tp, n, m>& a, Matx_TOp); + + _Tp val[m*n]; //< matrix elements }; @@ -649,7 +557,100 @@ typedef Matx Matx44f; typedef Matx Matx44d; typedef Matx Matx66f; typedef Matx Matx66d; + + +/*! + A short numerical vector. + + This template class represents short numerical vectors (of 1, 2, 3, 4 ... elements) + on which you can perform basic arithmetical operations, access individual elements using [] operator etc. + The vectors are allocated on stack, as opposite to std::valarray, std::vector, cv::Mat etc., + which elements are dynamically allocated in the heap. + + The template takes 2 parameters: + -# _Tp element type + -# cn the number of elements + + In addition to the universal notation like Vec, you can use shorter aliases + for the most popular specialized variants of Vec, e.g. Vec3f ~ Vec. +*/ +template class CV_EXPORTS Vec : public Matx<_Tp, cn, 1> +{ +public: + typedef _Tp value_type; + enum { depth = DataDepth<_Tp>::value, channels = cn, type = CV_MAKETYPE(depth, channels) }; + //! default constructor + Vec(); + + Vec(_Tp v0); //!< 1-element vector constructor + Vec(_Tp v0, _Tp v1); //!< 2-element vector constructor + Vec(_Tp v0, _Tp v1, _Tp v2); //!< 3-element vector constructor + Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3); //!< 4-element vector constructor + Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4); //!< 5-element vector constructor + Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5); //!< 6-element vector constructor + Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6); //!< 7-element vector constructor + Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7); //!< 8-element vector constructor + Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8); //!< 9-element vector constructor + Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9); //!< 10-element vector constructor + explicit Vec(const _Tp* values); + + Vec(const Vec<_Tp, cn>& v); + static Vec all(_Tp alpha); + + //! per-element multiplication + Vec mul(const Vec<_Tp, cn>& v) const; + + /*! + cross product of the two 3D vectors. + + For other dimensionalities the exception is raised + */ + Vec cross(const Vec& v) const; + //! convertion to another data type + template operator Vec() const; + //! conversion to 4-element CvScalar. + operator CvScalar() const; + + /*! element access */ + const _Tp& operator [](int i) const; + _Tp& operator[](int i); + const _Tp& operator ()(int i) const; + _Tp& operator ()(int i); +}; + + +/* \typedef + + Shorter aliases for the most popular specializations of Vec +*/ +typedef Vec Vec2b; +typedef Vec Vec3b; +typedef Vec Vec4b; + +typedef Vec Vec2s; +typedef Vec Vec3s; +typedef Vec Vec4s; + +typedef Vec Vec2w; +typedef Vec Vec3w; +typedef Vec Vec4w; + +typedef Vec Vec2i; +typedef Vec Vec3i; +typedef Vec Vec4i; + +typedef Vec Vec2f; +typedef Vec Vec3f; +typedef Vec Vec4f; +typedef Vec Vec6f; + +typedef Vec Vec2d; +typedef Vec Vec3d; +typedef Vec Vec4d; +typedef Vec Vec6d; + + //////////////////////////////// Complex ////////////////////////////// /*! @@ -918,8 +919,6 @@ public: //! per-element product Scalar_<_Tp> mul(const Scalar_<_Tp>& t, double scale=1 ) const; - //! another helper conversion method. \see cvScalarToRawData - template void convertTo(T2* buf, int channels, int unroll_to=0) const; // returns (v0, -v1, -v2, -v3) Scalar_<_Tp> conj() const; @@ -930,6 +929,8 @@ public: typedef Scalar_ Scalar; +CV_EXPORTS void scalarToRawData(const Scalar& s, void* buf, int type, int unroll_to=0); + //////////////////////////////// Range ///////////////////////////////// /*! @@ -2784,28 +2785,25 @@ protected: }; -template class CV_EXPORTS VecCommaInitializer -{ -public: - VecCommaInitializer(Vec<_Tp, n>* _vec); - template VecCommaInitializer<_Tp, n>& operator , (T2 val); - Vec<_Tp, n> operator *() const; - - Vec<_Tp, n>* vec; - int idx; -}; - - -template class CV_EXPORTS MatxCommaInitializer : - public VecCommaInitializer<_Tp, m*n> +template class CV_EXPORTS MatxCommaInitializer { public: MatxCommaInitializer(Matx<_Tp, m, n>* _mtx); template MatxCommaInitializer<_Tp, m, n>& operator , (T2 val); Matx<_Tp, m, n> operator *() const; + + Matx<_Tp, m, n>* dst; + int idx; }; - - + +template class CV_EXPORTS VecCommaInitializer : public MatxCommaInitializer<_Tp, m, 1> +{ +public: + VecCommaInitializer(Vec<_Tp, m>* _vec); + template VecCommaInitializer<_Tp, m>& operator , (T2 val); + Vec<_Tp, m> operator *() const; +}; + /*! Automatically Allocated Buffer Class diff --git a/modules/core/include/opencv2/core/mat.hpp b/modules/core/include/opencv2/core/mat.hpp index da05e17f18..5d18dd01d7 100644 --- a/modules/core/include/opencv2/core/mat.hpp +++ b/modules/core/include/opencv2/core/mat.hpp @@ -1873,15 +1873,15 @@ static inline MatConstIterator operator - (const MatConstIterator& a, ptrdiff_t template static inline MatConstIterator_<_Tp> operator + (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs) -{ return (MatConstIterator_<_Tp>&)((const MatConstIterator&)a + ofs); } +{ MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatConstIterator_<_Tp>&)t; } template static inline MatConstIterator_<_Tp> operator + (ptrdiff_t ofs, const MatConstIterator_<_Tp>& a) -{ return (MatConstIterator_<_Tp>&)((const MatConstIterator&)a + ofs); } +{ MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatConstIterator_<_Tp>&)t; } template static inline MatConstIterator_<_Tp> operator - (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs) -{ return (MatConstIterator_<_Tp>&)((const MatConstIterator&)a - ofs); } +{ MatConstIterator t = (const MatConstIterator&)a - ofs; return (MatConstIterator_<_Tp>&)t; } inline uchar* MatConstIterator::operator [](ptrdiff_t i) const { return *(*this + i); } @@ -1891,15 +1891,15 @@ template inline _Tp MatConstIterator_<_Tp>::operator [](ptrdiff_t template static inline MatIterator_<_Tp> operator + (const MatIterator_<_Tp>& a, ptrdiff_t ofs) -{ return (MatIterator_<_Tp>&)((const MatConstIterator&)a + ofs); } +{ MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatIterator_<_Tp>&)t; } template static inline MatIterator_<_Tp> operator + (ptrdiff_t ofs, const MatIterator_<_Tp>& a) -{ return (MatIterator_<_Tp>&)((const MatConstIterator&)a + ofs); } +{ MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatIterator_<_Tp>&)t; } template static inline MatIterator_<_Tp> operator - (const MatIterator_<_Tp>& a, ptrdiff_t ofs) -{ return (MatIterator_<_Tp>&)((const MatConstIterator&)a - ofs); } +{ MatConstIterator t = (const MatConstIterator&)a - ofs; return (MatIterator_<_Tp>&)t; } template inline _Tp& MatIterator_<_Tp>::operator [](ptrdiff_t i) const { return *(*this + i); } diff --git a/modules/core/include/opencv2/core/operations.hpp b/modules/core/include/opencv2/core/operations.hpp index aa40f9eb73..a0db6ce68b 100644 --- a/modules/core/include/opencv2/core/operations.hpp +++ b/modules/core/include/opencv2/core/operations.hpp @@ -196,470 +196,107 @@ template<> inline unsigned saturate_cast(float v){ return cvRound(v); template<> inline unsigned saturate_cast(double v) { return cvRound(v); } -/////////////////////////// short vector (Vec) ///////////////////////////// +//////////////////////////////// Matx ///////////////////////////////// -template inline Vec<_Tp, cn>::Vec() + +template inline Matx<_Tp, m, n>::Matx() { - for(int i = 0; i < cn; i++) val[i] = _Tp(0); + for(int i = 0; i < channels; i++) val[i] = _Tp(0); } -template inline Vec<_Tp, cn>::Vec(_Tp v0) +template inline Matx<_Tp, m, n>::Matx(_Tp v0) { val[0] = v0; - for(int i = 1; i < cn; i++) val[i] = _Tp(0); + for(int i = 1; i < channels; i++) val[i] = _Tp(0); } -template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1) +template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1) { - assert(cn >= 2); + assert(channels >= 2); val[0] = v0; val[1] = v1; - for(int i = 2; i < cn; i++) val[i] = _Tp(0); + for(int i = 2; i < channels; i++) val[i] = _Tp(0); } -template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2) +template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2) { - assert(cn >= 3); + assert(channels >= 3); val[0] = v0; val[1] = v1; val[2] = v2; - for(int i = 3; i < cn; i++) val[i] = _Tp(0); + for(int i = 3; i < channels; i++) val[i] = _Tp(0); } -template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3) +template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3) { - assert(cn >= 4); + assert(channels >= 4); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; - for(int i = 4; i < cn; i++) val[i] = _Tp(0); + for(int i = 4; i < channels; i++) val[i] = _Tp(0); } -template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4) +template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4) { - assert(cn >= 5); + assert(channels >= 5); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4; - for(int i = 5; i < cn; i++) val[i] = _Tp(0); + for(int i = 5; i < channels; i++) val[i] = _Tp(0); } -template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, +template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5) { - assert(cn >= 6); + assert(channels >= 6); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4; val[5] = v5; - for(int i = 6; i < cn; i++) val[i] = _Tp(0); + for(int i = 6; i < channels; i++) val[i] = _Tp(0); } -template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, +template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6) { - assert(cn >= 7); + assert(channels >= 7); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4; val[5] = v5; val[6] = v6; - for(int i = 7; i < cn; i++) val[i] = _Tp(0); + for(int i = 7; i < channels; i++) val[i] = _Tp(0); } -template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, +template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7) { - assert(cn >= 8); + assert(channels >= 8); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7; - for(int i = 8; i < cn; i++) val[i] = _Tp(0); + for(int i = 8; i < channels; i++) val[i] = _Tp(0); } -template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, +template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8) { - assert(cn >= 9); + assert(channels >= 9); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7; val[8] = v8; - for(int i = 9; i < cn; i++) val[i] = _Tp(0); + for(int i = 9; i < channels; i++) val[i] = _Tp(0); } -template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, +template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9) { - assert(cn >= 10); + assert(channels >= 10); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7; val[8] = v8; val[9] = v9; - for(int i = 10; i < cn; i++) val[i] = _Tp(0); + for(int i = 10; i < channels; i++) val[i] = _Tp(0); } -template inline Vec<_Tp, cn>::Vec(const _Tp* values) -{ - for( int i = 0; i < cn; i++ ) val[i] = values[i]; -} - - -template inline Vec<_Tp, cn>::Vec(const Vec<_Tp, cn>& v) -{ - for( int i = 0; i < cn; i++ ) val[i] = v.val[i]; -} - -template inline Vec<_Tp, cn> Vec<_Tp, cn>::all(_Tp alpha) -{ - Vec v; - for( int i = 0; i < cn; i++ ) v.val[i] = alpha; - return v; -} - -template inline _Tp Vec<_Tp, cn>::dot(const Vec<_Tp, cn>& v) const -{ - _Tp s = 0; - for( int i = 0; i < cn; i++ ) s += val[i]*v.val[i]; - return s; -} - - -template inline double Vec<_Tp, cn>::ddot(const Vec<_Tp, cn>& v) const -{ - double s = 0; - for( int i = 0; i < cn; i++ ) s += (double)val[i]*v.val[i]; - return s; -} - - -template inline Vec<_Tp, cn> Vec<_Tp, cn>::mul(const Vec<_Tp, cn>& v) const -{ - Vec<_Tp, cn> w; - for( int i = 0; i < cn; i++ ) w.val[i] = saturate_cast<_Tp>(val[i]*v.val[i]); - return w; -} - - -template inline Vec<_Tp, cn> Vec<_Tp, cn>::cross(const Vec<_Tp, cn>& v) const -{ - CV_Error(CV_StsError, "for arbitrary-size vector there is no cross-product defined"); - return Vec<_Tp, cn>(); -} - - -template inline Matx<_Tp, 1, cn> Vec<_Tp, cn>::t() const -{ - return (const Matx<_Tp, 1, cn>&)*this; -} - - -template template -inline Vec<_Tp, cn>::operator Vec() const -{ - Vec v; - for( int i = 0; i < cn; i++ ) v.val[i] = saturate_cast(val[i]); - return v; -} - -template inline Vec<_Tp, cn>::operator CvScalar() const -{ - CvScalar s = {{0,0,0,0}}; - int i; - for( i = 0; i < std::min(cn, 4); i++ ) s.val[i] = val[i]; - for( ; i < 4; i++ ) s.val[i] = 0; - return s; -} - -template inline const _Tp& Vec<_Tp, cn>::operator [](int i) const -{ - CV_DbgAssert( (unsigned)i < (unsigned)cn ); - return val[i]; -} - -template inline _Tp& Vec<_Tp, cn>::operator [](int i) -{ - CV_DbgAssert( (unsigned)i < (unsigned)cn ); - return val[i]; -} - -template inline const _Tp& Vec<_Tp, cn>::operator ()(int i) const -{ - CV_DbgAssert( (unsigned)i < (unsigned)cn ); - return val[i]; -} - -template inline _Tp& Vec<_Tp, cn>::operator ()(int i) -{ - CV_DbgAssert( (unsigned)i < (unsigned)cn ); - return val[i]; -} - -template static inline Vec<_Tp1, cn>& -operator += (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b) -{ - for( int i = 0; i < cn; i++ ) - a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]); - return a; -} - -template static inline Vec<_Tp1, cn>& -operator -= (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b) -{ - for( int i = 0; i < cn; i++ ) - a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]); - return a; -} - -template static inline Vec<_Tp, cn> -operator + (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b) -{ - Vec<_Tp, cn> c = a; - return c += b; -} - -template static inline Vec<_Tp, cn> -operator - (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b) -{ - Vec<_Tp, cn> c = a; - return c -= b; -} - -template static inline -Vec<_Tp, 2>& operator *= (Vec<_Tp, 2>& a, _Tp alpha) -{ - a[0] *= alpha; a[1] *= alpha; - return a; -} - -template static inline -Vec<_Tp, 3>& operator *= (Vec<_Tp, 3>& a, _Tp alpha) -{ - a[0] *= alpha; a[1] *= alpha; a[2] *= alpha; - return a; -} - -template static inline -Vec<_Tp, 4>& operator *= (Vec<_Tp, 4>& a, _Tp alpha) -{ - a[0] *= alpha; a[1] *= alpha; a[2] *= alpha; a[3] *= alpha; - return a; -} - -template static inline Vec<_Tp, cn> -operator * (const Vec<_Tp, cn>& a, _Tp alpha) -{ - Vec<_Tp, cn> c = a; - return c *= alpha; -} - -template static inline Vec<_Tp, cn> -operator * (_Tp alpha, const Vec<_Tp, cn>& a) -{ - return a * alpha; -} - - -template static inline Vec<_Tp, 4> -operator * (const Vec<_Tp, 4>& a, const Vec<_Tp, 4>& b) -{ - return Vec<_Tp, 4>(saturate_cast<_Tp>(a[0]*b[0] - a[1]*b[1] - a[2]*b[2] - a[3]*b[3]), - saturate_cast<_Tp>(a[0]*b[1] + a[1]*b[0] + a[2]*b[3] - a[3]*b[2]), - saturate_cast<_Tp>(a[0]*b[2] - a[1]*b[3] + a[2]*b[0] - a[3]*b[1]), - saturate_cast<_Tp>(a[0]*b[3] + a[1]*b[2] - a[2]*b[1] - a[3]*b[0])); -} - - -template static inline Vec<_Tp, 4>& -operator *= (Vec<_Tp, 4>& a, const Vec<_Tp, 4>& b) -{ - a = a*b; - return a; -} - - -template static inline Vec<_Tp, cn> -operator - (const Vec<_Tp, cn>& a) -{ - Vec<_Tp,cn> t; - for( int i = 0; i < cn; i++ ) t.val[i] = saturate_cast<_Tp>(-a.val[i]); - return t; -} - -template<> inline Vec Vec::cross(const Vec& v) const -{ - return Vec(val[1]*v.val[2] - val[2]*v.val[1], - val[2]*v.val[0] - val[0]*v.val[2], - val[0]*v.val[1] - val[1]*v.val[0]); -} - -template<> inline Vec Vec::cross(const Vec& v) const -{ - return Vec(val[1]*v.val[2] - val[2]*v.val[1], - val[2]*v.val[0] - val[0]*v.val[2], - val[0]*v.val[1] - val[1]*v.val[0]); -} - -template static inline -Vec& operator += (Vec& a, const Vec& b) -{ - a[0] = saturate_cast(a[0] + b[0]); - a[1] = saturate_cast(a[1] + b[1]); - return a; -} - -template static inline -Vec& operator += (Vec& a, const Vec& b) -{ - a[0] = saturate_cast(a[0] + b[0]); - a[1] = saturate_cast(a[1] + b[1]); - a[2] = saturate_cast(a[2] + b[2]); - return a; -} - - -template static inline -Vec& operator += (Vec& a, const Vec& b) -{ - a[0] = saturate_cast(a[0] + b[0]); - a[1] = saturate_cast(a[1] + b[1]); - a[2] = saturate_cast(a[2] + b[2]); - a[3] = saturate_cast(a[3] + b[3]); - return a; -} - - -template static inline -double norm(const Vec& a) -{ - double s = 0; - for( int i = 0; i < n; i++ ) - s += (double)a.val[i]*a.val[i]; - return std::sqrt(s); -} - - -template static inline -double norm(const Vec& a, int normType) -{ - if( normType == NORM_INF ) - { - T1 s = 0; - for( int i = 0; i < n; i++ ) - s = std::max(s, std::abs(a.val[i])); - return s; - } - - if( normType == NORM_L1 ) - { - T1 s = 0; - for( int i = 0; i < n; i++ ) - s += std::abs(a.val[i]); - return s; - } - - CV_DbgAssert( normType == NORM_L2 ); - return norm(a); -} - - -template static inline -bool operator == (const Vec& a, const Vec& b) -{ - for( int i = 0; i < n; i++ ) - if( a[i] != b[i] ) return false; - return true; -} - -template static inline -bool operator != (const Vec& a, const Vec& b) -{ - return !(a == b); -} - -template static inline -VecCommaInitializer<_Tp, n> operator << (const Vec<_Tp, n>& vec, _T2 val) -{ - VecCommaInitializer<_Tp, n> commaInitializer((Vec<_Tp, n>*)&vec); - return (commaInitializer, val); -} - -template inline -VecCommaInitializer<_Tp, n>::VecCommaInitializer(Vec<_Tp, n>* _vec) - : vec(_vec), idx(0) -{} - -template template inline -VecCommaInitializer<_Tp, n>& VecCommaInitializer<_Tp, n>::operator , (_T2 value) -{ - CV_DbgAssert( idx < n ); - vec->val[idx++] = saturate_cast<_Tp>(value); - return *this; -} - -template inline -Vec<_Tp, n> VecCommaInitializer<_Tp, n>::operator *() const -{ - CV_DbgAssert( idx == n ); - return *vec; -} - -//////////////////////////////// Matx ///////////////////////////////// - - -template Matx<_Tp,m,n>::Matx() -{} - -template -inline Matx<_Tp,m,n>::Matx(_Tp v0) -: Matx<_Tp,m,n>::base_type(v0) -{} - -template -inline Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1) -: Matx<_Tp,m,n>::base_type(v0, v1) -{} - -template -inline Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2) -: Matx<_Tp,m,n>::base_type(v0, v1, v2) -{} - -template Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3) -: Matx<_Tp,m,n>::base_type(v0, v1, v2, v3) -{} - -template -inline Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4) -: Matx<_Tp,m,n>::base_type(v0, v1, v2, v3, v4) -{} - -template -inline Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5) -: Matx<_Tp,m,n>::base_type(v0, v1, v2, v3, v4, v5) -{} - -template -inline Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6) -: Matx<_Tp,m,n>::base_type(v0, v1, v2, v3, v4, v5, v6) -{} - -template -inline Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, - _Tp v4, _Tp v5, _Tp v6, _Tp v7) -: Matx<_Tp,m,n>::base_type(v0, v1, v2, v3, v4, v5, v6, v7) -{} - -template -inline Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, - _Tp v3, _Tp v4, _Tp v5, - _Tp v6, _Tp v7, _Tp v8) -: Matx<_Tp,m,n>::base_type(v0, v1, v2, v3, v4, v5, v6, v7, v8) -{} - -template -inline Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, - _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9) -: Matx<_Tp,m,n>::base_type(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) -{} - template inline Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11) { - assert(m*n == 12); - this->val[0] = v0; this->val[1] = v1; this->val[2] = v2; this->val[3] = v3; - this->val[4] = v4; this->val[5] = v5; this->val[6] = v6; this->val[7] = v7; - this->val[8] = v8; this->val[9] = v9; this->val[10] = v10; this->val[11] = v11; + assert(channels == 12); + val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; + val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7; + val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11; } template @@ -668,31 +305,23 @@ inline Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v8, _Tp v9, _Tp v10, _Tp v11, _Tp v12, _Tp v13, _Tp v14, _Tp v15) { - assert(m*n == 16); - this->val[0] = v0; this->val[1] = v1; this->val[2] = v2; this->val[3] = v3; - this->val[4] = v4; this->val[5] = v5; this->val[6] = v6; this->val[7] = v7; - this->val[8] = v8; this->val[9] = v9; this->val[10] = v10; this->val[11] = v11; - this->val[12] = v12; this->val[13] = v13; this->val[14] = v14; this->val[15] = v15; + assert(channels == 16); + val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; + val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7; + val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11; + val[12] = v12; val[13] = v13; val[14] = v14; val[15] = v15; } - - -template -inline Matx<_Tp,m,n>::Matx(const _Tp* vals) -: Matx<_Tp,m,n>::base_type(vals) -{} - -template - inline Matx<_Tp,m,n>::Matx(const Vec<_Tp,m*n>& v) -: base_type(v) +template inline Matx<_Tp, m, n>::Matx(const _Tp* values) { + for( int i = 0; i < channels; i++ ) val[i] = values[i]; } - -template inline -Matx<_Tp,m,n> Matx<_Tp,m,n>::all(_Tp alpha) + +template inline Matx<_Tp, m, n> Matx<_Tp, m, n>::all(_Tp alpha) { - base_type v = base_type::all(alpha); - return (Matx<_Tp,m,n>&)v; + Matx<_Tp, m, n> M; + for( int i = 0; i < m*n; i++ ) M.val[i] = alpha; + return M; } template inline @@ -716,13 +345,30 @@ Matx<_Tp,m,n> Matx<_Tp,m,n>::eye() return M; } +template inline _Tp Matx<_Tp, m, n>::dot(const Matx<_Tp, m, n>& M) const +{ + _Tp s = 0; + for( int i = 0; i < m*n; i++ ) s += val[i]*M.val[i]; + return s; +} + + +template inline double Matx<_Tp, m, n>::ddot(const Matx<_Tp, m, n>& M) const +{ + double s = 0; + for( int i = 0; i < m*n; i++ ) s += (double)val[i]*M.val[i]; + return s; +} + + template inline -Matx<_Tp,m,n> Matx<_Tp,m,n>::diag(const Vec<_Tp,MIN(m,n)>& d) +Matx<_Tp,m,n> Matx<_Tp,m,n>::diag(const Matx<_Tp,MIN(m,n),1>& d) { Matx<_Tp,m,n> M; for(int i = 0; i < MIN(m,n); i++) M(i,i) = d[i]; + return M; } template inline @@ -746,8 +392,9 @@ Matx<_Tp,m,n> Matx<_Tp,m,n>::randn(_Tp a, _Tp b) template template inline Matx<_Tp, m, n>::operator Matx() const { - Vec v = *this; - return (Matx&)v; + Matx M; + for( int i = 0; i < m*n; i++ ) M.val[i] = saturate_cast(val[i]); + return M; } @@ -776,27 +423,27 @@ template inline Matx<_Tp, 1, n> Matx<_Tp, m, n>::row(int i) const { CV_DbgAssert((unsigned)i < (unsigned)m); - return (Matx<_Tp, 1, n>&)(*this)(i,0); + return Matx<_Tp, 1, n>(&val[i*n]); } template inline -Vec<_Tp, m> Matx<_Tp, m, n>::col(int j) const +Matx<_Tp, m, 1> Matx<_Tp, m, n>::col(int j) const { CV_DbgAssert((unsigned)j < (unsigned)n); - Vec<_Tp, m> v; + Matx<_Tp, m, 1> v; for( int i = 0; i < m; i++ ) - v[i] = (*this)(i,j); + v[i] = val[i*n + j]; return v; } template inline -Vec<_Tp, MIN(m,n)> Matx<_Tp, m, n>::diag() const +Matx<_Tp, MIN(m,n), 1> Matx<_Tp, m, n>::diag() const { diag_type d; for( int i = 0; i < MIN(m, n); i++ ) - d[i] = (*this)(i,i); + d.val[i] = val[i*n + i]; return d; } @@ -813,7 +460,7 @@ template inline _Tp& Matx<_Tp, m, n>::operator ()(int i, int j) { CV_DbgAssert( (unsigned)i < (unsigned)m && (unsigned)j < (unsigned)n ); - return this->val[i*n + j]; + return val[i*n + j]; } @@ -821,7 +468,7 @@ template inline const _Tp& Matx<_Tp, m, n>::operator ()(int i) const { CV_DbgAssert( (m == 1 || n == 1) && (unsigned)i < (unsigned)(m+n-1) ); - return this->val[i]; + return val[i]; } @@ -829,7 +476,7 @@ template inline _Tp& Matx<_Tp, m, n>::operator ()(int i) { CV_DbgAssert( (m == 1 || n == 1) && (unsigned)i < (unsigned)(m+n-1) ); - return this->val[i]; + return val[i]; } @@ -855,7 +502,7 @@ template inline Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_AddOp) { for( int i = 0; i < m*n; i++ ) - this->val[i] = saturate_cast<_Tp>(a.val[i] + b.val[i]); + val[i] = saturate_cast<_Tp>(a.val[i] + b.val[i]); } @@ -863,7 +510,7 @@ template inline Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_SubOp) { for( int i = 0; i < m*n; i++ ) - this->val[i] = saturate_cast<_Tp>(a.val[i] - b.val[i]); + val[i] = saturate_cast<_Tp>(a.val[i] - b.val[i]); } @@ -871,7 +518,7 @@ template template inline Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, _T2 alpha, Matx_ScaleOp) { for( int i = 0; i < m*n; i++ ) - this->val[i] = saturate_cast<_Tp>(a.val[i] * alpha); + val[i] = saturate_cast<_Tp>(a.val[i] * alpha); } @@ -879,7 +526,7 @@ template inline Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp) { for( int i = 0; i < m*n; i++ ) - this->val[i] = saturate_cast<_Tp>(a.val[i] * b.val[i]); + val[i] = saturate_cast<_Tp>(a.val[i] * b.val[i]); } @@ -892,7 +539,7 @@ Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_Mat _Tp s = 0; for( int k = 0; k < l; k++ ) s += a(i, k) * b(k, j); - this->val[i*n + j] = s; + val[i*n + j] = s; } } @@ -902,7 +549,7 @@ Matx<_Tp,m,n>::Matx(const Matx<_Tp, n, m>& a, Matx_TOp) { for( int i = 0; i < m; i++ ) for( int j = 0; j < n; j++ ) - this->val[i*n + j] = a(j, i); + val[i*n + j] = a(j, i); } @@ -991,13 +638,6 @@ Matx<_Tp, m, n> operator * (const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b) } -template static inline -Vec<_Tp, m> operator * (const Matx<_Tp, m, n>& a, const Vec<_Tp, n>& b) -{ - return Matx<_Tp, m, 1>(a, (const Matx<_Tp, n, 1>&)b, Matx_MatMulOp()); -} - - template static inline Point_<_Tp> operator * (const Matx<_Tp, 2, 2>& a, const Point_<_Tp>& b) { @@ -1020,16 +660,16 @@ Point3_<_Tp> operator * (const Matx<_Tp, 3, 3>& a, const Point_<_Tp>& b) template static inline -Vec<_Tp, 4> operator * (const Matx<_Tp, 4, 4>& a, const Point3_<_Tp>& b) +Matx<_Tp, 4, 1> operator * (const Matx<_Tp, 4, 4>& a, const Point3_<_Tp>& b) { - return a*Vec<_Tp,4>(b.x, b.y, b.z, 1); + return a*Matx<_Tp, 4, 1>(b.x, b.y, b.z, 1); } template static inline Scalar operator * (const Matx<_Tp, 4, 4>& a, const Scalar& b) { - return Scalar(a*Vec<_Tp,4>(b)); + return Scalar(a*Matx<_Tp, 4, 1>(b)); } @@ -1259,14 +899,57 @@ Matx<_Tp, n, l> Matx<_Tp, m, n>::solve(const Matx<_Tp, m, l>& rhs, int method) c return ok ? x : Matx<_Tp, n, l>::zeros(); } - -template inline -Vec<_Tp, n> Matx<_Tp, m, n>::solve(const Vec<_Tp, m>& rhs, int method) const + +template static inline +double norm(const Matx<_Tp, m, n>& M) { - return solve(Matx<_Tp, m, 1>(rhs), method); + double s = 0; + for( int i = 0; i < m*n; i++ ) + s += (double)M.val[i]*M.val[i]; + return std::sqrt(s); } - + + +template static inline +double norm(const Matx<_Tp, m, n>& M, int normType) +{ + if( normType == NORM_INF ) + { + T1 s = 0; + for( int i = 0; i < m*n; i++ ) + s = std::max(s, std::abs(M.val[i])); + return s; + } + + if( normType == NORM_L1 ) + { + T1 s = 0; + for( int i = 0; i < m*n; i++ ) + s += std::abs(M.val[i]); + return s; + } + + CV_DbgAssert( normType == NORM_L2 ); + return norm(M); +} + + +template static inline +bool operator == (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b) +{ + for( int i = 0; i < m*n; i++ ) + if( a[i] != b[i] ) return false; + return true; +} + +template static inline +bool operator != (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b) +{ + return !(a == b); +} + + template static inline MatxCommaInitializer<_Tp, m, n> operator << (const Matx<_Tp, m, n>& mtx, _T2 val) { @@ -1276,20 +959,304 @@ MatxCommaInitializer<_Tp, m, n> operator << (const Matx<_Tp, m, n>& mtx, _T2 val template inline MatxCommaInitializer<_Tp, m, n>::MatxCommaInitializer(Matx<_Tp, m, n>* _mtx) - : VecCommaInitializer<_Tp, m*n>((Vec<_Tp,m*n>*)_mtx) + : dst(_mtx), idx(0) {} template template inline MatxCommaInitializer<_Tp, m, n>& MatxCommaInitializer<_Tp, m, n>::operator , (_T2 value) { - return (MatxCommaInitializer<_Tp, m, n>&)VecCommaInitializer<_Tp, m*n>::operator ,(value); + CV_DbgAssert( idx < m*n ); + dst->val[idx++] = saturate_cast<_Tp>(value); + return *this; } template inline Matx<_Tp, m, n> MatxCommaInitializer<_Tp, m, n>::operator *() const { - CV_DbgAssert( this->idx == n*m ); - return (Matx<_Tp, m, n>&)*(this->vec); + CV_DbgAssert( idx == n*m ); + return *dst; +} + +/////////////////////////// short vector (Vec) ///////////////////////////// + +template inline Vec<_Tp, cn>::Vec() +{} + +template inline Vec<_Tp, cn>::Vec(_Tp v0) + : Matx<_Tp, cn, 1>(v0) +{} + +template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1) + : Matx<_Tp, cn, 1>(v0, v1) +{} + +template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2) + : Matx<_Tp, cn, 1>(v0, v1, v2) +{} + +template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3) + : Matx<_Tp, cn, 1>(v0, v1, v2, v3) +{} + +template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4) + : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4) +{} + +template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5) + : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5) +{} + +template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, + _Tp v4, _Tp v5, _Tp v6) + : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6) +{} + +template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, + _Tp v4, _Tp v5, _Tp v6, _Tp v7) + : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7) +{} + +template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, + _Tp v4, _Tp v5, _Tp v6, _Tp v7, + _Tp v8) + : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8) +{} + +template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, + _Tp v4, _Tp v5, _Tp v6, _Tp v7, + _Tp v8, _Tp v9) + : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) +{} + +template inline Vec<_Tp, cn>::Vec(const _Tp* values) + : Matx<_Tp, cn, 1>(values) +{} + + +template inline Vec<_Tp, cn>::Vec(const Vec<_Tp, cn>& v) + : Matx<_Tp, cn, 1>(v.val) +{} + +template inline Vec<_Tp, cn> Vec<_Tp, cn>::all(_Tp alpha) +{ + Vec v; + for( int i = 0; i < cn; i++ ) v.val[i] = alpha; + return v; +} + +template inline Vec<_Tp, cn> Vec<_Tp, cn>::mul(const Vec<_Tp, cn>& v) const +{ + Vec<_Tp, cn> w; + for( int i = 0; i < cn; i++ ) w.val[i] = saturate_cast<_Tp>(val[i]*v.val[i]); + return w; +} + +template inline Vec<_Tp, cn> Vec<_Tp, cn>::cross(const Vec<_Tp, cn>& v) const +{ + CV_Error(CV_StsError, "for arbitrary-size vector there is no cross-product defined"); + return Vec<_Tp, cn>(); +} + +template template +inline Vec<_Tp, cn>::operator Vec() const +{ + Vec v; + for( int i = 0; i < cn; i++ ) v.val[i] = saturate_cast(val[i]); + return v; +} + +template inline Vec<_Tp, cn>::operator CvScalar() const +{ + CvScalar s = {{0,0,0,0}}; + int i; + for( i = 0; i < std::min(cn, 4); i++ ) s.val[i] = val[i]; + for( ; i < 4; i++ ) s.val[i] = 0; + return s; +} + +template inline const _Tp& Vec<_Tp, cn>::operator [](int i) const +{ + CV_DbgAssert( (unsigned)i < (unsigned)cn ); + return val[i]; +} + +template inline _Tp& Vec<_Tp, cn>::operator [](int i) +{ + CV_DbgAssert( (unsigned)i < (unsigned)cn ); + return val[i]; +} + +template inline const _Tp& Vec<_Tp, cn>::operator ()(int i) const +{ + CV_DbgAssert( (unsigned)i < (unsigned)cn ); + return val[i]; +} + +template inline _Tp& Vec<_Tp, cn>::operator ()(int i) +{ + CV_DbgAssert( (unsigned)i < (unsigned)cn ); + return val[i]; +} + +template static inline Vec<_Tp1, cn>& +operator += (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b) +{ + for( int i = 0; i < cn; i++ ) + a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]); + return a; +} + +template static inline Vec<_Tp1, cn>& +operator -= (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b) +{ + for( int i = 0; i < cn; i++ ) + a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]); + return a; +} + +template static inline Vec<_Tp, cn> +operator + (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b) +{ + Vec<_Tp, cn> c = a; + return c += b; +} + +template static inline Vec<_Tp, cn> +operator - (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b) +{ + Vec<_Tp, cn> c = a; + return c -= b; +} + +template static inline +Vec<_Tp, 2>& operator *= (Vec<_Tp, 2>& a, _Tp alpha) +{ + a[0] *= alpha; a[1] *= alpha; + return a; +} + +template static inline +Vec<_Tp, 3>& operator *= (Vec<_Tp, 3>& a, _Tp alpha) +{ + a[0] *= alpha; a[1] *= alpha; a[2] *= alpha; + return a; +} + +template static inline +Vec<_Tp, 4>& operator *= (Vec<_Tp, 4>& a, _Tp alpha) +{ + a[0] *= alpha; a[1] *= alpha; a[2] *= alpha; a[3] *= alpha; + return a; +} + +template static inline Vec<_Tp, cn> +operator * (const Vec<_Tp, cn>& a, _Tp alpha) +{ + Vec<_Tp, cn> c = a; + return c *= alpha; +} + +template static inline Vec<_Tp, cn> +operator * (_Tp alpha, const Vec<_Tp, cn>& a) +{ + return a * alpha; +} + + +template static inline Vec<_Tp, 4> +operator * (const Vec<_Tp, 4>& a, const Vec<_Tp, 4>& b) +{ + return Vec<_Tp, 4>(saturate_cast<_Tp>(a[0]*b[0] - a[1]*b[1] - a[2]*b[2] - a[3]*b[3]), + saturate_cast<_Tp>(a[0]*b[1] + a[1]*b[0] + a[2]*b[3] - a[3]*b[2]), + saturate_cast<_Tp>(a[0]*b[2] - a[1]*b[3] + a[2]*b[0] - a[3]*b[1]), + saturate_cast<_Tp>(a[0]*b[3] + a[1]*b[2] - a[2]*b[1] - a[3]*b[0])); +} + + +template static inline Vec<_Tp, 4>& +operator *= (Vec<_Tp, 4>& a, const Vec<_Tp, 4>& b) +{ + a = a*b; + return a; +} + + +template static inline Vec<_Tp, cn> +operator - (const Vec<_Tp, cn>& a) +{ + Vec<_Tp,cn> t; + for( int i = 0; i < cn; i++ ) t.val[i] = saturate_cast<_Tp>(-a.val[i]); + return t; +} + +template<> inline Vec Vec::cross(const Vec& v) const +{ + return Vec(val[1]*v.val[2] - val[2]*v.val[1], + val[2]*v.val[0] - val[0]*v.val[2], + val[0]*v.val[1] - val[1]*v.val[0]); +} + +template<> inline Vec Vec::cross(const Vec& v) const +{ + return Vec(val[1]*v.val[2] - val[2]*v.val[1], + val[2]*v.val[0] - val[0]*v.val[2], + val[0]*v.val[1] - val[1]*v.val[0]); +} + +template static inline +Vec& operator += (Vec& a, const Vec& b) +{ + a[0] = saturate_cast(a[0] + b[0]); + a[1] = saturate_cast(a[1] + b[1]); + return a; +} + +template static inline +Vec& operator += (Vec& a, const Vec& b) +{ + a[0] = saturate_cast(a[0] + b[0]); + a[1] = saturate_cast(a[1] + b[1]); + a[2] = saturate_cast(a[2] + b[2]); + return a; +} + + +template static inline +Vec& operator += (Vec& a, const Vec& b) +{ + a[0] = saturate_cast(a[0] + b[0]); + a[1] = saturate_cast(a[1] + b[1]); + a[2] = saturate_cast(a[2] + b[2]); + a[3] = saturate_cast(a[3] + b[3]); + return a; +} + + +template static inline +VecCommaInitializer<_Tp, cn> operator << (const Vec<_Tp, cn>& vec, _T2 val) +{ + VecCommaInitializer<_Tp, cn> commaInitializer((Vec<_Tp, cn>*)&vec); + return (commaInitializer, val); +} + +template inline +VecCommaInitializer<_Tp, cn>::VecCommaInitializer(Vec<_Tp, cn>* _vec) + : MatxCommaInitializer(_vec) +{} + +template template inline +VecCommaInitializer<_Tp, cn>& VecCommaInitializer<_Tp, cn>::operator , (_T2 value) +{ + CV_DbgAssert( idx < cn ); + vec->val[idx++] = saturate_cast<_Tp>(value); + return *this; +} + +template inline +Vec<_Tp, cn> VecCommaInitializer<_Tp, cn>::operator *() const +{ + CV_DbgAssert( this->idx == cn ); + return *vec; } //////////////////////////////// Complex ////////////////////////////// @@ -1894,48 +1861,6 @@ template static inline bool operator != ( const Scalar_<_Tp>& a, c a.val[2] != b.val[2] || a.val[3] != b.val[3]; } -template template inline void Scalar_<_Tp>::convertTo(T2* buf, int cn, int unroll_to) const -{ - int i; - CV_Assert(cn <= 4); - for( i = 0; i < cn; i++ ) - buf[i] = saturate_cast(this->val[i]); - for( ; i < unroll_to; i++ ) - buf[i] = buf[i-cn]; -} - -static inline void scalarToRawData(const Scalar& s, void* buf, int type, int unroll_to=0) -{ - int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); - switch(depth) - { - case CV_8U: - s.convertTo((uchar*)buf, cn, unroll_to); - break; - case CV_8S: - s.convertTo((schar*)buf, cn, unroll_to); - break; - case CV_16U: - s.convertTo((ushort*)buf, cn, unroll_to); - break; - case CV_16S: - s.convertTo((short*)buf, cn, unroll_to); - break; - case CV_32S: - s.convertTo((int*)buf, cn, unroll_to); - break; - case CV_32F: - s.convertTo((float*)buf, cn, unroll_to); - break; - case CV_64F: - s.convertTo((double*)buf, cn, unroll_to); - break; - default: - CV_Error(CV_StsUnsupportedFormat,""); - } -} - - template static inline Scalar_<_Tp> operator + (const Scalar_<_Tp>& a, const Scalar_<_Tp>& b) { return Scalar_<_Tp>(saturate_cast<_Tp>(a.val[0] + b.val[0]), diff --git a/modules/core/src/arithm.cpp b/modules/core/src/arithm.cpp index d1c30e2891..813e3c59c8 100644 --- a/modules/core/src/arithm.cpp +++ b/modules/core/src/arithm.cpp @@ -1296,8 +1296,8 @@ inRangeS_( const Mat& srcmat1, const Scalar& _a, const Scalar& _b, Mat& dstmat ) size_t dstep = dstmat.step; Size size = getContinuousSize( srcmat1, dstmat ); int cn = srcmat1.channels(); - _a.convertTo((WT1*)&a, cn); - _b.convertTo((WT1*)&b, cn); + scalarToRawData(_a, &a, CV_MAKETYPE(DataType::depth, cn)); + scalarToRawData(_b, &b, CV_MAKETYPE(DataType::depth, cn)); for( int y = 0; y < size.height; y++, dst += dstep ) { diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index d599e0d47a..6ed7d6a75b 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -759,6 +759,81 @@ int Mat::checkVector(int _elemChannels, int _depth, bool _requireContinuous) con (isContinuous() || step.p[1] == step.p[2]*size.p[2]))) ? (int)(total()*channels()/_elemChannels) : -1; } + + +void scalarToRawData(const Scalar& s, void* _buf, int type, int unroll_to) +{ + int i, depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); + CV_Assert(cn <= 4); + switch(depth) + { + case CV_8U: + { + uchar* buf = (uchar*)_buf; + for(i = 0; i < cn; i++) + buf[i] = saturate_cast(s.val[i]); + for(; i < unroll_to; i++) + buf[i] = buf[i-cn]; + } + break; + case CV_8S: + { + schar* buf = (schar*)_buf; + for(i = 0; i < cn; i++) + buf[i] = saturate_cast(s.val[i]); + for(; i < unroll_to; i++) + buf[i] = buf[i-cn]; + } + break; + case CV_16U: + { + ushort* buf = (ushort*)_buf; + for(i = 0; i < cn; i++) + buf[i] = saturate_cast(s.val[i]); + for(; i < unroll_to; i++) + buf[i] = buf[i-cn]; + } + break; + case CV_16S: + { + short* buf = (short*)_buf; + for(i = 0; i < cn; i++) + buf[i] = saturate_cast(s.val[i]); + for(; i < unroll_to; i++) + buf[i] = buf[i-cn]; + } + break; + case CV_32S: + { + int* buf = (int*)_buf; + for(i = 0; i < cn; i++) + buf[i] = saturate_cast(s.val[i]); + for(; i < unroll_to; i++) + buf[i] = buf[i-cn]; + } + break; + case CV_32F: + { + float* buf = (float*)_buf; + for(i = 0; i < cn; i++) + buf[i] = saturate_cast(s.val[i]); + for(; i < unroll_to; i++) + buf[i] = buf[i-cn]; + } + break; + case CV_64F: + { + double* buf = (double*)_buf; + for(i = 0; i < cn; i++) + buf[i] = saturate_cast(s.val[i]); + for(; i < unroll_to; i++) + buf[i] = buf[i-cn]; + break; + } + default: + CV_Error(CV_StsUnsupportedFormat,""); + } +} /*************************************************************************************************\ Matrix Operations diff --git a/modules/core/src/precomp.hpp b/modules/core/src/precomp.hpp index b51ec7d699..d85d41b4b1 100644 --- a/modules/core/src/precomp.hpp +++ b/modules/core/src/precomp.hpp @@ -325,7 +325,7 @@ binarySOpCn_( const Mat& srcmat, Mat& dstmat, const Scalar& _scalar ) int cn = dstmat.channels(); Size size = getContinuousSize( srcmat, dstmat, cn ); WT scalar[12]; - _scalar.convertTo(scalar, cn, 12); + scalarToRawData(_scalar, scalar, CV_MAKETYPE(DataType::depth,cn), 12); for( ; size.height--; src0 += step1, dst0 += step ) { diff --git a/modules/flann/include/opencv2/flann/autotuned_index.h b/modules/flann/include/opencv2/flann/autotuned_index.h index 8a9b724983..4cc5829046 100644 --- a/modules/flann/include/opencv2/flann/autotuned_index.h +++ b/modules/flann/include/opencv2/flann/autotuned_index.h @@ -156,7 +156,7 @@ public: { int index_type; load_value(stream,index_type); - IndexParams* params = ParamsFactory::instance().create((flann_algorithm_t)index_type); + IndexParams* params = ParamsFactory_instance().create((flann_algorithm_t)index_type); bestIndex = create_index_by_type(dataset, *params); bestIndex->loadIndex(stream); load_value(stream, bestSearchParams); diff --git a/modules/flann/include/opencv2/flann/flann_base.hpp b/modules/flann/include/opencv2/flann/flann_base.hpp index f5e740cef0..89acd3c552 100644 --- a/modules/flann/include/opencv2/flann/flann_base.hpp +++ b/modules/flann/include/opencv2/flann/flann_base.hpp @@ -123,7 +123,7 @@ NNIndex* load_saved_index(const Matrix& dataset, const string& filename) throw FLANNException("The index saved belongs to a different dataset"); } - IndexParams* params = ParamsFactory::instance().create(header.index_type); + IndexParams* params = ParamsFactory_instance().create(header.index_type); NNIndex* nnIndex = create_index_by_type(dataset, *params); nnIndex->loadIndex(fin); fclose(fin); diff --git a/modules/flann/include/opencv2/flann/general.h b/modules/flann/include/opencv2/flann/general.h index 078ff1617e..045cdb3d47 100644 --- a/modules/flann/include/opencv2/flann/general.h +++ b/modules/flann/include/opencv2/flann/general.h @@ -134,7 +134,7 @@ public: typedef ObjectFactory ParamsFactory; - +CV_EXPORTS ParamsFactory& ParamsFactory_instance(); struct CV_EXPORTS SearchParams { SearchParams(int checks_ = 32) : diff --git a/modules/flann/include/opencv2/flann/object_factory.h b/modules/flann/include/opencv2/flann/object_factory.h index 927c7c233c..5c51e0db90 100644 --- a/modules/flann/include/opencv2/flann/object_factory.h +++ b/modules/flann/include/opencv2/flann/object_factory.h @@ -50,7 +50,7 @@ class ObjectFactory std::map object_registry; // singleton class, private constructor - ObjectFactory() {}; + //ObjectFactory() {}; public: typedef typename std::map::iterator Iterator; @@ -81,11 +81,11 @@ public: return ((*iter).second)(); } - static ObjectFactory& instance() + /*static ObjectFactory& instance() { static ObjectFactory the_factory; return the_factory; - } + }*/ }; diff --git a/modules/flann/src/flann.cpp b/modules/flann/src/flann.cpp index 828637192f..29ec99c0bf 100644 --- a/modules/flann/src/flann.cpp +++ b/modules/flann/src/flann.cpp @@ -195,16 +195,24 @@ void set_distance_type(flann_distance_t distance_type, int order) flann_minkowski_order_ = order; } + +static ParamsFactory the_factory; + +ParamsFactory& ParamsFactory_instance() +{ + return the_factory; +} + class StaticInit { public: StaticInit() { - ParamsFactory::instance().register_(LINEAR); - ParamsFactory::instance().register_(KDTREE); - ParamsFactory::instance().register_(KMEANS); - ParamsFactory::instance().register_(COMPOSITE); - ParamsFactory::instance().register_(AUTOTUNED); + ParamsFactory_instance().register_(LINEAR); + ParamsFactory_instance().register_(KDTREE); + ParamsFactory_instance().register_(KMEANS); + ParamsFactory_instance().register_(COMPOSITE); + ParamsFactory_instance().register_(AUTOTUNED); // ParamsFactory::instance().register_(SAVED); } }; diff --git a/modules/ml/src/gbt.cpp b/modules/ml/src/gbt.cpp index 9ee4ffd4a4..e277b01c61 100644 --- a/modules/ml/src/gbt.cpp +++ b/modules/ml/src/gbt.cpp @@ -188,7 +188,7 @@ CvGBTrees::train( const CvMat* _train_data, int _tflag, const CvMat* _responses, const CvMat* _var_idx, const CvMat* _sample_idx, const CvMat* _var_type, const CvMat* _missing_mask, - CvGBTreesParams _params, bool _update ) //update is not supported + CvGBTreesParams _params, bool /*_update*/ ) //update is not supported { CvMemStorage* storage = 0; @@ -1071,7 +1071,7 @@ bool CvGBTrees::train( const cv::Mat& trainData, int tflag, bool update ) { CvMat _trainData = trainData, _responses = responses; - CvMat _varIdx = varIdx, _sampleIdx = sampleIdx, _varType = _varType; + CvMat _varIdx = varIdx, _sampleIdx = sampleIdx, _varType = varType; CvMat _missingDataMask = missingDataMask; return train(&_trainData, tflag, &_responses, varIdx.empty() ? &_varIdx : 0,