Reputation: 79
I was looking for a way to initialize a cv::Umat
with the data of an already allocated GPU-Buffer (from OpenCL, Cuda or OpenGL) without having to copy the data back to the CPU side.
Ideally, no copy operation is involved and the UMat would "wrap" around the already existing data on the GPU (which was previously allocated using CUDA/OpenCL/OpenGL). If that does not work, it would also be acceptable to copy an OpenCL/CUDA Buffer directly on the GPU into an UMat
without transfering the data back to the host side (since the copy operation on the GPU device is much faster than on the CPU side).
The OpenCV API describes how to get an OpenCL handle to the data of an existing UMat
, but not the other way round. Anybody who already did this or has an idea how to get it done? Thanks!
Upvotes: 1
Views: 913
Reputation: 333
I'm also looking for the same answer, I have something that might work, however setting them all up may prove challenging... From (opencl-opencv-interop.cpp):
// this function is an example of interoperability between OpenCL buffer
// and OpenCV UMat objects. It converts (without copying data) OpenCL buffer
// to OpenCV UMat and then do blur on these data
int App::process_cl_buffer_with_opencv(cl_mem buffer, size_t step, int rows, int cols, int type, cv::UMat& u)
{
cv::ocl::convertFromBuffer(buffer, step, rows, cols, type, u);
// process right half of frame in OpenCV
cv::Point pt(u.cols / 2, 0);
cv::Size sz(u.cols / 2, u.rows);
cv::Rect roi(pt, sz);
cv::UMat uroi(u, roi);
cv::blur(uroi, uroi, cv::Size(7, 7), cv::Point(-3, -3));
if (buffer)
clReleaseMemObject(buffer);
m_mem_obj = 0;
return 0;
}
You should be able to convert to a CL buffer from GL, or use a CL buffer directly and then use the function above or the convertFromBuffer()
.
Upvotes: 2