简体   繁体   中英

opencv Mat initialization with zero runtime error

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM