# DeepStream get cv::mat wrong

**URL:** <https://forums.developer.nvidia.com/t/deepstream-get-cv-mat-wrong/128433>\
**Category:** DeepStream SDK\
**Created:** [June 18, 2020, 9:19am UTC](https://forums.developer.nvidia.com/t/deepstream-get-cv-mat-wrong/128433 "2020-06-18T09:19:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RayZhang](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/rayzhang/32/22394_2.png) [@RayZhang](https://forums.developer.nvidia.com/u/RayZhang)\
**Post date:** [June 18, 2020, 9:19am UTC](https://forums.developer.nvidia.com/t/deepstream-get-cv-mat-wrong/128433/1 "2020-06-18T09:19:46Z")

</div>

CODE:(in osd sink probe)

```
if(1)
{
    guint idx = 0;
    GstMapInfo in_map_info;
    NvBufSurface *surface = NULL, ip_surf;
    memset (&in_map_info, 0, sizeof (in_map_info));
    if (!gst_buffer_map (buf, &in_map_info, GST_MAP_READ))
    {
      g_print ("Error: Failed to map gst buffer\n");
      gst_buffer_unmap (buf, &in_map_info);
      return GST_FLOW_ERROR;
    }

    cudaError_t cuda_err;

    NvBufSurfTransformRect src_rect, dst_rect;
    surface = (NvBufSurface *) in_map_info.data;

    ip_surf = *surface;

    ip_surf.numFilled = ip_surf.batchSize = 1;
    ip_surf.surfaceList = &(surface->surfaceList[idx]);

    int batch_size= surface->batchSize;
    // g_print("\nBatch Size : %d, resolution : %dx%d \n",batch_size,
    // surface->surfaceList[idx].width, surface->surfaceList[idx].height);

    src_rect.top = 0;
    src_rect.left = 0;
    src_rect.width = (guint) surface->surfaceList[idx].width;
    src_rect.height= (guint) surface->surfaceList[idx].height;

    dst_rect.top = 0;
    dst_rect.left = 0;
    dst_rect.width = (guint) surface->surfaceList[idx].width;
    dst_rect.height= (guint) surface->surfaceList[idx].height;

    NvBufSurfTransformParams nvbufsurface_params;
    nvbufsurface_params.src_rect = &src_rect;
    nvbufsurface_params.dst_rect = &dst_rect;
    nvbufsurface_params.transform_flag = NVBUFSURF_TRANSFORM_CROP_SRC | NVBUFSURF_TRANSFORM_CROP_DST;
    nvbufsurface_params.transform_filter = NvBufSurfTransformInter_Default;

    NvBufSurface *dst_surface = NULL;
    NvBufSurfaceCreateParams nvbufsurface_create_params;

    /* An intermediate buffer for NV12/RGBA to BGR conversion will be
     * required. Can be skipped if custom algorithm can work directly on NV12/RGBA. */
    nvbufsurface_create_params.gpuId = surface->gpuId;
    nvbufsurface_create_params.width = (gint) surface->surfaceList[idx].width;
    nvbufsurface_create_params.height = (gint) surface->surfaceList[idx].height;
    nvbufsurface_create_params.size = 0;
    nvbufsurface_create_params.colorFormat = NVBUF_COLOR_FORMAT_RGBA;
    nvbufsurface_create_params.layout = NVBUF_LAYOUT_PITCH;
    nvbufsurface_create_params.memType = NVBUF_MEM_DEFAULT;

    cuda_err = cudaSetDevice (surface->gpuId);

    cudaStream_t cuda_stream;

    cuda_err=cudaStreamCreate (&cuda_stream);

    int create_result = NvBufSurfaceCreate(&dst_surface,1,&nvbufsurface_create_params);	

    NvBufSurfTransformConfigParams transform_config_params;
    NvBufSurfTransform_Error err;

    transform_config_params.compute_mode = NvBufSurfTransformCompute_Default;
    transform_config_params.gpu_id = surface->gpuId;
    transform_config_params.cuda_stream = cuda_stream;
    err = NvBufSurfTransformSetSessionParams (&transform_config_params);

    NvBufSurfaceMemSet (dst_surface, 0, 0, 0);
    err = NvBufSurfTransform (&ip_surf, dst_surface, &nvbufsurface_params);
    if (err != NvBufSurfTransformError_Success)
    {
  	  g_print ("NvBufSurfTransform failed with error %d while converting buffer\n", err);
    }
    NvBufSurfaceMap (dst_surface, 0, 0, NVBUF_MAP_READ); //FIRST
    NvBufSurfaceSyncForCpu (dst_surface, 0, 0);

    cv::Mat bgr_frame = cv::Mat (cv::Size(nvbufsurface_create_params.width, nvbufsurface_create_params.height), CV_8UC3);

    cv::Mat in_mat = // SECOND
        cv::Mat (nvbufsurface_create_params.height, nvbufsurface_create_params.width,
        CV_8UC4, dst_surface->surfaceList[idx].mappedAddr.addr[0],
        dst_surface->surfaceList[idx].pitch);

    cv::cvtColor (in_mat, bgr_frame, cv::COLOR_RGBA2BGR);
    cv::imencode(".jpg", bgr_frame, data_encode);
    NvBufSurfaceUnMap (dst_surface, 0, 0);
    NvBufSurfaceDestroy (dst_surface);
    cudaStreamDestroy (cuda_stream);
    gst_buffer_unmap (buf, &in_map_info);
    if(self->asyncSend)
    {
      self->threadPool->submit([=](){
      std::string str(data_encode.begin(),data_encode.end());
      self->alarm.execute(str,root);
      });
    }
    else
    {
      std::string str(data_encode.begin(),data_encode.end());
      self->alarm.execute(str,root);
    }
  }

```

**it will crash in 1080Ti , but it works well in NX. why?**  
log is :  
nvbufsurface: mapping of memory type (0) not supported \*\*by FIRST in CODE  
OpenCV Error: Assertion failed (total() == 0 || data != NULL) in Mat, file \*\*by SECOND in CODE  
/usr/include/opencv2/core/mat.inl.hpp, line 431  
terminate called after throwing an instance of ‘cv::Exception’  
what(): /usr/include/opencv2/core/mat.inl.hpp:431: error: (-215) total() == 0 || data != NULL in function Mat

**• Hardware Platform (Jetson / GPU)** 1080Ti  
**• DeepStream Version** 5.0dp  
**• JetPack Version (valid for Jetson only)**  
**• TensorRT Version**  
**• NVIDIA GPU Driver Version (valid for GPU only)**

---

<div class="post-metadata">

**Author:** ![mchi](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/mchi/32/14043_2.png) [@mchi](https://forums.developer.nvidia.com/u/mchi)\
**Post date:** [June 19, 2020, 12:53am UTC](https://forums.developer.nvidia.com/t/deepstream-get-cv-mat-wrong/128433/3 "2020-06-19T00:53:03Z")

</div>

do you run deepstream in docker? which docker image?  
Is the opencv installed by yourself? how?

can you narrow down which code cause the crash?

---

<div class="post-metadata">

**Author:** ![RayZhang](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/rayzhang/32/22394_2.png) [@RayZhang](https://forums.developer.nvidia.com/u/RayZhang)\
**Post date:** [June 19, 2020, 1:08am UTC](https://forums.developer.nvidia.com/t/deepstream-get-cv-mat-wrong/128433/4 "2020-06-19T01:08:46Z")

</div>

1.Yes ,image is [nvcr.io/nvidia/deepstream:5.0-dp-20.04-devel](http://nvcr.io/nvidia/deepstream:5.0-dp-20.04-devel) .  
2.i don`t install opencv  
3. NvBufSurfaceMap (dst\_surface, 0, 0, NVBUF\_MAP\_READ); log :nvbufsurface: mapping of memory type (0) not supported  
cv::Mat in\_mat = cv::Mat (nvbufsurface\_create\_params.height, nvbufsurface\_create\_params.width, CV\_8UC4, dst\_surface-\>surfaceList[idx].mappedAddr.addr[0], dst\_surface-\>surfaceList[idx].pitch); log:OpenCV Error …

---

<div class="post-metadata">

**Author:** ![RayZhang](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/rayzhang/32/22394_2.png) [@RayZhang](https://forums.developer.nvidia.com/u/RayZhang)\
**Post date:** [June 19, 2020, 2:15am UTC](https://forums.developer.nvidia.com/t/deepstream-get-cv-mat-wrong/128433/5 "2020-06-19T02:15:23Z")

</div>

I have solved this problem by referring to the gst-dsexample

---

<div class="post-metadata">

**Author:** ![mchi](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/mchi/32/14043_2.png) [@mchi](https://forums.developer.nvidia.com/u/mchi)\
**Post date:** [June 19, 2020, 2:18am UTC](https://forums.developer.nvidia.com/t/deepstream-get-cv-mat-wrong/128433/6 "2020-06-19T02:18:52Z")

</div>

Glad to know that, thanks!

---

<div class="post-metadata">

**Author:** ![TomNVIDIA](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/tomnvidia/32/14181_2.png) [@TomNVIDIA](https://forums.developer.nvidia.com/u/TomNVIDIA)\
**Post date:** [October 12, 2021, 4:17pm UTC](https://forums.developer.nvidia.com/t/deepstream-get-cv-mat-wrong/128433/8 "2021-10-12T16:17:34Z")

</div>

Closed on October 12, 2021, 4:17pm UTC


