I have a question about saving deepstream nv12 data as iamge via opencv

static GstPadProbeReturn
tiler_src_pad_buffer_probe (GstPad * pad, GstPadProbeInfo * info,
    gpointer u_data)
{
    GstBuffer *buf = (GstBuffer *)info->data;
    GstMapInfo in_map_info;
    NvBufSurface *surface = NULL;
    guint num_rects = 0;
    NvDsObjectMeta *obj_meta = NULL;
    guint vehicle_count = 0;
    guint person_count = 0;
    NvDsMetaList * l_frame = NULL;
    NvDsMetaList * l_obj = NULL;
    //NvDsDisplayMeta *display_meta = NULL;
    NvDsBatchMeta *batch_meta = gst_buffer_get_nvds_batch_meta (buf);

    for (l_frame = batch_meta->frame_meta_list; l_frame != NULL;
        l_frame = l_frame->next) 
    {
        NvDsFrameMeta *frame_meta = (NvDsFrameMeta *) (l_frame->data);
        if (!gst_buffer_map (buf, &in_map_info, GST_MAP_READ))
        {
            g_print ("gst_buffer_map() error!");
            gst_buffer_unmap(buf, &in_map_info);
            return (GstPadProbeReturn)NULL;
        }
        surface =  (NvBufSurface *) in_map_info.data;
        char *src_data = (char*)malloc(surface->surfaceList[frame_meta->batch_id].dataSize);
        cudaMemcpy((void *)src_data,
                     (void *)surface->surfaceList[frame_meta->batch_id].dataPtr,
                     surface->surfaceList[frame_meta->batch_id].dataSize,
                     cudaMemcpyDeviceToHost);
        gint frame_width = (gint)surface->surfaceList[frame_meta->batch_id].width;
        gint frame_height = (gint)surface->surfaceList[frame_meta->batch_id].height;
        size_t frame_step = surface->surfaceList[frame_meta->batch_id].pitch;

        cv::Mat frame = cv::Mat(frame_height*3/2 , frame_width, CV_8UC1, src_data, frame_step);
        cv::Mat src_mat_BGRA;
        cv::cvtColor(frame, src_mat_BGRA, COLOR::CV_YUV2BGR_NV12);
        char filename[200];
        printf("frame number:%d\n", frame_num);
        sprintf(filename,"./images/file_%dx%d_%d_%.5d.jpg",frame_width,frame_height, frame_meta->source_id, frame_num);
        frame_num++;
        cv::imwrite(filename, src_mat_BGRA);
        free(src_data);
        gst_buffer_unmap (buf, &in_map_info);
    }
    return GST_PAD_PROBE_OK;
}

The picture cannot be saved properly, Carry out cv::Mat frame = cv::Mat(frame_height*3/2 , frame_width, CV_8UC1, src_data, frame_step);
Prompt error

Please provide complete information as applicable to your setup. Thanks
Hardware Platform (Jetson / GPU)
DeepStream Version
JetPack Version (valid for Jetson only)
TensorRT Version
NVIDIA GPU Driver Version (valid for GPU only)
Issue Type( questions, new requirements, bugs)
How to reproduce the issue ? (This is for bugs. Including which sample app is using, the configuration files content, the command line used and other details for reproducing)
Requirement details( This is for new requirement. Including the module name-for which plugin or for which sample application, the function description)

Thank you very much for the reply。
Hardware Platform is GPU.
CUDA 11.4.
opencv 4.5.
deepstream.6.3.
This problem has plagued me for a long time

There might be something wrong with the frame_step parameter between the DeepStream and OpenCV. Could you refer to 292824 to debug the value of the frame_step?

1 Like

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