Ask Your Question
0

how to new Mat

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

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()?

Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

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

berak gravatar image

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

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

Question Tools

1 follower

Stats

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

Seen: 1,344 times

Last updated: Jun 30 '18