opencv-视频输出

#include <iostream>	// for standard I/O
#include <string>   // for strings

#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat)
#include <opencv2/highgui/highgui.hpp>  // Video write

using namespace std;
using namespace cv;

void help()
{
    cout
        << "\n--------------------------------------------------------------------------" << endl
        << "This program shows how to write video files. You can extract the R or G or B color channel "
        << " of the input video.write "  << endl
        << "Usage:"                                                               << endl
        << "./video-write inputvideoName [ R | G | B] [Y | N]"                   << endl
        << "--------------------------------------------------------------------------"   << endl
        << endl;
}
int main(int argc, char *argv[], char *window_name)
{
    help();
    if (argc != 4)
    {
        cout << "Not enough parameters" << endl;
        return -1;
    }

    const string source      = argv[1];           // 输入视频名字
    const bool askOutputType = argv[3][0] =='Y';  // If false it will use the inputs codec type

    VideoCapture inputVideo(source);        //打开输入视频
    if ( !inputVideo.isOpened())
    {
        cout  << "Could not open the input video." << source << endl;
        return -1;
    }

    string::size_type pAt = source.find_last_of('.');   // 查找扩展点
    const string NAME = source.substr(0, pAt) + argv[2][0] + ".avi";   // 生成存储器的新名字
    int ex = static_cast<int>(inputVideo.get(CV_CAP_PROP_FOURCC));     // 获得编解码类型
    
	// 比特操作把int转换为char
    char EXT[] = {ex & 0XFF , (ex & 0XFF00) >> 8,(ex & 0XFF0000) >> 16,(ex & 0XFF000000) >> 24, 0};

    Size S = Size((int) inputVideo.get(CV_CAP_PROP_FRAME_WIDTH),    //获得输入视频大小
                  (int) inputVideo.get(CV_CAP_PROP_FRAME_HEIGHT));

    VideoWriter outputVideo;                                        //打开输出视频
    if (askOutputType)
            outputVideo.open(NAME  , ex=-1, inputVideo.get(CV_CAP_PROP_FPS),S, true);
    else
        outputVideo.open(NAME , ex, inputVideo.get(CV_CAP_PROP_FPS),S, true);

    if (!outputVideo.isOpened())
    {
        cout  << "Could not open the output video for write: " << source << endl;
        return -1;
    }

    union { int v; char c[5];} uEx ;
    uEx.v = ex;                              // 通过union函数,把char转换为int
    uEx.c[4]='\0';

    cout << "Input frame resolution: Width=" << S.width << "  Height=" << S.height
        << " of nr#: " << inputVideo.get(CV_CAP_PROP_FRAME_COUNT) << endl;
    cout << "Input codec type: " << EXT << endl;

    int channel = 2;    // 选择保存通道 B,R,G
    switch(argv[2][0])
    {
    case 'R' : {channel = 2; break;}
    case 'G' : {channel = 1; break;}
    case 'B' : {channel = 0; break;}
    }
    Mat src,res;
    vector<Mat> spl;

    while( true) //Show the image captured in the window and repeat
    {
        inputVideo >> src;              // read
        if( src.empty()) break;         // check if at end

       split(src, spl);                 // process - extract only the correct channel
       for( int i =0; i < 3; ++i)
        if (i != channel)
           spl[i] = Mat::zeros(S, spl[0].type());
       merge(spl, res);

       //outputVideo.write(res); //save or
       outputVideo << res;
    }

    cout << "Finished writing" << endl;
    return 0;
}














  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 要安装opencv-contrib-python,您可以使用pip命令在命令行中运行以下命令: pip install opencv-contrib-python 这将自动下载和安装最新版本的opencv-contrib-python。请确保您的计算机已连接到互联网,并且您具有管理员权限。如果您遇到任何问题,请尝试使用管理员权限运行命令提示符。 ### 回答2: OpenCV是一款广泛应用于计算机视觉领域的开源库,支持多种编程语言(如C++、Python等)。OpenCV提供了大量的图像和视频处理函数和算法,包括但不限于图像的处理、特征提取、目标检测、人脸识别等功能。 而opencv-contrib-python则是OpenCV官方的扩展包,提供了更多的功能和算法。在使用Python进行图像处理和计算机视觉方面的应用时,opencv-contrib-python是非常重要的一个工具。 安装opencv-contrib-python要考虑多个方面,其中比较重要的两个方面是你所使用的Python版本和操作系统。具体的安装步骤如下: 1. 安装Python 访问Python官网或第三方Python发行版网站,在网站的页面中可以下载Python。根据自己的电脑操作系统和位数下载相应的版本,例如 Windows x64 Installer 这个exe文件即可。 2. 安装OpenCV 在Windows环境下可使用pip install opencv-contrib-python命令进行安装。另外需要注意的是,opencv-contrib-python命令默认会安装OpenCV 3.4.2版本,如果需要安装其他版本则需要加上版本号进行安装。如果没有pip可以去初步下载中默认下载的图形化界面Anaconda Navigator中寻找opencv-contrib-python,选择对应版本进行安装。 在Linux环境下可使用以下命令安装: sudo apt-get update sudo apt-get install python-opencv 3. 测试安装是否成功 打开Python编辑器,在其中输入import cv2即可,如果没有报错说明安装成功了。 总结,opencv-contrib-python在Python的图像处理和计算机视觉方面应用中不可缺少,通过上述安装步骤可以顺利安装并使用它。 ### 回答3: Opencv-contrib-python是Opencv的扩展库,其包含了Opencv官方没有包含的模块,例如SIFT算法等。它提供了方便的接口与脚本语言的接口,对于需要使用这些模块的Python开发者来说很有价值。 在安装opencv-contrib-python之前,需要先安装Opencv。如果已经安装好了Opencv的话,可以先在终端中输入 pip install opencv-contrib-python 来直接安装Opencv-contrib-python。如果还不确定是否安装好了Opencv,可以通过终端中输入下述命令进行确认: import cv2 print(cv2.__version__) 如果上述代码正确执行并输出Opencv的版本号,则表明Opencv已经成功安装。 如果使用的是Anaconda,则可以在终端直接使用命令 conda install opencv-contrib-python 进行安装。如果想要使用末版的opencv-contrib-python,则可以尝试以下代码: pip install opencv-contrib-python-headless 这些命令在安装过程中需要获取一些文件并安装一些依赖包。如果使用的是Python 3,需要确保下载安装的是Python 3.x版的Opencv-contrib-python,否则会出现错误。安装完成后,可以在Python中导入模块并调用其中的函数。 总体而言,Opencv-contrib-python安装比较简单,只需要保证已经正确安装了Opencv,并根据自己的需求选择安装相应的版本即可。安装过程中可能会遇到一些问题,但一般都可以在网络上找到解决方法。如果有更多的问题或者疑惑,在网络或者社区中提出问题是一个不错的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Digital2Slave

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

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

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

打赏作者

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

抵扣说明:

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

余额充值