opencv写视频

opencv中C++写视频的函数如下:

C++: VideoWriter::VideoWriter(const string& filename, int fourcc, double fps, Size frameSize, bool isColor=true)

参数:
filename  - 输出视频文件的名称。
fourcc  - 用于压缩帧的4字符编解码器代码。 例如,CV_FOURCC('P','I','M','1')是MPEG-1编解码器,CV_FOURCC('M','J','P','G')是一个动作 -  jpeg编解码器等。代码列表可以通过FOURCC页面在视频编解码器中获得。
fps  - 创建的视频流的帧率。
frameSize  - 视频帧的大小。
isColor  - 如果它不为零,编码器将对彩色帧进行预期和编码,否则它将与灰度帧一起使用(该标志目前仅在Windows上受支持)。
构造函数/函数初始化视频编写器。 在Linux上,FFMPEG用于编写视频; 在Windows FFMPEG或VFW上使用; 在MacOSX上使用QTKit。

opencv函数介绍见:https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=videowriter#videowriter

上代码:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>  
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
    // 视频读入与输出路径设置
    string sourceVideoPath = "C:\\Users\\Administrator\\Desktop\\飞机.avi";
    string outputVideoPath = "F:\\a1\\xinjian\\P.avi";
    // 视频输入
    VideoCapture inputVideo(sourceVideoPath);
    // 检测视频输入的有效性      
    if (!inputVideo.isOpened())
    {
        cout << "fail to open!" << endl;
        return -1;
    }
    VideoWriter outputVideo;
    // 获取视频分辨率
    cv::Size videoResolution = cv::Size((int)inputVideo.get(
        CV_CAP_PROP_FRAME_WIDTH), (int)inputVideo.get(
        CV_CAP_PROP_FRAME_HEIGHT));
    double fps = inputVideo.get(CV_CAP_PROP_FPS);
    // 获取视频帧率
    cout << "totalFrame:" << inputVideo.get(
        CV_CAP_PROP_FRAME_COUNT) << endl;
    // 获取视频总帧数   
    cout << "fps:" << inputVideo.get(CV_CAP_PROP_FPS) << endl;
    // 获取视频图像宽高
    cout << "videoResolution:" << videoResolution.width <<
        " " << videoResolution.height << endl;

    VideoWriter outputVideo("V.avi", CV_FOURCC('M','J','P','G'), 25.0,
        videoResolution, true);
    if (!outputVideo.isOpened())
    {
        cout << "fail to open!" << endl;
        return -1;
    }
    cv::Mat frameImg;
    int count = 0;
    cv::Mat resultImg1;
    while (true)
    {
        inputVideo >> frameImg;
        // 视频帧结束判断
        if (!frameImg.empty())
        {
            count++;
            applyColorMap(frameImg,resultImg1,COLORMAP_HOT);
            imshow("Canny Video",resultImg1);
            outputVideo<< resultImg1;
        }
        else
        {
            break;
        }
        // 按下键盘上q键退出
        if (char(waitKey(1)) == 'q')
        {
            inputVideo.release();
            outputVideo.release();
            break;
        }
    }
    std::cout << "writeTotalFrame:" << count << std::endl;

    inputVideo.release();
    outputVideo.release();
    return 0;
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值