How to resize cv :: Mat image dimensions dynamically?

I would like to declare a cv :: Mat object and somewhere else in my code, resize it (nrows and ncols). I could not find any method in the OpenCV documentation. They always suggest including a dimension in the constuctor.

+4
source share
4 answers

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 } 
+3
source

If you want to resize the image, check resize() !

Create a new Mat dst with the dimensions and type of data you want:

 cv::resize(src, dst, dst.size(), 0, 0, cv::INTER_CUBIC); 

There are other interpolation methods besides cv::INTER_CUBIC , check the docs.

+6
source

Do you just want to define it using the Size variable, which you calculate as follows?

 // dynamically compute size... Size dynSize(0, 0); dynSize.width = magicWidth(); dynSize.height = magicHeight(); int dynType = CV_8UC1; // determine the type you want... Mat dynMat(dynSize, dynType); 
+3
source

If you know the maximum sizes and you only need to use the / cols row range from the total number of Mat, use the functions cv :: Mat :: rowRange and / or cv :: Mat :: colRange

http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-rowrange

0
source

Source: https://habr.com/ru/post/1387592/


All Articles