Create cv::Mat from nvBufferMem cause Segmentation fault (core dump)

Hello, I am testing nvBuffer and OpenCV data Transfer by using examples from camera_v4l2_cuda. My camera is a USB MJPEG. At the first step, I successfully ran the original code of camera_v4l2_cuda. After that, I modified the code to create cv::Mat from nvBuffer. It cause a Segmentation fault (core dump) when I ran it. Could you advise what should I do?

void *pdata = NULL;
NvBufferParams params;
NvBufferGetParams(ctx->render_dmabuf_fd, &params);
NvBufferMemMap(ctx->render_dmabuf_fd, 0, NvBufferMem_Read, &pdata);
NvBufferMemSyncForCpu(ctx->render_dmabuf_fd, 0, &pdata);
cv::Mat picYV = cv::Mat(ctx->cam_h, ctx->cam_w, CV_8UC4, pdata);
cv::Mat picBGR;
cv::cvtColor(picYV, picBGR, cv::COLOR_RGBA2BGR); // albo COLOR_YUV2BGRA_Y422
NvBufferMemUnMap(ctx->render_dmabuf_fd, 0, &pdata);
cv::imshow(img, picBGR);
int key = cv::waitKey(1);

Sorry for the late response, our team will do the investigation and provide suggestions soon. Thanks

Hi,
There is a patch based on 13_multi_camera:
NVBuffer (FD) to opencv Mat - #6 by DaneLLL

Please refer to it and apply to 12_camera_v4l2_cuda

Thank you for advise, the problem was solved. Below is my late code.
/* transfer mmap to opencv Mat */
NvBufferParams params;
void *pdata = NULL;
NvBufferGetParams(ctx->render_dmabuf_fd, &params);
NvBufferMemMap(ctx->render_dmabuf_fd, 0, NvBufferMem_Read_Write, &pdata);
NvBufferMemSyncForCpu(ctx->render_dmabuf_fd, 0, &pdata);
//
// put data into matprocess queue.
cv::Mat picBGR = cv::Mat(ctx->cam_h, ctx->cam_w, CV_8UC4, pdata);
cv::Mat matIn = picBGR.clone();
//
// Unmap hardware buffer, and put buffer back to video queue.
NvBufferMemUnMap(ctx->render_dmabuf_fd, 0, &pdata); // ← must unmap after opencv display.

and I did some patches in function prepare_buffers_mjpeg, I used NvBufferColorFormat_ARGB32.

input_params.colorFormat= NvBufferColorFormat_ARGB32 ;

above code is work fine with my camera ELP MJPEG 260fps @640x360.

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