opencv2:读入摄像机视频并写入AVI视频文件

首先用opencv 来显示一段视频,视频是提取成图片帧来播放的。

opencv读入一段视频
#include <opencv2/opencv.hpp>  

using namespace cv; 

int main(int argc, char** argv){
    cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);
    CvCapture* capture = cvCreateFileCapture("Bye.mp4");
    IplImage* frame;
    while(1){
        frame = cvQueryFrame(capture);
        if(!frame) break;
        cvShowImage("Example2", frame);
        char c = cvWaitKey(33);
        if(c == 27) break;
    }
    cvReleaseCapture(&capture);
    cvDestroyWindow("Example2");
}

显示结果:
这里写图片描述

添加一个滚动条,但没有滚动条随代码播放移动功能。

opencv输出带有滚动条的视频播放
#include <opencv2/opencv.hpp>  

using namespace cv; 

int g_slider_position = 0;
CvCapture* g_capture = NULL;

void onTrackbarSlide(int pos){
    cvSetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES, pos);
}

int main(int argc, char** argv){
    cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);
    g_capture = cvCreateFileCapture("Bye.mp4");
    int frames = (int) cvGetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_COUNT); //obtain the total frame number of video

    if(frames!=0){
        cvCreateTrackbar("Position", "Example2", &g_slider_position, frames, onTrackbarSlide);
    }

    CvCapture* capture = cvCreateFileCapture("Bye.mp4");
    IplImage* frame;
    while(1){
        frame = cvQueryFrame(capture);
        if(!frame) break;
        cvShowImage("Example2", frame);
        char c = cvWaitKey(33);
        if(c == 27) break;
    }
    cvReleaseCapture(&capture);
    cvDestroyWindow("Example2");

    return 0;
}

显示结果:
这里写图片描述

opencv从摄像机读入数据并写入AVI视频文件
#include <opencv2/opencv.hpp>  

using namespace cv; 
using namespace std;

IplImage* in = NULL;
IplImage* out = NULL;
const char * InputTitle = "Input Video";
const char * OutputTitle = "Output Video";

int main(int argc, char** argv){
    cvNamedWindow(InputTitle, CV_WINDOW_AUTOSIZE);
    cvNamedWindow(OutputTitle, CV_WINDOW_AUTOSIZE);

    CvCapture* capture;
    if(argc == 1){
        capture = cvCreateCameraCapture(0);
    }else{
        capture = cvCreateFileCapture("Bye.mp4");
    }
    assert(capture != NULL);


    double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
    CvSize size = cvSize((int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));
    CvVideoWriter* writer = cvCreateVideoWriter("shuchu2.avi", CV_FOURCC('M', 'J', 'P', 'S'), fps, size);

    // show 
    IplImage* in_frame;
    IplImage* out_frame  = cvCreateImage(size, IPL_DEPTH_8U, 1);
    while(in_frame = cvQueryFrame(capture)){
        cvCvtColor(in_frame, out_frame, CV_BGR2GRAY);
        cvWriteFrame(writer, in_frame);

        cvShowImage(InputTitle, in_frame);
        cvShowImage(OutputTitle, out_frame);
        char c = cvWaitKey(33);
        if(c == 27) break;
    }

    waitKey(0);
    cvReleaseVideoWriter(&writer);
    cvReleaseCapture(&capture);
    cvDestroyWindow(InputTitle);
    cvDestroyWindow(OutputTitle);

    return 0;
}

显示结果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mingo_敏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值