From fa6692afcf64d7d4a082175ce8f4ab4dcd03afb7 Mon Sep 17 00:00:00 2001 From: Addison Elliott Date: Wed, 14 Dec 2016 12:56:43 -0600 Subject: [PATCH] Added new overloaded functions for Mat and UMat that accepts std::vector instead of int * for the sizes on a N-dimensional array. This allows for an N-dimensional array to be setup in one line instead of two when using C++11 initializer lists. cv::Mat(3, {zDim, yDim, xDim}, ...) can be used instead of having to create an int pointer to hold the size array. --- modules/core/include/opencv2/core/mat.hpp | 39 +++++++++++++++++++ modules/core/include/opencv2/core/mat.inl.hpp | 17 ++++++++ modules/core/src/matrix.cpp | 16 ++++++++ modules/core/src/umatrix.cpp | 5 +++ 4 files changed, 77 insertions(+) diff --git a/modules/core/include/opencv2/core/mat.hpp b/modules/core/include/opencv2/core/mat.hpp index 758016a03e..c2b4558db5 100644 --- a/modules/core/include/opencv2/core/mat.hpp +++ b/modules/core/include/opencv2/core/mat.hpp @@ -794,6 +794,13 @@ public: */ Mat(int ndims, const int* sizes, int type); + /** @overload + @param sizes Array of integers specifying an n-dimensional array shape. + @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or + CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. + */ + Mat(const std::vector& sizes, int type); + /** @overload @param ndims Array dimensionality. @param sizes Array of integers specifying an n-dimensional array shape. @@ -805,6 +812,17 @@ public: */ Mat(int ndims, const int* sizes, int type, const Scalar& s); + /** @overload + @param sizes Array of integers specifying an n-dimensional array shape. + @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or + CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. + @param s An optional value to initialize each matrix element with. To set all the matrix elements to + the particular value after the construction, use the assignment operator + Mat::operator=(const Scalar& value) . + */ + Mat(const std::vector& sizes, int type, const Scalar& s); + + /** @overload @param m Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and @@ -861,6 +879,20 @@ public: */ Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0); + /** @overload + @param sizes Array of integers specifying an n-dimensional array shape. + @param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or + CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices. + @param data Pointer to the user data. Matrix constructors that take data and step parameters do not + allocate matrix data. Instead, they just initialize the matrix header that points to the specified + data, which means that no data is copied. This operation is very efficient and can be used to + process external data using OpenCV functions. The external data is not automatically deallocated, so + you should take care of it. + @param steps Array of ndims-1 steps in case of a multi-dimensional array (the last step is always + set to the element size). If not specified, the matrix is assumed to be continuous. + */ + Mat(const std::vector& sizes, int type, void* data, const size_t* steps=0); + /** @overload @param m Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and @@ -1328,6 +1360,12 @@ public: */ void create(int ndims, const int* sizes, int type); + /** @overload + @param sizes Array of integers specifying a new array shape. + @param type New matrix type. + */ + void create(const std::vector& sizes, int type); + /** @brief Increments the reference counter. The method increments the reference counter associated with the matrix data. If the matrix header @@ -2273,6 +2311,7 @@ public: void create(int rows, int cols, int type, UMatUsageFlags usageFlags = USAGE_DEFAULT); void create(Size size, int type, UMatUsageFlags usageFlags = USAGE_DEFAULT); void create(int ndims, const int* sizes, int type, UMatUsageFlags usageFlags = USAGE_DEFAULT); + void create(const std::vector& sizes, int type, UMatUsageFlags usageFlags = USAGE_DEFAULT); //! increases the reference counter; use with care to avoid memleaks void addref(); diff --git a/modules/core/include/opencv2/core/mat.inl.hpp b/modules/core/include/opencv2/core/mat.inl.hpp index 31638afd01..d164ac68c8 100644 --- a/modules/core/include/opencv2/core/mat.inl.hpp +++ b/modules/core/include/opencv2/core/mat.inl.hpp @@ -386,6 +386,23 @@ Mat::Mat(int _dims, const int* _sz, int _type, const Scalar& _s) *this = _s; } +inline +Mat::Mat(const std::vector& _sz, int _type) + : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), + datalimit(0), allocator(0), u(0), size(&rows) +{ + create(_sz, _type); +} + +inline +Mat::Mat(const std::vector& _sz, int _type, const Scalar& _s) + : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), + datalimit(0), allocator(0), u(0), size(&rows) +{ + create(_sz, _type); + *this = _s; +} + inline Mat::Mat(const Mat& m) : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), data(m.data), diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index 46137ddda3..d1383af8e8 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -439,6 +439,11 @@ void Mat::create(int d, const int* _sizes, int _type) finalizeHdr(*this); } +void Mat::create(const std::vector& _sizes, int _type) +{ + create((int)_sizes.size(), _sizes.data(), _type); +} + void Mat::copySize(const Mat& m) { setSize(*this, m.dims, 0, 0); @@ -541,6 +546,17 @@ Mat::Mat(int _dims, const int* _sizes, int _type, void* _data, const size_t* _st } +Mat::Mat(const std::vector& _sizes, int _type, void* _data, const size_t* _steps) + : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), + datalimit(0), allocator(0), u(0), size(&rows) +{ + flags |= CV_MAT_TYPE(_type); + datastart = data = (uchar*)_data; + setSize(*this, (int)_sizes.size(), _sizes.data(), _steps, true); + finalizeHdr(*this); +} + + Mat::Mat(const Mat& m, const Range* ranges) : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows) diff --git a/modules/core/src/umatrix.cpp b/modules/core/src/umatrix.cpp index 997ee2b6ef..a543595b8b 100644 --- a/modules/core/src/umatrix.cpp +++ b/modules/core/src/umatrix.cpp @@ -386,6 +386,11 @@ void UMat::create(int d, const int* _sizes, int _type, UMatUsageFlags _usageFlag addref(); } +void UMat::create(const std::vector& _sizes, int _type, UMatUsageFlags _usageFlags) +{ + create((int)_sizes.size(), _sizes.data(), _type, _usageFlags); +} + void UMat::copySize(const UMat& m) { setSize(*this, m.dims, 0, 0);