C++ opencv cv::mat 分配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11361765/
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
opencv cv::mat allocation
提问by jamk
Hello I have a basic question about opencv. If I try to allocate memory with the cv::Mat class I could do the following:
您好,我有一个关于 opencv 的基本问题。如果我尝试使用 cv::Mat 类分配内存,我可以执行以下操作:
cv::Mat sumimg(rows,cols,CV_32F,0);
float* sumimgrowptr = sumimg.ptr<float>(0);
but then I get a bad pointer (Null) back. In the internet some person use this:
但后来我得到了一个错误的指针(空)。在互联网上有人使用这个:
cv::Mat* ptrsumimg = new cv::Mat(rows,cols,CV_32F,0);
float* sumimgrowptr = ptrsumimg->ptr<float>(0);
and also here I get a Null pointer back ! but If I finally do this :
并且在这里我得到一个空指针!但如果我最终这样做:
cv::Mat sumimg;
sumimg.create(rows,cols,CV_32F);
sumimg.setTo(0);
float* sumimgrowptr = sumimg.ptr<float>(0);
then everything is fine ! so I wannted to know what is wrong in what I am doing ?
那么一切都很好!所以我想知道我在做什么有什么问题?
回答by Sam
The main problem is here
主要问题在这里
cv::Mat sumimg(rows,cols,CV_32F,0);
OpenCV provides multiple constructors for matrices. Two of them are declares as follows:
OpenCV 为矩阵提供了多个构造函数。其中两个声明如下:
cv::Mat(int rows, int cols, int type, SomeType initialValue);
and
和
cv::Mat(int rows, int cols, int type, char* preAllocatedPointerToData);
Now, if you declare a Mat as follows:
现在,如果你声明一个 Mat 如下:
cv::Mat sumimg(rows,cols,CV_32F,5.0);
you get a matrix of floats, allocated and initialized with 5.0. The constructor called is the first one.
你得到一个浮点矩阵,用 5.0 分配和初始化。调用的构造函数是第一个。
But here
但在这里
cv::Mat sumimg(rows,cols,CV_32F,0);
what you send is a 0
, which in C++ is a valid pointer address. So the compiler calls the constructor for the preallocated data. It does not allocate anything, and when you want to access its data pointer, it is, no wonder, 0 or NULL.
您发送的是 a 0
,它在 C++ 中是一个有效的指针地址。所以编译器为预分配的数据调用构造函数。它不分配任何东西,当你想访问它的数据指针时,难怪它是 0 或 NULL。
A solution is to specify that the fourth parameter is a float:
一个解决方案是指定第四个参数是一个浮点数:
cv::Mat sumimg(rows,cols,CV_32F,0.0);
But the best is to avoid such situations, by using a cv::Scalar() to init:
但最好是避免这种情况,通过使用 cv::Scalar() 来初始化:
cv::Mat sumimg(rows,cols,CV_32F, cv::Scalar::all(0) );
回答by Man of One Way
I believe you are calling the constructor Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP)
when you do cv::Mat sumimg(rows,cols,CV_32F,0);
and cv::Mat* ptrsumimg = new cv::Mat(rows,cols,CV_32F,0);
.
我相信您在调用和调用构造函数Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP)
时。cv::Mat sumimg(rows,cols,CV_32F,0);
cv::Mat* ptrsumimg = new cv::Mat(rows,cols,CV_32F,0);
See the documentation : http://opencv.willowgarage.com/documentation/cpp/basic_structures.html#mat
请参阅文档:http: //opencv.willowgarage.com/documentation/cpp/basic_structures.html#mat
I.e. you are not creating any values for the matrix, with means that you are not allocating any space for the values within the matrix, and because of that you receive a NULL
pointer when doing
即您没有为矩阵创建任何值,这意味着您没有为矩阵内的值分配任何空间,因此您NULL
在执行时会收到一个指针
ptrsumimg->ptr<float>(0);
ptrsumimg->ptr<float>(0);