I get image from NvBufSurface but i cvtColor NV12 to RGB Error. why?

static GstPadProbeReturn
nvvidconvert2_sink_pad_buffer_probe (GstPad * pad, GstPadProbeInfo * info,
gpointer u_data)
GstBuffer *buf = (GstBuffer *)info->data;
NvDsMetaList * l_frame = NULL;
NvDsUserMeta *user_meta = NULL;
NvDsMetaList * l_user_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);
    GstMapInfo in_map_info;
    memset(&in_map_info, 0, sizeof(in_map_info));
    if (!gst_buffer_map(buf, &in_map_info, GST_MAP_READ)) {
        g_print("buffer map to be accessed by CPU failed");
    NvBufSurface *surface = (NvBufSurface *)in_map_info.data;
    if (surface->surfaceList[0].mappedAddr.addr[0] == NULL) {
        if (NvBufSurfaceMap(surface, -1, -1, NVBUF_MAP_READ) != 0) {
            g_print("buffer map to be accessed by CPU failed");
    NvBufSurfaceSyncForCpu(surface, -1, -1);

Mat inMat = Mat(surface->surfaceList[frame_meta->batch_id].height,
                surface->surfaceList[frame_meta->batch_id].width, CV_8UC1,

cv::Mat out_mat = cv::Mat(cv::Size(surface->surfaceList[frame_meta->batch_id].width,
                          surface->surfaceList[frame_meta->batch_id].height), CV_8UC3);
cv::cvtColor(inMat, out_mat, COLOR_YUV2BGR_NV12);
cv::imwrite("test.jpg", inMat);

NvBufSurfaceUnMap(surface, 0, 0);
gst_buffer_unmap(buf, &in_map_info);


I get the picture format 31 (NV12_12LE format), then i create Mat in Mat which show gray picture.When I tried to convert the NV12 image into RGB, I found that the image had bright green and purple colors. Could you tell me if I made a mistake in transcoding?