C++ 如何使用 zeros() 正确初始化指向 0 矩阵的 cv::Mat 指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8187763/
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 correctly initialize a cv::Mat pointer to a 0 matrix with zeros()
提问by Chris
I have the following initialized at the top of a function:
我在函数顶部初始化了以下内容:
cv::Mat *m;
Then, within a loop I am allocating new matrices with this name and storing them in a list. I want them to initialize as zero matrices with a specific size.
然后,在一个循环中,我用这个名字分配新矩阵并将它们存储在一个列表中。我希望它们初始化为具有特定大小的零矩阵。
This is what I tried:
这是我尝试过的:
m = new cv::Mat::zeros(height, width, CV_32F);
I tried this based on the example given in the OpenCV documentation. What is the correct way to perform this operation?
我根据 OpenCV 文档中给出的示例进行了尝试。执行此操作的正确方法是什么?
回答by Tyler Hyndman
From the documentation of Mat::zerosit use used like so
从Mat::zeros的文档中它使用像这样
cv::Mat m = cv::Mat::zeros(height, width, CV_32F);
If you want to use a Mat
allocated on the heap use
如果要Mat
在堆上使用分配的
cv::Mat * m = new cv::Mat( cv::Mat::zeros(height, width, CV_32F) );
// use m
delete m; // don't forget to delete m