need to encode directly from OpenCV::cuda::GpuMat

I want to compress the contents of an OpenCV::cuda::GpuMat to H264. I don’t know much about OpenCV’s cuda::GpuMat class, but I know it allocates and uses cuda GPU memory buffers, and you can do all sorts of nifty cuda operations on them. I’d like to compress from one of these buffers to H264, but when I try to “map” or “register” the GpuMat to allow the encoder to use it directly, I get lost, and nothing seems to work. Calling NvEncRegisterResource( ), passing in NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR gives a return code of “device missing” or something like that. I don’t know what a “CudaDevicePtr” is, but I would guess it’s a pointer to cuda memory. But then, maybe not, the docs don’t specify.

One of the things that annoys me about OpenCV’s cuda::GpuMat is that it’s nothing more than a Gpu byte *, you can’t tell anything else about the class, as far as I can tell. I don’t know which video card it was allocated upon, or any other information about it at all.

Also, OpenCV allows one to “map” a DirectX texture to a “cudaGraphicsResource”, but I can’t figure out what a “cudaGraphicsResource” is useful for, or what it’s good for. My idea would be to try to find a way to allocate a DirectX texture, get a pointer to its Gpu bits on the NVidia card (by mapping/registering it with cuda?), then ALSO register this buffer with the encoder SDK. But, I have no idea how to create an OpenCV GpuMat on that buffer/texture so I can do the pre-encode buffer operations in OpenCV/Cuda! This is a mess.

can anybody offer some advice? I know this is poorly described.

LOVE the abundance of help on this one, the lengthy replies, the community effort… Thanks!


I am looking for a technical resource that can assist you.
Thanks for your patience.



The person that can best assist you is currently out of the office. He is aware of this forum topic and will get back to you as soon as he returns.



I am facing same issue and I want to compress the contents of an OpenCV::cuda::GpuMat to H265.
Can you please provide API interface for the same?


OpenCV GpuMat implementation uses a CUdeviceptr for its storage. That class has an element data which is a CUdeviceptr, so this can be used directly for registering with NVENCAPI. Is it possible for you to share the problematic sample implementation with us?

Hello Abhijit,

The original poster has vanished, i will continue this thread since it keeps popping up in my google searches.

I have a Camaera and get frames via a cv::cuda::GpuMat.

I want to have a working stream. But i have trouble getting that gpumat into Gstreamer.

here is some pseudo code to see what i have:

cv::cuda::GpuMat frame;
// simplyfied for this example. we get an image into the frame variable
camera.getFrame( &frame);

//currently i am showing the frames on my local screen


I have a working camera feed on my screen. But i wish i could use Gstreamer to make it into a h264 stream. But for this to work i somehow need to plug that GpuMat into gstreamer. How would i do that?