How to create opencv gpumat from nvstream?

Hi,
Please apply below code to get_converted_mat() in gstdsexample.cpp

#include <cudaEGL.h>
#include <opencv2/cudafilters.hpp>
#ifdef __aarch64__
  // To use the converted buffer in CUDA, create an EGLImage and then use
  // CUDA-EGL interop APIs
  if (USE_EGLIMAGE) {
    if (NvBufSurfaceMapEglImage (dsexample->inter_buf, 0) !=0 ) {
      goto error;
    }

    // dsexample->inter_buf->surfaceList[0].mappedAddr.eglImage
    // Use interop APIs cuGraphicsEGLRegisterImage and
    // cuGraphicsResourceGetMappedEglFrame to access the buffer in CUDA
[b]#if 1
    static bool create_filter = true;
    static cv::Ptr< cv::cuda::Filter > filter;
    CUresult status;
    CUeglFrame eglFrame;
    CUgraphicsResource pResource = NULL;
    cudaFree(0);
    status = cuGraphicsEGLRegisterImage(&pResource,
		dsexample->inter_buf->surfaceList[0].mappedAddr.eglImage,
                CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE);
    status = cuGraphicsResourceGetMappedEglFrame(&eglFrame, pResource, 0, 0);
    status = cuCtxSynchronize();
    if (create_filter) {
        filter = cv::cuda::createSobelFilter(CV_8UC4, CV_8UC4, 1, 0, 3, 1, cv::BORDER_DEFAULT);
        //filter = cv::cuda::createGaussianFilter(CV_8UC4, CV_8UC4, cv::Size(31,31), 0, 0, cv::BORDER_DEFAULT);
        create_filter = false;
    }
    cv::cuda::GpuMat d_mat(dsexample->processing_height, dsexample->processing_width, CV_8UC4, eglFrame.frame.pPitch[0]);
    filter->apply (d_mat, d_mat);
    status = cuCtxSynchronize();
    status = cuGraphicsUnregisterResource(pResource);

    // apply back to the original buffer
    transform_params.src_rect = &dst_rect;
    transform_params.dst_rect = &src_rect;
    NvBufSurfTransform (dsexample->inter_buf, &ip_surf, &transform_params);
#endif[/b]
    // Destroy the EGLImage
    NvBufSurfaceUnMapEglImage (dsexample->inter_buf, 0);
  }
#endif

Makefile

# Remove opencv in PKGS
PKGS:= gstreamer-1.0 gstreamer-base-1.0 gstreamer-video-1.0 <s>opencv</s>
# Add opencv4 to CFLAGS and LIBS
CFLAGS+= -I /usr/local/include/opencv4
LIBS+=-L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_videoio -lopencv_cudafilters

Enable dsexample in config file:

[ds-example]
enable=1
processing-width=640
processing-height=480
full-frame=1
unique-id=15
gpu-id=0
2 Likes