From 993522598b718bf141936c9f62b7e0171cd85998 Mon Sep 17 00:00:00 2001 From: Artanis <283186127@qq.com> Date: Fri, 1 Mar 2013 13:18:44 +0800 Subject: [PATCH] Add implementations for Mat::zeros() and Mat::ones() In class Mat, "static MatExpr Mat::zeros(int ndims, const int* sz, int type)" and "static MatExpr Mat::ones(int ndims, const int* sz, int type)" are declared but never implemented. That means we can see their manuals from "http://docs.opencv.org/modules/core/doc/basic_structures.html" but we can't use them. Here I tried to finish their implementation. I have also changed MatOp_Initializer::assign to make it support multi-dimension Mat. Test cases are added in test_math.cpp as well. --- modules/core/src/matop.cpp | 28 ++++++++++++++++++++++++++-- modules/core/test/test_math.cpp | 24 +++++++++++++++++++++--- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/modules/core/src/matop.cpp b/modules/core/src/matop.cpp index 736984e85d..7d43a8f029 100644 --- a/modules/core/src/matop.cpp +++ b/modules/core/src/matop.cpp @@ -200,6 +200,7 @@ public: void multiply(const MatExpr& e, double s, MatExpr& res) const; static void makeExpr(MatExpr& res, int method, Size sz, int type, double alpha=1); + static void makeExpr(MatExpr& res, int method, int ndims, const int* sizes, int type, double alpha=1); }; static MatOp_Initializer g_MatOp_Initializer; @@ -1551,8 +1552,13 @@ void MatOp_Initializer::assign(const MatExpr& e, Mat& m, int _type) const { if( _type == -1 ) _type = e.a.type(); - m.create(e.a.size(), _type); - if( e.flags == 'I' ) + + if( e.a.dims <= 2 ) + m.create(e.a.size(), _type); + else + m.create(e.a.dims, e.a.size, _type); + + if( e.flags == 'I' && e.a.dims <= 2 ) setIdentity(m, Scalar(e.alpha)); else if( e.flags == '0' ) m = Scalar(); @@ -1573,6 +1579,10 @@ inline void MatOp_Initializer::makeExpr(MatExpr& res, int method, Size sz, int t res = MatExpr(&g_MatOp_Initializer, method, Mat(sz, type, (void*)0), Mat(), Mat(), alpha, 0); } +inline void MatOp_Initializer::makeExpr(MatExpr& res, int method, int ndims, const int* sizes, int type, double alpha) +{ + res = MatExpr(&g_MatOp_Initializer, method, Mat(ndims, sizes, type, (void*)0), Mat(), Mat(), alpha, 0); +} /////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1618,6 +1628,13 @@ MatExpr Mat::zeros(Size size, int type) return e; } +MatExpr Mat::zeros(int ndims, const int* sizes, int type) +{ + MatExpr e; + MatOp_Initializer::makeExpr(e, '0', ndims, sizes, type); + return e; +} + MatExpr Mat::ones(int rows, int cols, int type) { MatExpr e; @@ -1632,6 +1649,13 @@ MatExpr Mat::ones(Size size, int type) return e; } +MatExpr Mat::ones(int ndims, const int* sizes, int type) +{ + MatExpr e; + MatOp_Initializer::makeExpr(e, '1', ndims, sizes, type); + return e; +} + MatExpr Mat::eye(int rows, int cols, int type) { MatExpr e; diff --git a/modules/core/test/test_math.cpp b/modules/core/test/test_math.cpp index 21c8cb4d5d..e02f78c541 100644 --- a/modules/core/test/test_math.cpp +++ b/modules/core/test/test_math.cpp @@ -2536,12 +2536,30 @@ TYPED_TEST_P(Core_CheckRange, Zero) double min_bound = 0.0; double max_bound = 0.1; - cv::Mat src = cv::Mat::zeros(3,3, cv::DataDepth::value); + cv::Mat src1 = cv::Mat::zeros(3, 3, cv::DataDepth::value); - ASSERT_TRUE( checkRange(src, true, NULL, min_bound, max_bound) ); + int sizes[] = {5, 6, 7}; + cv::Mat src2 = cv::Mat::zeros(3, sizes, cv::DataDepth::value); + + ASSERT_TRUE( checkRange(src1, true, NULL, min_bound, max_bound) ); + ASSERT_TRUE( checkRange(src2, true, NULL, min_bound, max_bound) ); +} + +TYPED_TEST_P(Core_CheckRange, One) +{ + double min_bound = 1.0; + double max_bound = 1.1; + + cv::Mat src1 = cv::Mat::ones(3, 3, cv::DataDepth::value); + + int sizes[] = {5, 6, 7}; + cv::Mat src2 = cv::Mat::ones(3, sizes, cv::DataDepth::value); + + ASSERT_TRUE( checkRange(src1, true, NULL, min_bound, max_bound) ); + ASSERT_TRUE( checkRange(src2, true, NULL, min_bound, max_bound) ); } -REGISTER_TYPED_TEST_CASE_P(Core_CheckRange, Negative, Positive, Bounds, Zero); +REGISTER_TYPED_TEST_CASE_P(Core_CheckRange, Negative, Positive, Bounds, Zero, One); typedef ::testing::Types mat_data_types; INSTANTIATE_TYPED_TEST_CASE_P(Negative_Test, Core_CheckRange, mat_data_types);