使用V4L进行OpenCv捕获

时间:2019-01-15 17:18:18

标签: opencv v4l2

我正在尝试从OV13850捕获帧

但是它显示非常绿色的屏幕。几乎所有的绿色?

有什么主意吗?

gst-camera.sh正常,gst-launch显示相机正常。

但是,当我尝试从下面的代码中抓取时,它们全部变为绿色。

编辑:此处为完整代码

Ubuntu 18 NanopcT4板 OV13850车载摄像头 cpu而不是gpu使用。

#include <opencv2/opencv.hpp>
#include <iostream>
#include <sys/time.h>
#include <cstdlib>
#include "v4l2_helper.h"

using namespace std;
using namespace cv;



int main(int argc, char **argv)
{
    unsigned int width, height;
    static const char* default_videodev = "/dev/video0";
    const char *videodev;
    unsigned int start, end, fps = 0;
    unsigned char* ptr_cam_frame;
    int bytes_used;


    Mat yuyv_frame, preview;


    if (argc == 4) {
        videodev = argv[1];


        string width_str = argv[2];
        string height_str = argv[3];
        try {
            size_t pos;
            width = stoi(width_str, &pos);
            if (pos < width_str.size()) {
                cerr << "Trailing characters after width: " << width_str << '\n';
            }

            height = stoi(height_str, &pos);
            if (pos < height_str.size()) {
                cerr << "Trailing characters after height: " << height_str << '\n';
            }
        } catch (invalid_argument const &ex) {
            cerr << "Invalid width or height\n";
            return EXIT_FAILURE;
        } catch (out_of_range const &ex) {
            cerr << "Width or Height out of range\n";
            return EXIT_FAILURE;
        }
    } else {
        cout << "Note: This program accepts (only) three arguments.\n";
        cout << "First arg: device file path, Second arg: width, Third arg: height\n";
        cout << "No arguments given. Assuming default values.\n";
        cout << "Device file path: " << default_videodev << "; Width: 640; Height: 480\n";
        videodev = default_videodev;
        width = 640;
        height = 480;
    }


    if (helper_init_cam(videodev, width, height, V4L2_PIX_FMT_UYVY, IO_METHOD_USERPTR) < 0) {
        return EXIT_FAILURE;
    }

#ifdef ENABLE_DISPLAY

    #ifdef ENABLE_GL_DISPLAY
    namedWindow("OpenCV V4L2", CV_WINDOW_OPENGL);
    #else
    namedWindow("OpenCV V4L2");
    #endif
    cout << "Note: Click 'Esc' key to exit the window.\n";
#endif


    yuyv_frame = Mat(height, width, CV_8UC2);

    while(1) {

        if (helper_get_cam_frame(&ptr_cam_frame, &bytes_used) < 0) {
            break;
        }


        yuyv_frame.data = ptr_cam_frame;
        if(yuyv_frame.empty()) {
            cout << "Img load failed" << endl;
            break;
        }


        cvtColor(yuyv_frame, preview, COLOR_YUV2BGR_UYVY);

#ifdef ENABLE_DISPLAY

    #if (defined ENABLE_GL_DISPLAY) && (defined ENABLE_GPU_UPLOAD)

        gpu_frame.upload(preview);
        imshow("OpenCV V4L2", gpu_frame);
    #else
        imshow("OpenCV V4L2", preview);
    #endif
#endif


        if (helper_release_cam_frame() < 0)
        {
            break;
        }

#ifdef ENABLE_DISPLAY
        if(waitKey(1) == 27) break;
#endif

        fps++;
        end = GetTickCount();
        if ((end - start) >= 1000) {
            cout << "fps = " << fps << endl ;
            fps = 0;
            start = end;
        }


    }


    if (helper_deinit_cam() < 0)
    {
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

EDIT-2

:v4l2-ctl -d / dev / video0 --list-formats的输出

ioctl:VIDIOC_ENUM_FMT     索引:0     类型:视频捕获     像素格式:“ YUYV”     名称:YUYV 4:2:2

Index       : 1
Type        : Video Capture
Pixel Format: 'YUYV'
Name        : YUYV 4:2:2

Index       : 2
Type        : Video Capture
Pixel Format: 'UYVY'
Name        : UYVY 4:2:2

Index       : 3
Type        : Video Capture
Pixel Format: '422P'
Name        : Planar YVU 4:2:2

Index       : 4
Type        : Video Capture
Pixel Format: 'NV16'
Name        : Y/CbCr 4:2:2

Index       : 5
Type        : Video Capture
Pixel Format: 'YU12'
Name        : Planar YUV 4:2:0

Index       : 6
Type        : Video Capture
Pixel Format: 'YU12'
Name        : Planar YUV 4:2:0

Index       : 7
Type        : Video Capture
Pixel Format: 'YV12'
Name        : Planar YVU 4:2:0

Index       : 8
Type        : Video Capture
Pixel Format: 'NV12'
Name        : Y/CbCr 4:2:0

Index       : 9
Type        : Video Capture
Pixel Format: 'NV21'
Name        : Y/CrCb 4:2:0

Index       : 10
Type        : Video Capture
Pixel Format: 'GREY'
Name        : 8-bit Greyscale

Index       : 11
Type        : Video Capture
Pixel Format: 'Y444'
Name        : 16-bit A/XYUV 4-4-4-4

Index       : 12
Type        : Video Capture
Pixel Format: 'NV24'
Name        : Y/CbCr 4:4:4

Index       : 13
Type        : Video Capture
Pixel Format: 'JPEG' (compressed)
Name        : JFIF JPEG

Index       : 14
Type        : Video Capture
Pixel Format: 'RGBP'
Name        : 16-bit RGB 5-6-5

Index       : 15
Type        : Video Capture
Pixel Format: 'GRBG'
Name        : 8-bit Bayer GRGR/BGBG

0 个答案:

没有答案