Getting strange cv::Mat from NvBufSurface

• Jetson AGX Xavier
• Deepstream 6.0
• JetPack 4.6
• TensorRT 8.0.1
• NVIDIA GPU Driver 32.6.1

Hi, I’m trying to get cv:Mat image from NvBufSurface and save it to file, but got image like this:
image

I getting Surface in analytics_done_buf_prob() function with this code:

GstMapInfo in_map_info;

    GstMapInfo inmap = GST_MAP_INFO_INIT;
    if (!gst_buffer_map (buf, &inmap, GST_MAP_READ)) {
        GST_ERROR ("input buffer mapinfo failed");
        gst_buffer_unmap (buf, &in_map_info);
    }
    NvBufSurface *surface = (NvBufSurface *) inmap.data;

    for (NvDsMetaList *l_frame = batch_meta->frame_meta_list; l_frame != NULL; l_frame = l_frame->next) {
        NvDsFrameMeta *frame_meta = (NvDsFrameMeta *)l_frame->data;

        if (surface->surfaceList[frame_meta->batch_id].mappedAddr.addr[0] == NULL){

            if (NvBufSurfaceMap(surface, frame_meta->batch_id, 0, NVBUF_MAP_READ) != 0)
            {
            std::cout << "Faild to map the surface buffer\n";
            }
        }
        NvBufSurfaceSyncForCpu (surface, 0, 0);

        guint height = surface->surfaceList[frame_meta->batch_id].height;
        guint width = surface->surfaceList[frame_meta->batch_id].width;
        Mat nv12_mat = Mat(height, width, CV_8UC1, surface->surfaceList[frame_meta->batch_id].mappedAddr.addr[0],
            surface->surfaceList[frame_meta->batch_id].pitch);

        Mat rgba_mat;
        cvtColor(nv12_mat, rgba_mat, CV_YUV2BGRA_NV12);
        imwrite("Mat.jpg", rgba_mat);
        NvBufSurfaceUnMap (surface, frame_meta->batch_id, 0);
        guint i = 0;
        for (NvDsMetaList *l_obj = frame_meta->obj_meta_list; l_obj != NULL; l_obj = l_obj->next) {
            NvDsObjectMeta *obj = (NvDsObjectMeta *) l_obj->data;
            std::string str = appCtx->predNames[i];
            std::cout<<obj->class_id<<std::endl;
            
            /*Init curl for POST-request*/
            CURL *curl;
            CURLcode res;
            std::string readBuffer;

            curl = curl_easy_init();
            std::cout<<curl<<std::endl;
            if (obj->class_id == 2){
                str = "Unmasked |" + str;
                if(curl) {
                    curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.253.62:8000/");
                    curl_easy_setopt(curl, CURLOPT_POST, 1);
                    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "Id=" + appCtx->predNames[i] + "&cam=1");
                    res = curl_easy_perform(curl);
                    curl_easy_cleanup(curl);
                }
            }
    }

Can you try CV_8UC4 when creating the nv12_mat from surface?
CV_8UC1 keeps only one channel and it may be the rootcause of wrong image.

It seems that the UV channel is not right. May be you can refer the sample code:

/opt/nvidia/deepstream/deepstream-XX/sources/gst-plugins/gst-dsexample/gstdsexample.cpp

Thanks