Add new Mat constructor (#10808)

* Add new Mat constructor

* Fix build error

* Fix build error

* Fixed the code about 4 comments

* Fixed three comments

* delete previous local declaration

* fix build error
pull/10843/head
yuki takehara 7 years ago committed by Vadim Pisarevsky
parent ab0f0f26a1
commit 379ea15d16
  1. 5
      modules/core/include/opencv2/core/mat.hpp
  2. 17
      modules/core/include/opencv2/core/mat.inl.hpp
  3. 6
      modules/core/test/test_mat.cpp

@ -989,6 +989,10 @@ public:
*/ */
template<typename _Tp, typename = typename std::enable_if<std::is_arithmetic<_Tp>::value>::type> template<typename _Tp, typename = typename std::enable_if<std::is_arithmetic<_Tp>::value>::type>
explicit Mat(const std::initializer_list<_Tp> list); explicit Mat(const std::initializer_list<_Tp> list);
/** @overload
*/
template<typename _Tp> explicit Mat(const std::initializer_list<int> sizes, const std::initializer_list<_Tp> list);
#endif #endif
#ifdef CV_CXX_STD_ARRAY #ifdef CV_CXX_STD_ARRAY
@ -2208,6 +2212,7 @@ public:
#ifdef CV_CXX11 #ifdef CV_CXX11
Mat_(std::initializer_list<_Tp> values); Mat_(std::initializer_list<_Tp> values);
explicit Mat_(const std::initializer_list<int> sizes, const std::initializer_list<_Tp> values);
#endif #endif
#ifdef CV_CXX_STD_ARRAY #ifdef CV_CXX_STD_ARRAY

@ -584,6 +584,18 @@ Mat::Mat(const std::initializer_list<_Tp> list)
return; return;
Mat((int)list.size(), 1, traits::Type<_Tp>::value, (uchar*)list.begin()).copyTo(*this); Mat((int)list.size(), 1, traits::Type<_Tp>::value, (uchar*)list.begin()).copyTo(*this);
} }
template<typename _Tp> inline
Mat::Mat(const std::initializer_list<int> sizes, const std::initializer_list<_Tp> list)
: Mat()
{
size_t size_total = 1;
int *sz = (int*)sizes.begin();
for(auto s : sizes)
size_total *= s;
CV_Assert(list.size() != 0 || size_total == list.size());
Mat((int)sizes.size(), sz, traits::Type<_Tp>::value, (uchar*)list.begin()).copyTo(*this);
}
#endif #endif
#ifdef CV_CXX_STD_ARRAY #ifdef CV_CXX_STD_ARRAY
@ -1629,6 +1641,11 @@ template<typename _Tp> inline
Mat_<_Tp>::Mat_(std::initializer_list<_Tp> list) Mat_<_Tp>::Mat_(std::initializer_list<_Tp> list)
: Mat(list) : Mat(list)
{} {}
template<typename _Tp> inline
Mat_<_Tp>::Mat_(const std::initializer_list<int> sizes, std::initializer_list<_Tp> list)
: Mat(sizes, list)
{}
#endif #endif
#ifdef CV_CXX_STD_ARRAY #ifdef CV_CXX_STD_ARRAY

@ -1719,17 +1719,23 @@ TEST(Mat, from_initializer_list)
{ {
Mat A({1.f, 2.f, 3.f}); Mat A({1.f, 2.f, 3.f});
Mat_<float> B(3, 1); B << 1, 2, 3; Mat_<float> B(3, 1); B << 1, 2, 3;
Mat_<float> C({3}, {1,2,3});
ASSERT_EQ(A.type(), CV_32F); ASSERT_EQ(A.type(), CV_32F);
ASSERT_DOUBLE_EQ(cvtest::norm(A, B, NORM_INF), 0.); ASSERT_DOUBLE_EQ(cvtest::norm(A, B, NORM_INF), 0.);
ASSERT_DOUBLE_EQ(cvtest::norm(A, C, NORM_INF), 0.);
ASSERT_DOUBLE_EQ(cvtest::norm(B, C, NORM_INF), 0.);
} }
TEST(Mat_, from_initializer_list) TEST(Mat_, from_initializer_list)
{ {
Mat_<float> A = {1, 2, 3}; Mat_<float> A = {1, 2, 3};
Mat_<float> B(3, 1); B << 1, 2, 3; Mat_<float> B(3, 1); B << 1, 2, 3;
Mat_<float> C({3}, {1,2,3});
ASSERT_DOUBLE_EQ(cvtest::norm(A, B, NORM_INF), 0.); ASSERT_DOUBLE_EQ(cvtest::norm(A, B, NORM_INF), 0.);
ASSERT_DOUBLE_EQ(cvtest::norm(A, C, NORM_INF), 0.);
ASSERT_DOUBLE_EQ(cvtest::norm(B, C, NORM_INF), 0.);
} }

Loading…
Cancel
Save