# Getting pointer from NvBuffer

**URL:** <https://forums.developer.nvidia.com/t/getting-pointer-from-nvbuffer/257977>\
**Category:** Jetson Xavier NX\
**Tags:** mmapi\
**Created:** [June 27, 2023, 6:44am UTC](https://forums.developer.nvidia.com/t/getting-pointer-from-nvbuffer/257977 "2023-06-27T06:44:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![borjabasket14](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@borjabasket14](https://forums.developer.nvidia.com/u/borjabasket14)\
**Post date:** [June 27, 2023, 6:44am UTC](https://forums.developer.nvidia.com/t/getting-pointer-from-nvbuffer/257977/1 "2023-06-27T06:44:21Z")

</div>

Hello,

I am developing an application to do some video processing. For the use case, I am grabbing a video stream using the V4l2 direct interface (example 12\_camera\_v4l2\_cuda).

In the end, my goal is to get a pointer in order to do some more processing with opencv and avoid memory copies.

The output of that example I mentioned is a NvBuffer file descriptor. What is the best way to get a pointer out of that? Is this the best way to get the best performance?

Thanks

---

<div class="post-metadata">

**Author:** ![DaneLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/danelll/32/14043_2.png) [@DaneLLL](https://forums.developer.nvidia.com/u/DaneLLL)\
**Post date:** [June 27, 2023, 9:21am UTC](https://forums.developer.nvidia.com/t/getting-pointer-from-nvbuffer/257977/3 "2023-06-27T09:21:14Z")

</div>

Hi,  
Please refer to the topics for aceesing the buffer in `cv::Mat` or `cv::gpu::gpuMat`:  
[NVBuffer (FD) to opencv Mat - #6 by DaneLLL](https://forums.developer.nvidia.com/t/nvbuffer-fd-to-opencv-mat/83012/6)  
[LibArgus EGLStream to nvivafilter - #14 by DaneLLL](https://forums.developer.nvidia.com/t/libargus-eglstream-to-nvivafilter/70627/14)  
[Real-time CLAHE processing of video, framerate issue. Gstreamer + nvivafilter + OpenCV - #5 by Honey\_Patouceul](https://forums.developer.nvidia.com/t/real-time-clahe-processing-of-video-framerate-issue-gstreamer-nvivafilter-opencv/155614/5)

---

<div class="post-metadata">

**Author:** ![borjabasket14](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@borjabasket14](https://forums.developer.nvidia.com/u/borjabasket14)\
**Post date:** [June 27, 2023, 9:50am UTC](https://forums.developer.nvidia.com/t/getting-pointer-from-nvbuffer/257977/4 "2023-06-27T09:50:50Z")

</div>

Hi,

Thanks for the reply, first topic is what I was looking for. [NVBuffer (FD) to opencv Mat - #6 by DaneLLL](https://forums.developer.nvidia.com/t/nvbuffer-fd-to-opencv-mat/83012/6)

However, I have a small difference and question. Since I am having a latency critical iamge application, I need to avoid color conversions, and my camera has YUV 422 format. Is there any way to map the buffer without converting it to RGB?

Thanks

---

<div class="post-metadata">

**Author:** ![DaneLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/danelll/32/14043_2.png) [@DaneLLL](https://forums.developer.nvidia.com/u/DaneLLL)\
**Post date:** [June 27, 2023, 10:57am UTC](https://forums.developer.nvidia.com/t/getting-pointer-from-nvbuffer/257977/5 "2023-06-27T10:57:00Z")

</div>

Hi,  
We know that BGR format is common in OpenCV. Not sure about YUV422 formats. If the formats are supported same as BGR, you can try the same to map NvBuffer to `cv::Mat`.

---

<div class="post-metadata">

**Author:** ![borjabasket14](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@borjabasket14](https://forums.developer.nvidia.com/u/borjabasket14)\
**Post date:** [June 29, 2023, 12:09pm UTC](https://forums.developer.nvidia.com/t/getting-pointer-from-nvbuffer/257977/6 "2023-06-29T12:09:55Z")

</div>

Hi,

Thanks for reply. Since I am interested in cv::GpuMat, I have followed the following approach based on the different forums that you have shared:

NvBuffer → EGLImage → EGLFrame → OpenCV Gpu Mat

Here is the example code:

```auto
/* Convert the camera buffer from YUV422 to YUV420P */
            if (-1 == NvBufferTransform(cameraAcquireParams->g_buff[v4l2_buf.index].dmabuff_fd, cameraAcquireParams->render_dmabuf_fd,
                &transParams))
                ERROR_RETURN("Failed to convert the buffer");

            /* Create EGLImage from dmabuf fd */
            cameraAcquireParams->egl_image = NvEGLImageFromFd(cameraAcquireParams->egl_display, cameraAcquireParams->render_dmabuf_fd);
            if (cameraAcquireParams->egl_image == NULL)
                ERROR_RETURN("Failed to map dmabuf fd (0x%X) to EGLImage",
                    cameraAcquireParams->render_dmabuf_fd);

            /* Create EglFrame from EGL Image*/
            CUresult status;
            CUeglFrame myframe;
            CUgraphicsResource pResource = NULL;

            cudaFree(0);
            status = cuGraphicsEGLRegisterImage(&pResource, cameraAcquireParams->egl_image,
                CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE);
            if (status != CUDA_SUCCESS)
            {
                printf("cuGraphicsEGLRegisterImage failed: %d, cuda process stop\n",
                    status);
                break;
            }

            status = cuGraphicsResourceGetMappedEglFrame(&myframe, pResource, 0, 0);
            if (status != CUDA_SUCCESS)
            {
                printf("cuGraphicsSubResourceGetMappedArray failed\n");
                break;
            }

```

My question is, I do not really know if this would be the correct approach for handling the image into a Gpu Mat. Would this be correct? It seems like it is performing copy operations, but I really do not know this.

---

<div class="post-metadata">

**Author:** ![DaneLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/danelll/32/14043_2.png) [@DaneLLL](https://forums.developer.nvidia.com/u/DaneLLL)\
**Post date:** [June 30, 2023, 1:09am UTC](https://forums.developer.nvidia.com/t/getting-pointer-from-nvbuffer/257977/7 "2023-06-30T01:09:26Z")

</div>

Hi,  
The example code should work for RGBA. It is similar to  
[LibArgus EGLStream to nvivafilter - #14 by DaneLLL](https://forums.developer.nvidia.com/t/libargus-eglstream-to-nvivafilter/70627/14)

For YUV422, you would need to try. Not sure if YUV422 is well supported in cv::gpu::gpuMat.

There is format conversion through hardware converter so throughput is good. For optimal performance of hardware converter, please refer to the post for disabling DFS:  
[Nvvideoconvert issue, nvvideoconvert in DS4 is better than Ds5? - #3 by DaneLLL](https://forums.developer.nvidia.com/t/142115/3)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/system/32/68080_2.png) [@system](https://forums.developer.nvidia.com/u/system)\
**Post date:** [July 25, 2023, 7:45am UTC](https://forums.developer.nvidia.com/t/getting-pointer-from-nvbuffer/257977/9 "2023-07-25T07:45:55Z")

</div>

Closed on July 25, 2023, 7:45am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
