Many operation will automatically init cv::Mat on heap like cv::resize
:
cv::Mat a = cv::imread("aaa.jpg");
cv::Mat b;
cv::Mat c;
cv::Mat d;
cv::resize(a, b, {256, 256});
cv::resize(a, c, {256*2, 256*2});
cv::resize(a, d, {256*3, 256*3});
This operation will alloc memory for b,c and d but may not be Continuous. In Android if this alloc/release operation happened frequently it will cause many memory fragment and may drag performance.
I’v noticed that pre-alloc memory for cv::mat using overload function cv::Mat(int h, int w, int type, void* data)
for dst
can avoid this matter but I must destinate image size at first and this seems not elegant.
Any better way to avoid memory fragment ?