Memory Management



About

Memory management of mat objects across the opencv api.


Construction

Resources

Examples

# Internally allocated
cv::Mat image(rows, cols, CV_8U);
std::vector<unsigned char> image_buffer; // add some stuff to image buffer
cv::Mat image(image_buffer, true);       // second variable is copyData=true
# Externally Allocated - I
std::vector<unsigned char> memory;
memory.reserve(rows*cols);
cv::Mat image(rows, cols, CV_8U, memory.data());

# Externally Allocated - II
std::vector<unsigned char> memory(rows*cols);
cv::Mat image(rows, cols, CV_8U, memory.data());

# Reassigned - only creates new memory if the old matrix dimensions or type is different
image = cv::Mat(rows, cols, CV_8U);  # reuses
image = cv::Mat(rows, cols, CV_RGB); # new memory

CvtColor

These use the mat create function under the hood so the destination mat is only reallocated if it is a new object, or has different dimensions.

# calling this every loop for image_disparity_plane will only create the memory on the first run.
cv::cvtColor(gim0, image_disparity_plane, cv::COLOR_GRAY2BGR);