Ivan
Ivan

Reputation: 1455

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!

Upvotes: 0

Views: 761

Answers (1)

MassPikeMike
MassPikeMike

Reputation: 682

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.

Upvotes: 2

Related Questions