opencv自测程序(视频函数)

#include "VideoDemo.h"
#include 

using namespace cv;

VideoDemo::VideoDemo()
{

}

VideoDemo::~VideoDemo()
{

}

/* 打开视频 */
void VideoDemo::test1()
{
    VideoCapture vcHandle = VideoCapture("../x64/Debug/video/112.mp4");
    Mat frame;

    if(!vcHandle.isOpened()) {
        return;
    }

    while(1) {
        vcHandle.read(frame);
        if(frame.empty()) {
            break;
        }

        //namedWindow("1", WINDOW_NORMAL);
        imshow("1", frame);
        char c = waitKey(50);
        if(c == '2') { //键盘响应事件
            break;
        }
    }
    vcHandle.release();
}

//https://blog.csdn.net/Day_upon/article/details/85991445
//以下编码格式在OpenCV4中都可以运行
//CV_FOURCC('M','J','P','G')  注意此种格式保存的视频一直无法打开(在opencv3下)
//CV_FOURCC('P', 'I', 'M', '1') = MPEG - 1 codec(无法打开)(在opencv3下)
//CV_FOURCC('M', 'J', 'P', 'G') = motion - jpeg codec(无法打开)(在opencv3下)
//CV_FOURCC('M', 'P', '4', '2') = MPEG - 4.2 codec(保存的avi可以打开)(在opencv3下)
//CV_FOURCC('D', 'I', 'V', '3') = MPEG - 4.3 codec(保存的avi可以打开)(在opencv3下)
//CV_FOURCC('D', 'I', 'V', 'X') = MPEG - 4 codec(可以打开)(在opencv3下)
//CV_FOURCC('U', '2', '6', '3') = H263 codec(未测试)
//CV_FOURCC('I', '2', '6', '3') = H263I codec(未测试)
//CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec(无法打开)


/* 保存视频 */
void VideoDemo::test2()
{
    VideoCapture vcHandle = VideoCapture("../x64/Debug/video/cork.mp4");
    Mat frame;

    if(!vcHandle.isOpened()) {
        return;
    }

    int frameWithd = vcHandle.get(CAP_PROP_FRAME_WIDTH);
    int frameHeight = vcHandle.get(CAP_PROP_FRAME_HEIGHT);
    int frameCount = vcHandle.get(CAP_PROP_FRAME_COUNT);
    double fps = vcHandle.get(CAP_PROP_FPS);
    int fourcc = vcHandle.get(CAP_PROP_FOURCC);
    std::cout << "[" << frameWithd << "]-" << "[" << frameHeight << "]-"<< "[" << frameCount << "]-"<< "[" << fps << "]-" << std::endl;

    VideoWriter write;
    int fourcc1 = write.fourcc('M', 'J', 'P', 'G'); //('D', 'I', 'V', 'X')可以正常保存mp4
    bool ret = write.open("../x64/Debug/video/cork_1.avi", fourcc1, fps, Size(frameWithd, frameHeight), true);

    while(1) {
        vcHandle >> frame; //>>等同于read
        if(frame.empty()) {
            break;
        }

        write.write(frame);
        char c = waitKey(1);
        if(c == '2') {
            break;
        }
    }

    vcHandle.release();
    write.release();
    std::cout.flush();
}

VideoCapture g_cap;
void CallbackTest3(int pos, void *data) 
{
    g_cap.set(CAP_PROP_POS_FRAMES, pos);
}

/* 拖动视频进度 */
void VideoDemo::test3()
{
    std::string strWin = "video";
    namedWindow(strWin, WINDOW_NORMAL);
    g_cap = VideoCapture("../x64/Debug/video/112.mp4");
    int frames = g_cap.get(CAP_PROP_FRAME_COUNT);
    int tmpW = g_cap.get(CAP_PROP_FRAME_WIDTH);
    int tmpH = g_cap.get(CAP_PROP_FRAME_HEIGHT);

    int CurPos = 0;
    createTrackbar("pos", strWin, &CurPos, frames, CallbackTest3);

    Mat frame;
    while(1) {
        g_cap >> frame;
        if(frame.empty()) {
            char c = waitKey(33);
            continue; //视频结束也不要退出
        }
        int curPos = g_cap.get(CAP_PROP_POS_FRAMES);
        setTrackbarPos("pos", strWin, curPos);
        imshow(strWin, frame);
        
        char c = waitKey(50);
        if(c == '2') {
            break;
        }
    }
}

/*  */
void VideoDemo::test4()
{
    
}

/*  */
void VideoDemo::test5()
{

}

/*  */
void VideoDemo::test6()
{

}

你可能感兴趣的:(opencv,音视频,人工智能)