Ask Your Question

how to new Mat

asked 2018-06-28 09:52:18 -0500

waka gravatar image

In C++, we can dynamically create pointer like this:

int nCount = 5; float *fBuffer = new float[nCount];

For OenCV, if we want to allocate 5 Mat as well, shall we call

cv::Mat *MatArray = new cv::Mat(Height,width, 32FC3)[nCount] ?

or how can we achieve it?

Also, do we need to call MatArray.Release()?


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-06-28 10:05:47 -0500

berak gravatar image

updated 2018-06-30 01:47:26 -0500

no, please don't use new with cv::Mat, or raw pointers at all.

cv::Mat is already a smartpointer around the pixels, so passing around pointers to that will for sure wreck the internal refcounts, sooner or later

instead, please use a std::vector , like this:

vector<Mat> imgs(5); // allocate 5 (empty) Mats
for (int i=0; i<5; i++)  // load 5 images
    imgs[i] = imread(format("my_%d.jpg", i));

// no need to release() the mat's, this will be done automatically
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-06-28 09:52:18 -0500

Seen: 42 times

Last updated: Jun 30