C++ 如何清除 cv::Mat 内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16817263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear the cv::Mat contents?
提问by anarchy99
I have a cv::Mat
but I have already insert it with some values, how do I clear the contents in it?
我有一个cv::Mat
但我已经插入了一些值,如何清除其中的内容?
Thank you
谢谢
回答by Alexey
If you want to release the memory of the Mat
variable use release()
.
如果要释放Mat
变量的内存,请使用release()
.
Mat m;
// initialize m or do some processing
m.release();
For a vector of cv::Mat
objects you can release the memory of the whole vector with myvector.clear()
.
对于cv::Mat
对象向量,您可以使用 释放整个向量的内存myvector.clear()
。
std::vector<cv::Mat> myvector;
// initialize myvector ..
myvector.clear(); // to release the memory of the vector
回答by CapelliC
回答by Poko
You should call release() function.
您应该调用 release() 函数。
Mat img = Mat(Size(width, height), CV_8UC3, Scalar(0, 0, 0));
img.release();
回答by abggcv
You can release
the current contents or assign a new Mat
.
您可以release
将当前内容或分配一个新的Mat
.
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;