繁体   English   中英

为cv :: Mat保留未知大小的矢量存储器

[英]reserve vector memory for cv::Mat of unknown size

我想用给定的内存量初始化std :: vector,因此我必须使用std::vector::reserve 我完全理解它适用于int float等数据类型。

但是,对于cv::Mat来说,它应该如何工作基本上取决于尺寸,因此它需要多少内存是可变的。

我想知道是否有一种方法可以初始化传递给它的矩阵大小的向量。 否则,我想对像矩阵这样的数据类型使用reserve()毫无意义。

cv::mat矩阵在构造函数中分配数据。 正如您在下面看到的,数据只是指向某些数据的指针:

class CV_EXPORTS Mat
{
public:
    // ... a lot of methods ...
    ...

    /*! includes several bit-fields:
         - the magic signature
         - continuity flag
         - depth
         - number of channels
     */
    int flags;
    //! the array dimensionality, >= 2
    int dims;
    //! the number of rows and columns or (-1, -1) when the array has more than 2 dimensions
    int rows, cols;
    //! pointer to the data
    uchar* data;

    //! pointer to the reference counter;
    // when array points to user-allocated data, the pointer is NULL
    int* refcount;

    // other members
    ...
};

数据不是静态存储在矩阵中。 因此,每个Mat的大小均应相同。 在您的用例中,使用vector甚至没有意义,因为您似乎不需要存储大小不同的对象(除非您还计划添加从cv :: Mat派生的对象)

像所有其他对象一样, cv::mat堆栈大小在编译时是恒定的。 您可以使用sizeof(cv::mat)来学习它。 因此,您可以安全地将std::vector::reserve用于任何对象。

暂无
暂无

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

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