简体   繁体   English

opencv cv :: mat allocation

[英]opencv cv::mat allocation

Hello I have a basic question about opencv. 你好,我有一个关于opencv的基本问题。 If I try to allocate memory with the cv::Mat class I could do the following: 如果我尝试使用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. 但后来我得到了一个糟糕的指针(Null)。 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 ! 而且在这里我得到一个Null指针! 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 ? 所以我很想知道我在做什么是错的?

The main problem is here 主要问题在这里

cv::Mat sumimg(rows,cols,CV_32F,0);

OpenCV provides multiple constructors for matrices. OpenCV为矩阵提供了多个构造函数。 Two of them are declares as follows: 其中两个声明如下:

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. 你得到一个浮点矩阵,用5.0分配和初始化。 The constructor called is the first one. 被调用的构造函数是第一个。

But here 但在这儿

cv::Mat sumimg(rows,cols,CV_32F,0);

what you send is a 0 , which in C++ is a valid pointer address. 你发送的是0 ,它在C ++中是一个有效的指针地址。 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. 它没有分配任何东西,当你想访问它的数据指针时,毫无疑问,0或NULL。

A solution is to specify that the fourth parameter is a float: 解决方案是指定第四个参数是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) );

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); 当你做cv::Mat sumimg(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); and cv::Mat* ptrsumimg = new cv::Mat(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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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