V4l2 Multimedia api and opencv

Currently I am using camera v4l2 multimedia api to open a usb camera interface. All I wanted to do is to create a opencv path to display the same content simultaneously on the opencv window using the same camera buffer file. When I tried to implement this I only get a black screen with no error or warning.

static int
stream_opencvFrame(context_t * ctx)
struct pollfd fds[1];
NvBufferTransformParams transParams;
memset(&transParams, 0, sizeof(transParams));
transParams.transform_flag = NVBUFFER_TRANSFORM_FILTER;
transParams.transform_filter = NvBufferTransform_Filter_Smart;
fds[0].fd = ctx->cam_fd;
fds[0].events = POLLIN;

while (poll(fds, 1, 500) > 0 && !quit)
    if (fds[0].revents & POLLIN) {
        struct v4l2_buffer v4l2_buf;

        /* Dequeue a camera buff */
        memset(&v4l2_buf, 0, sizeof(v4l2_buf));
        v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        if (ctx->capture_dmabuf)
            v4l2_buf.memory = V4L2_MEMORY_DMABUF;
            v4l2_buf.memory = V4L2_MEMORY_MMAP;
        if (ioctl(ctx->cam_fd, VIDIOC_DQBUF, &v4l2_buf) < 0)
            ERROR_RETURN("Failed to dequeue camera buff: %s (%d)",
                    strerror(errno), errno);

        /* Save the n-th frame to file */
        if (ctx->frame == ctx->save_n_frame)
            save_frame_to_file(ctx, &v4l2_buf);

        Mat image = imread (ctx->cam_file, IMREAD_COLOR);
        String windowName = "screen"; 

        imshow(windowName, image);
return 0;


Here is my Opencv method which I call before the start capture method. When I try to call this after the start capture method I get segmentation error.

The code which I used is available here

Please refer to this patch:
NVBuffer (FD) to opencv Mat - #6 by DaneLLL
You need to get RGBA NvBuffer, map it to cv::Mat, and convert to BGR.

1 Like

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