JPEG encoding of cv::Mat

I would like to use the class NvJPEGEncoder from the Multimedia API to compress an Opencv image.

This class has the methods:


Does anyone know how to convert a cv::Mat to the corresponding nvBuffer, apply the encoding and re-convert the result back to opencv again?

Best regards!

JPEGEncoder can only take Nvbuffer or dma_buffer as input.

As a result, you need to use mmap to create a pointer that can sync with your Nvbuffer or dam_buffer fd. Copy you Mat to that memory (call NvBufferMemSyncForDevice for syncing) and then use encoder.

Thank you very much for your fast answer! I know the types that JPEGEncoder accept, but I can’t find an example to convert from cv::Mat to NvBuffer.

I found this example that converts fd to cv::Mat (, but I need the opposite conversion…

Can you help me with a simple example?

Thank you very much!


What I told in #2 is the way to copy Mat to Nvbuffer. Please refer to mmapi sample 12_camera_v4l2_cuda.

Though I don’t think this one is easier to understand for your case.

I just address it again. You need to call mmap for your NvBuffer so that there is a cpu pointer. After that, copy your Mat content into that memory. Call NvBufferMemSyncForDevice, so that cpu memory is truly sync with gpu memory. Use that Nvbuffer to jpeg encoder.