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.
This commit is contained in:
Artanis 2013-03-01 13:18:44 +08:00
parent 0532a521a2
commit 993522598b
2 changed files with 47 additions and 5 deletions

View File

@ -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;

View File

@ -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<TypeParam>::value);
cv::Mat src1 = cv::Mat::zeros(3, 3, cv::DataDepth<TypeParam>::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<TypeParam>::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);
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<TypeParam>::value);
int sizes[] = {5, 6, 7};
cv::Mat src2 = cv::Mat::ones(3, sizes, cv::DataDepth<TypeParam>::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, One);
typedef ::testing::Types<signed char,unsigned char, signed short, unsigned short, signed int> mat_data_types;
INSTANTIATE_TYPED_TEST_CASE_P(Negative_Test, Core_CheckRange, mat_data_types);