0

I am trying to create an array of Mat objects to store images and am getting warnings for using anything other than a statically typed number such as 10

int numberOfRects = boundRect.size();
Mat image_array[numberOfRects];

When I try this code I get an error stating Variable length array of non-POD element type 'cv::Mat'

Same goes for this code: Mat image_array[boundRect.size()];

How can I create an array of Mats based on the size of boundRect?

1 Answer 1

2

You need to create a dynamic array. This is a basic, but non-simple aspect of the language to learn, so I suggest you learn on some simple examples First.

You can create it like this

Mat *image_array = new Mat[numberOfRects];

But must delete it when you are done, otherwise there will be a memory leak.

delete[] image_array;

A better alternative is to use std::vector, which automatically deletes its contents. But a class must be copyable to be used in it, and I don't know what's allowed by this Mat class of yours. It would look like

std::vector<Mat> image_array(numberOfRects);

If Mat cannot be copied, the correct C++11 solution is to use a vector of smart pointers.

std::vector<std::unique_ptr<Mat>> image_array(numberOfRects);
for (auto& mat : image_array)
{
    mat = std::make_unique<Mat>();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.