Is there a way to read an image into an allocated space of memory?

In my programs so far I have been using

cv::Mat h_image = cv::imread(dirname+image_filenames[ni], cv::IMREAD_GRAYSCALE);

However, (correct me if I am wrong) I suspect that imread allocates space in the memory and put the image here.

I need now to read an image into an already allocated space of memory

void *unified_ptr;
cudaMallocManaged(&unified_ptr, frameByteSize);
cv::Mat h_image(height, width, CV_8UC1, unified_ptr);

Is there a way that I can read an image into this h_image variable?

You can try this patch https://github.com/opencv/opencv/pull/17753

I have no idea about cuda but it allows loading image into previously created Mat if the dimensions of the Mat and image dims are the same.

1 Like

you can do

// ...your h_image setup here...

cv::Mat some_image = cv::imread(...);

some_image.copyTo(h_image);