简体   繁体   中英

Control image data memory in cv::Mat

I'm building a pipeline for an image treatment project using OpenCv. In one of my classes I generate an image:

m_RGBMatData = cv::Mat(cv::Size(1824, 948), CV_16U);

and I want to save its data in a buffer. The pointer I use on my buffer is called m_host_Rgb This works when I do

memcpy(m_host_Rgb, m_RGBMatData.data, 1824 * 948 * 3 * 2); => (16bits RGB image!)

But I would like to avoid the memcpy function in oder to improve performance. I tried this:

m_host_Rgb = m_RGBMatData.data;

But I think the cv::Mat object deletes its memory when is out of scope. Is there a way to keep the cv::Mat object's memory until next call? or something like that?

thanks!

If you want to manage the memory yourself, then as Dan Mašek's comment suggested , you can use a different constructor for cv::Mat :

#include <cstdint>

uint16_t* m_host_Rgb = new uint16_t[1824 * 948 * 3];
cv::Mat m_RGBMatData(1824, 948, CV_16UC3, (void*) m_host_Rgb);

Even after m_RGBMatData goes out of scope, you can still use m_host_Rgb , because you are managing its memory yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM