From 3d1355efc10b260af0bad34026e0c08774e67ea8 Mon Sep 17 00:00:00 2001 From: Dan Moodie Date: Tue, 29 Sep 2015 20:16:18 -0400 Subject: [PATCH 1/2] Updated input / output array to support std::vector Expanded support for std::vector. Whitespace fix. --- modules/core/include/opencv2/core/mat.hpp | 11 +++- modules/core/include/opencv2/core/mat.inl.hpp | 10 +++ modules/core/src/matrix.cpp | 62 ++++++++++++++++++- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/modules/core/include/opencv2/core/mat.hpp b/modules/core/include/opencv2/core/mat.hpp index 3133876e4..cd76027c2 100644 --- a/modules/core/include/opencv2/core/mat.hpp +++ b/modules/core/include/opencv2/core/mat.hpp @@ -164,7 +164,8 @@ public: CUDA_GPU_MAT = 9 << KIND_SHIFT, UMAT =10 << KIND_SHIFT, STD_VECTOR_UMAT =11 << KIND_SHIFT, - STD_BOOL_VECTOR =12 << KIND_SHIFT + STD_BOOL_VECTOR =12 << KIND_SHIFT, + STD_VECTOR_CUDA_GPU_MAT = 13 << KIND_SHIFT }; _InputArray(); @@ -181,6 +182,7 @@ public: template _InputArray(const Matx<_Tp, m, n>& matx); _InputArray(const double& val); _InputArray(const cuda::GpuMat& d_mat); + _InputArray(const std::vector& d_mat_array); _InputArray(const ogl::Buffer& buf); _InputArray(const cuda::HostMem& cuda_mem); template _InputArray(const cudev::GpuMat_<_Tp>& m); @@ -192,6 +194,7 @@ public: UMat getUMat(int idx=-1) const; void getMatVector(std::vector& mv) const; void getUMatVector(std::vector& umv) const; + void getGpuMatVector(std::vector** gpumv) const; cuda::GpuMat getGpuMat() const; ogl::Buffer getOGlBuffer() const; @@ -223,7 +226,7 @@ public: bool isUMatVector() const; bool isMatx() const; bool isVector() const; - + bool isGpuMatVector() const; ~_InputArray(); protected: @@ -283,6 +286,7 @@ public: _OutputArray(Mat& m); _OutputArray(std::vector& vec); _OutputArray(cuda::GpuMat& d_mat); + _OutputArray(std::vector& d_mat); _OutputArray(ogl::Buffer& buf); _OutputArray(cuda::HostMem& cuda_mem); template _OutputArray(cudev::GpuMat_<_Tp>& m); @@ -299,6 +303,7 @@ public: _OutputArray(const Mat& m); _OutputArray(const std::vector& vec); _OutputArray(const cuda::GpuMat& d_mat); + _OutputArray(const std::vector& d_mat); _OutputArray(const ogl::Buffer& buf); _OutputArray(const cuda::HostMem& cuda_mem); template _OutputArray(const cudev::GpuMat_<_Tp>& m); @@ -317,6 +322,7 @@ public: Mat& getMatRef(int i=-1) const; UMat& getUMatRef(int i=-1) const; cuda::GpuMat& getGpuMatRef() const; + std::vector& getGpuMatVecRef() const; ogl::Buffer& getOGlBufferRef() const; cuda::HostMem& getHostMemRef() const; void create(Size sz, int type, int i=-1, bool allowTransposed=false, int fixedDepthMask=0) const; @@ -356,6 +362,7 @@ public: _InputOutputArray(const Mat& m); _InputOutputArray(const std::vector& vec); _InputOutputArray(const cuda::GpuMat& d_mat); + _InputOutputArray(const std::vector& d_mat); _InputOutputArray(const ogl::Buffer& buf); _InputOutputArray(const cuda::HostMem& cuda_mem); template _InputOutputArray(const cudev::GpuMat_<_Tp>& m); diff --git a/modules/core/include/opencv2/core/mat.inl.hpp b/modules/core/include/opencv2/core/mat.inl.hpp index b7f46d6a9..f27d8ddb6 100644 --- a/modules/core/include/opencv2/core/mat.inl.hpp +++ b/modules/core/include/opencv2/core/mat.inl.hpp @@ -110,6 +110,9 @@ inline _InputArray::_InputArray(const MatExpr& expr) inline _InputArray::_InputArray(const cuda::GpuMat& d_mat) { init(CUDA_GPU_MAT + ACCESS_READ, &d_mat); } +inline _InputArray::_InputArray(const std::vector& d_mat) +{ init(STD_VECTOR_CUDA_GPU_MAT + ACCESS_READ, &d_mat);} + inline _InputArray::_InputArray(const ogl::Buffer& buf) { init(OPENGL_BUFFER + ACCESS_READ, &buf); } @@ -131,6 +134,7 @@ inline bool _InputArray::isMatVector() const { return kind() == _InputArray::STD inline bool _InputArray::isUMatVector() const { return kind() == _InputArray::STD_VECTOR_UMAT; } inline bool _InputArray::isMatx() const { return kind() == _InputArray::MATX; } inline bool _InputArray::isVector() const { return kind() == _InputArray::STD_VECTOR || kind() == _InputArray::STD_BOOL_VECTOR; } +inline bool _InputArray::isGpuMatVector() const { return kind() == _InputArray::STD_VECTOR_CUDA_GPU_MAT; } //////////////////////////////////////////////////////////////////////////////////////// @@ -196,6 +200,9 @@ _OutputArray::_OutputArray(const _Tp* vec, int n) inline _OutputArray::_OutputArray(cuda::GpuMat& d_mat) { init(CUDA_GPU_MAT + ACCESS_WRITE, &d_mat); } +inline _OutputArray::_OutputArray(std::vector& d_mat) +{ init(STD_VECTOR_CUDA_GPU_MAT + ACCESS_WRITE, &d_mat);} + inline _OutputArray::_OutputArray(ogl::Buffer& buf) { init(OPENGL_BUFFER + ACCESS_WRITE, &buf); } @@ -217,6 +224,7 @@ inline _OutputArray::_OutputArray(const std::vector& vec) inline _OutputArray::_OutputArray(const cuda::GpuMat& d_mat) { init(FIXED_TYPE + FIXED_SIZE + CUDA_GPU_MAT + ACCESS_WRITE, &d_mat); } + inline _OutputArray::_OutputArray(const ogl::Buffer& buf) { init(FIXED_TYPE + FIXED_SIZE + OPENGL_BUFFER + ACCESS_WRITE, &buf); } @@ -306,6 +314,8 @@ inline _InputOutputArray::_InputOutputArray(const std::vector& vec) inline _InputOutputArray::_InputOutputArray(const cuda::GpuMat& d_mat) { init(FIXED_TYPE + FIXED_SIZE + CUDA_GPU_MAT + ACCESS_RW, &d_mat); } +inline _InputOutputArray::_InputOutputArray(const std::vector& d_mat) +{ init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_CUDA_GPU_MAT + ACCESS_RW, &d_mat);} inline _InputOutputArray::_InputOutputArray(const ogl::Buffer& buf) { init(FIXED_TYPE + FIXED_SIZE + OPENGL_BUFFER + ACCESS_RW, &buf); } diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index 9c05260e1..958ee616b 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -1429,7 +1429,14 @@ cuda::GpuMat _InputArray::getGpuMat() const CV_Error(cv::Error::StsNotImplemented, "getGpuMat is available only for cuda::GpuMat and cuda::HostMem"); return cuda::GpuMat(); } - +void _InputArray::getGpuMatVector(std::vector** gpumv) const +{ + int k = kind(); + if (k == STD_VECTOR_CUDA_GPU_MAT) + { + *gpumv = (std::vector*)obj; + } +} ogl::Buffer _InputArray::getOGlBuffer() const { int k = kind(); @@ -1524,6 +1531,15 @@ Size _InputArray::size(int i) const return vv[i].size(); } + if (k == STD_VECTOR_CUDA_GPU_MAT) + { + const std::vector& vv = *(const std::vector*)obj; + if (i < 0) + return vv.empty() ? Size() : Size((int)vv.size(), 1); + CV_Assert(i < (int)vv.size()); + return vv[i].size(); + } + if( k == STD_VECTOR_UMAT ) { const std::vector& vv = *(const std::vector*)obj; @@ -1765,6 +1781,7 @@ size_t _InputArray::total(int i) const return vv[i].total(); } + if( k == STD_VECTOR_UMAT ) { const std::vector& vv = *(const std::vector*)obj; @@ -1821,6 +1838,18 @@ int _InputArray::type(int i) const return vv[i >= 0 ? i : 0].type(); } + if (k == STD_VECTOR_CUDA_GPU_MAT) + { + const std::vector& vv = *(const std::vector*)obj; + if (vv.empty()) + { + CV_Assert((flags & FIXED_TYPE) != 0); + return CV_MAT_TYPE(flags); + } + CV_Assert(i < (int)vv.size()); + return vv[i >= 0 ? i : 0].type(); + } + if( k == OPENGL_BUFFER ) return ((const ogl::Buffer*)obj)->type(); @@ -1899,6 +1928,12 @@ bool _InputArray::empty() const if( k == CUDA_GPU_MAT ) return ((const cuda::GpuMat*)obj)->empty(); + if (k == STD_VECTOR_CUDA_GPU_MAT) + { + const std::vector& vv = *(const std::vector*)obj; + return vv.empty(); + } + if( k == CUDA_HOST_MEM ) return ((const cuda::HostMem*)obj)->empty(); @@ -2015,6 +2050,13 @@ size_t _InputArray::offset(int i) const return (size_t)(m->data - m->datastart); } + if (k == STD_VECTOR_CUDA_GPU_MAT) + { + const std::vector& vv = *(const std::vector*)obj; + CV_Assert((size_t)i < vv.size()); + return (size_t)(vv[i].data - vv[i].datastart); + } + CV_Error(Error::StsNotImplemented, ""); return 0; } @@ -2060,6 +2102,12 @@ size_t _InputArray::step(int i) const CV_Assert( i < 0 ); return ((const cuda::GpuMat*)obj)->step; } + if (k == STD_VECTOR_CUDA_GPU_MAT) + { + const std::vector& vv = *(const std::vector*)obj; + CV_Assert((size_t)i < vv.size()); + return vv[i].step; + } CV_Error(Error::StsNotImplemented, ""); return 0; @@ -2562,7 +2610,11 @@ void _OutputArray::release() const ((std::vector*)obj)->clear(); return; } - + if (k == STD_VECTOR_CUDA_GPU_MAT) + { + ((std::vector*)obj)->clear(); + return; + } CV_Error(Error::StsNotImplemented, "Unknown/unsupported array type"); } @@ -2625,6 +2677,12 @@ cuda::GpuMat& _OutputArray::getGpuMatRef() const CV_Assert( k == CUDA_GPU_MAT ); return *(cuda::GpuMat*)obj; } +std::vector& _OutputArray::getGpuMatVecRef() const +{ + int k = kind(); + CV_Assert(k == STD_VECTOR_CUDA_GPU_MAT); + return *(std::vector*)obj; +} ogl::Buffer& _OutputArray::getOGlBufferRef() const { From 1e1f20cfee89c9ac6c8b884e52bcd79a51278976 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 12 Oct 2015 09:21:53 -0400 Subject: [PATCH 2/2] Improved consistency. --- modules/core/include/opencv2/core/mat.hpp | 2 +- modules/core/src/matrix.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/core/include/opencv2/core/mat.hpp b/modules/core/include/opencv2/core/mat.hpp index cd76027c2..f85b38ea5 100644 --- a/modules/core/include/opencv2/core/mat.hpp +++ b/modules/core/include/opencv2/core/mat.hpp @@ -194,7 +194,7 @@ public: UMat getUMat(int idx=-1) const; void getMatVector(std::vector& mv) const; void getUMatVector(std::vector& umv) const; - void getGpuMatVector(std::vector** gpumv) const; + void getGpuMatVector(std::vector& gpumv) const; cuda::GpuMat getGpuMat() const; ogl::Buffer getOGlBuffer() const; diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index 958ee616b..717b7ea4f 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -1429,12 +1429,12 @@ cuda::GpuMat _InputArray::getGpuMat() const CV_Error(cv::Error::StsNotImplemented, "getGpuMat is available only for cuda::GpuMat and cuda::HostMem"); return cuda::GpuMat(); } -void _InputArray::getGpuMatVector(std::vector** gpumv) const +void _InputArray::getGpuMatVector(std::vector& gpumv) const { int k = kind(); if (k == STD_VECTOR_CUDA_GPU_MAT) { - *gpumv = (std::vector*)obj; + gpumv = *(std::vector*)obj; } } ogl::Buffer _InputArray::getOGlBuffer() const