Added new overloaded functions for Mat and UMat that accepts std::vector<int> 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.
pull/7858/head
Addison Elliott 8 years ago
parent 37cbcf024c
commit fa6692afcf
  1. 39
      modules/core/include/opencv2/core/mat.hpp
  2. 17
      modules/core/include/opencv2/core/mat.inl.hpp
  3. 16
      modules/core/src/matrix.cpp
  4. 5
      modules/core/src/umatrix.cpp

@ -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<int>& 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<int>& 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<int>& 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<int>& 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<int>& sizes, int type, UMatUsageFlags usageFlags = USAGE_DEFAULT);
//! increases the reference counter; use with care to avoid memleaks
void addref();

@ -386,6 +386,23 @@ Mat::Mat(int _dims, const int* _sz, int _type, const Scalar& _s)
*this = _s;
}
inline
Mat::Mat(const std::vector<int>& _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<int>& _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),

@ -439,6 +439,11 @@ void Mat::create(int d, const int* _sizes, int _type)
finalizeHdr(*this);
}
void Mat::create(const std::vector<int>& _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<int>& _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)

@ -386,6 +386,11 @@ void UMat::create(int d, const int* _sizes, int _type, UMatUsageFlags _usageFlag
addref();
}
void UMat::create(const std::vector<int>& _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);

Loading…
Cancel
Save