0
votes

When I try to initialize a cv::Mat with

mask = cv::Mat::zeros(20, 1, CV_8U);

or

mask = cv::Mat(20, 1, CV_8U, 1);

I seem to get a correctly initialized Mat. But

mask = cv::Mat(20, 1, CV_8U, 0);

throws this runtime error when I simply use std::cout << mask.size() << std::endl; or std::cout << mask << std::endl;

OpenCV Error: Assertion failed (total() == 0 || data != NULL) in Mat, file /usr/local/include/opencv2/core/mat.inl.hpp, line 579 terminate called after throwing an instance of 'cv::Exception' what(): /usr/local/include/opencv2/core/mat.inl.hpp:579: error: (-215) total() == 0 || data != NULL in function Mat

which is strange... Any idea why this might be happening?

1

1 Answers

0
votes

OpenCV has following two constructors for Mat among many others:

Mat (int ndims, const int *sizes, int type, const Scalar &s);
Mat (int rows, int cols, int type, void *data, size_t step=AUTO_STEP);

When you construct the Mat as in

mask = cv::Mat(20, 1, CV_8U, 0);

it uses the second constructor.

If you want to invoke the first constructor, you should do something like

mask = cv::Mat(20, 1, CV_8U, Scalar(0));