An easy and clean way is to use the create () method. You can call it as many times as you want, and it redistributes the image buffer when the transferred parameters are created, 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 }
source share