Rasoul Rasoul - 1 year ago 73
C++ Question

How to change cv::Mat image dimensions dynamically?

I would like to declare an cv::Mat object and somewhere else in my code, change its dimension (nrows and ncols). I couldn't find any method in the documentation of OpenCV. They always suggest to include the dimension in the constuctor.

Answer Source

An easy and clean way is to use the create() method. You can call it as many times as you want, and it will reallocate the image buffer when the paramters sent to create and the current image parameters do not match:

Mat frame;

for(int i=0;i<n;i++)
    // if width[i], height[i] or type[i] are != to those on the i-1
    // or the frame is empty(first loop)
    // it allocates new memory
    frame.create(width[i], height[i], type[i]);
    // do some processing
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download