![](images/MatBasicContainerOut3.png)
-- For small matrices you may use comma separated initializers:
+- For small matrices you may use comma separated initializers or initializer lists (C++11 support is required in the last case):
@snippet mat_the_basic_image_container.cpp comma
+ @snippet mat_the_basic_image_container.cpp list
+
![](images/MatBasicContainerOut6.png)
- Create a new header for an existing *Mat* object and @ref cv::Mat::clone or @ref cv::Mat::copyTo it.
/****************************************************************************************\
* C++ 11 *
\****************************************************************************************/
-#ifndef CV_CXX_11
-# if __cplusplus >= 201103L || defined(_MSC_VER) && _MSC_VER >= 1600
-# define CV_CXX_11 1
+#ifndef CV_CXX11
+# if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)
+# define CV_CXX11 1
# endif
#else
-# if CV_CXX_11 == 0
-# undef CV_CXX_11
+# if CV_CXX11 == 0
+# undef CV_CXX11
# endif
#endif
\****************************************************************************************/
#ifndef CV_CXX_MOVE_SEMANTICS
-# if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(_MSC_VER) && _MSC_VER >= 1600
+# if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || (defined(_MSC_VER) && _MSC_VER >= 1600)
# define CV_CXX_MOVE_SEMANTICS 1
# elif defined(__clang)
# if __has_feature(cxx_rvalue_references)
*/
template<typename _Tp> explicit Mat(const std::vector<_Tp>& vec, bool copyData=false);
+#ifdef CV_CXX11
+ /** @overload
+ */
+ template<typename _Tp> explicit Mat(const std::initializer_list<_Tp> list);
+#endif
+
#ifdef CV_CXX_STD_ARRAY
/** @overload
*/
explicit Mat_(const Point3_<typename DataType<_Tp>::channel_type>& pt, bool copyData=true);
explicit Mat_(const MatCommaInitializer_<_Tp>& commaInitializer);
+#ifdef CV_CXX11
+ Mat_(std::initializer_list<_Tp> values);
+#endif
+
#ifdef CV_CXX_STD_ARRAY
template <std::size_t _Nm> explicit Mat_(const std::array<_Tp, _Nm>& arr, bool copyData=false);
#endif
Mat((int)vec.size(), 1, DataType<_Tp>::type, (uchar*)&vec[0]).copyTo(*this);
}
+#ifdef CV_CXX11
+template<typename _Tp> inline
+Mat::Mat(const std::initializer_list<_Tp> list)
+ : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), dims(2), rows((int)list.size()),
+ cols(1), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
+{
+ if(list.size() == 0)
+ return;
+ Mat((int)list.size(), 1, DataType<_Tp>::type, (uchar*)list.begin()).copyTo(*this);
+}
+#endif
+
#ifdef CV_CXX_STD_ARRAY
template<typename _Tp, std::size_t _Nm> inline
Mat::Mat(const std::array<_Tp, _Nm>& arr, bool copyData)
: Mat(vec, copyData)
{}
+#ifdef CV_CXX11
+template<typename _Tp> inline
+Mat_<_Tp>::Mat_(std::initializer_list<_Tp> list)
+ : Mat(list)
+{}
+#endif
+
#ifdef CV_CXX_STD_ARRAY
template<typename _Tp> template<std::size_t _Nm> inline
Mat_<_Tp>::Mat_(const std::array<_Tp, _Nm>& arr, bool copyData)
#include "opencv2/core/traits.hpp"
#include "opencv2/core/saturate.hpp"
+#ifdef CV_CXX11
+#include <initializer_list>
+#endif
+
namespace cv
{
M(i,j) notation. Most of the common matrix operations (see also @ref MatrixExpressions ) are
available. To do an operation on Matx that is not implemented, you can easily convert the matrix to
Mat and backwards:
-@code
+@code{.cpp}
Matx33f m(1, 2, 3,
4, 5, 6,
7, 8, 9);
cout << sum(Mat(m*m.t())) << endl;
- @endcode
+@endcode
+Except of the plain constructor which takes a list of elements, Matx can be initialized from a C-array:
+@code{.cpp}
+ float values[] = { 1, 2, 3};
+ Matx31f m(values);
+@endcode
+In case if C++11 features are avaliable, std::initializer_list can be also used to initizlize Matx:
+@code{.cpp}
+ Matx31f m = { 1, 2, 3};
+@endcode
*/
template<typename _Tp, int m, int n> class Matx
{
_Tp v12, _Tp v13, _Tp v14, _Tp v15); //!< 1x16, 4x4 or 16x1 matrix
explicit Matx(const _Tp* vals); //!< initialize from a plain array
+#ifdef CV_CXX11
+ Matx(std::initializer_list<_Tp>); //!< initialize from an initializer list
+#endif
+
static Matx all(_Tp alpha);
static Matx zeros();
static Matx ones();
Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11, _Tp v12, _Tp v13); //!< 14-element vector constructor
explicit Vec(const _Tp* values);
+#ifdef CV_CXX11
+ Vec(std::initializer_list<_Tp>);
+#endif
+
Vec(const Vec<_Tp, cn>& v);
static Vec all(_Tp alpha);
for( int i = 0; i < channels; i++ ) val[i] = values[i];
}
+#ifdef CV_CXX11
+template<typename _Tp, int m, int n> inline
+Matx<_Tp, m, n>::Matx(std::initializer_list<_Tp> list)
+{
+ CV_DbgAssert(list.size() == channels);
+ int i = 0;
+ for(const auto& elem : list)
+ {
+ val[i++] = elem;
+ }
+}
+#endif
+
template<typename _Tp, int m, int n> inline
Matx<_Tp, m, n> Matx<_Tp, m, n>::all(_Tp alpha)
{
Vec<_Tp, cn>::Vec(const _Tp* values)
: Matx<_Tp, cn, 1>(values) {}
+#ifdef CV_CXX11
+template<typename _Tp, int cn> inline
+Vec<_Tp, cn>::Vec(std::initializer_list<_Tp> list)
+ : Matx<_Tp, cn, 1>(list) {}
+#endif
+
template<typename _Tp, int cn> inline
Vec<_Tp, cn>::Vec(const Vec<_Tp, cn>& m)
: Matx<_Tp, cn, 1>(m.val) {}
-//////////////////////////////// matx comma initializer //////////////////////////////////
+//////////////////////////////// vec comma initializer //////////////////////////////////
template<typename _Tp, typename _T2, int cn> static inline
#include "opencv2/core.hpp"
#include <ostream>
-#ifdef CV_CXX_11
+#ifdef CV_CXX11
#include <functional>
#endif
*/
CV_EXPORTS void parallel_for_(const Range& range, const ParallelLoopBody& body, double nstripes=-1.);
-#ifdef CV_CXX_11
+#ifdef CV_CXX11
class ParallelLoopBodyLambdaWrapper : public ParallelLoopBody
{
private:
ASSERT_EQ( cvtest::norm(a, b, NORM_INF), 0.);
}
+#ifdef CV_CXX11
+TEST(Core_Matx, from_initializer_list)
+{
+ Mat_<double> a = (Mat_<double>(2,2) << 10, 11, 12, 13);
+ Matx22d b = {10, 11, 12, 13};
+ ASSERT_EQ( cvtest::norm(a, b, NORM_INF), 0.);
+}
+#endif
+
TEST(Core_InputArray, empty)
{
vector<vector<Point> > data;
ASSERT_EQ(mat.channels(), 2);
}
-#ifdef CV_CXX_11
+#ifdef CV_CXX11
TEST(Mat_, range_based_for)
{
ASSERT_DOUBLE_EQ(norm(img, ref), 0.);
}
+TEST(Mat, from_initializer_list)
+{
+ Mat A({1.f, 2.f, 3.f});
+ Mat_<float> B(3, 1); B << 1, 2, 3;
+
+ ASSERT_EQ(A.type(), CV_32F);
+ ASSERT_DOUBLE_EQ(norm(A, B, NORM_INF), 0.);
+}
+
+TEST(Mat_, from_initializer_list)
+{
+ Mat_<float> A = {1, 2, 3};
+ Mat_<float> B(3, 1); B << 1, 2, 3;
+
+ ASSERT_DOUBLE_EQ(norm(A, B, NORM_INF), 0.);
+}
+
#endif
double t1 = (double) getTickCount();
- #ifdef CV_CXX_11
+ #ifdef CV_CXX11
//! [mandelbrot-parallel-call-cxx11]
parallel_for_(Range(0, mandelbrotImg.rows*mandelbrotImg.cols), [&](const Range& range){
Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
cout << "C = " << endl << " " << C << endl << endl;
//! [comma]
-
+ // do the same with initializer_list
+#ifdef CV_CXX11
+ //! [list]
+ C = (Mat_<double>({0, -1, 0, -1, 5, -1, 0, -1, 0})).reshape(3);
+ cout << "C = " << endl << " " << C << endl << endl;
+ //! [list]
+#endif
//! [clone]
Mat RowClone = C.row(1).clone();
cout << "RowClone = " << endl << " " << RowClone << endl << endl;