C++ 如何清除cv::Mat内容?

C++ 如何清除cv::Mat内容?,c++,opencv,C++,Opencv,我有一个cv::Mat,但我已经插入了一些值,如何清除其中的内容 感谢您来自: 那我们就可以做了 m = Scalar(0,0,0); 用黑色像素填充。Scalar有4个组件,最后一个-alpha-是可选的。您应该调用release()函数 Mat img = Mat(Size(width, height), CV_8UC3, Scalar(0, 0, 0)); img.release(); 如果要释放Mat变量的内存,请使用release() 对于cv::Mat对象的向量,可以使用my

我有一个
cv::Mat
,但我已经插入了一些值,如何清除其中的内容

感谢您

来自:

那我们就可以做了

m = Scalar(0,0,0);
用黑色像素填充。Scalar有4个组件,最后一个-alpha-是可选的。

您应该调用release()函数

 Mat img = Mat(Size(width, height), CV_8UC3, Scalar(0, 0, 0));
 img.release();

如果要释放
Mat
变量的内存,请使用
release()

对于
cv::Mat
对象的向量,可以使用
myvector.clear()
释放整个向量的内存

std::vector myvector;
//初始化myvector。。
myvector.clear();//释放向量的内存

您可以
释放当前内容或分配新的

Mat m = Mat::ones(1, 5, CV_8U);

cout << "m: " << m << endl;
m.release();  //this will remove Mat m from memory

//Another way to clear the contents is by assigning an empty Mat:
m = Mat();

//After this the Mat can be re-assigned another value for example:
m = Mat::zeros(2,3, CV_8U);
cout << "m: " << m << endl;
Mat m=Mat::one(1,5,CV_8U);

如果您使用m.release()并再次尝试使用垫子,您将得到一个错误。因为他似乎只是一个重置值的人,分配一个标量值听起来更好。@luiz_s81不清楚他想要什么;这取决于用户。将Mat元素设置为零有什么意义?这是一个额外的操作,不会使矩阵占用更少的内存。如果他想重用该矩阵,他不妨保留以前的矩阵,然后在需要时将其元素重新分配给他想要的任何元素(不一定为零)。m.release()将删除Mat变量的内存。但是,只要将m赋给Mat()就可以清除内容,而不是继续使用m.release()。在m被赋值为Mat()之后,它可以被重新赋值给其他一些值,比如Mat::zeros(2,3,CV_8U)
Mat m;
// initialize m or do some processing
m.release();
std::vector<cv::Mat> myvector;
// initialize myvector .. 

myvector.clear(); // to release the memory of the vector
Mat m = Mat::ones(1, 5, CV_8U);

cout << "m: " << m << endl;
m.release();  //this will remove Mat m from memory

//Another way to clear the contents is by assigning an empty Mat:
m = Mat();

//After this the Mat can be re-assigned another value for example:
m = Mat::zeros(2,3, CV_8U);
cout << "m: " << m << endl;