【opencv】教程代码 —videoio(3)读取输入的视频文件,提取出R、G或B颜色通道,并将结果写入新的视频文件...

cpp 复制代码
#include <iostream>  // 引入标准输入输出流库,用于输入输出
#include <string>   // 引入字符串库


#include <opencv2/core.hpp>     // 引入OpenCV基础结构(cv::Mat等)
#include <opencv2/videoio.hpp>  // 引入OpenCV视频读写


using namespace std; // 使用标准命名空间std
using namespace cv;  // 使用OpenCV命名空间cv


// 帮助函数,用于显示程序的使用说明
static void help()
{
    cout
        << "------------------------------------------------------------------------------" << endl
        << "This program shows how to write video files."                                   << endl
        << "You can extract the R or G or B color channel of the input video."              << endl
        << "Usage:"                                                                         << endl
        << "./video-write <input_video_name> [ R | G | B] [Y | N]"                          << endl
        << "------------------------------------------------------------------------------" << endl
        << endl;
}


// 主函数入口
int main(int argc, char *argv[])
{
    help(); // 调用帮助函数


    // 检查参数数量是否正确
    if (argc != 4)
    {
        cout << "Not enough parameters" << endl;
        return -1;
    }


    const string source      = argv[1];           // 命令行参数1:输入视频文件名
    const bool askOutputType = argv[3][0] =='Y';  // 命令行参数3:是否询问输出视频的类型(编码格式)


    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] + ".mp4";  //avi 生成新的视频文件名


    // 获取输入视频的编码格式
    int ex = static_cast<int>(inputVideo.get(CAP_PROP_FOURCC));     


    // 通过位运算将编码格式从int转换成字符形式
    char EXT[] = {(char)(ex & 0XFF) , (char)((ex & 0XFF00) >> 8),(char)((ex & 0XFF0000) >> 16),(char)((ex & 0XFF000000) >> 24), 0};


    // 获取输入视频的尺寸
    Size S = Size((int) inputVideo.get(CAP_PROP_FRAME_WIDTH),    // 宽
                  (int) inputVideo.get(CAP_PROP_FRAME_HEIGHT));  // 高


    VideoWriter outputVideo;                                    // 创建视频写入对象
    if (askOutputType)
        outputVideo.open(NAME, ex=-1, inputVideo.get(CAP_PROP_FPS), S, true);
    else
        outputVideo.open(NAME, ex, inputVideo.get(CAP_PROP_FPS), S, true);


    // 打开输出视频
    if (!outputVideo.isOpened())
    {
        cout  << "Could not open the output video for write: " << source << endl;
        return -1;
    }


    // 显示输入视频的相关信息
    cout << "Input frame resolution: Width=" << S.width << "  Height=" << S.height
         << " of nr#: " << inputVideo.get(CAP_PROP_FRAME_COUNT) << endl;
    cout << "Input codec type: " << EXT << endl;


    // 选择要保存的颜色通道
    int channel = 2; // 默认选择蓝色通道
    switch(argv[2][0])
    {
      case 'R' : channel = 2; break;
      case 'G' : channel = 1; break;
      case 'B' : channel = 0; break;
    }
    
    // 创建相关的Mat对象
    Mat src, res;
    vector<Mat> spl;


    // 无限循环,直到视频读取完毕
    for(;;)
    {
        inputVideo >> src;              // 读取视频帧
        if (src.empty()) break;         // 如果读取到空帧,则停止


        split(src, spl);                // 处理视频帧,分离色彩通道
        for (int i =0; i < 3; ++i)
            if (i != channel)
                spl[i] = Mat::zeros(S, spl[0].type()); // 将非选定通道置零
        merge(spl, res); // 合并通道


        outputVideo << res; // 将处理后的帧写入输出视频
    }


    cout << "Finished writing" << endl; // 写入完成
    return 0;
}

这段代码是一个C++程序,使用OpenCV库来处理视频文件。具体功能如下:

  1. 程序读取一个输入视频,并允许用户选择摘取红(R)、绿(G)或蓝(B)色彩通道。

  2. 用户可以选择是否询问输出视频的编码格式

  3. 如果输入视频成功打开,程序会通过用户选择提取特定的颜色通道,并将这些帧写入到一个新的视频文件中。

  4. 程序最终输出一个只包含选定颜色通道的视频文件。

char EXT[] = {(char)(ex & 0XFF) , (char)((ex & 0XFF00) >> 8),(char)((ex & 0XFF0000) >> 16),(char)((ex & 0XFF000000) >> 24), 0};

outputVideo.open(NAME, ex=-1, inputVideo.get(CAP_PROP_FPS), S, true);

./test.exe baseDraw.mp4 R Y 运行示例

报错:[ERROR:0@1.220] global cap.cpp:645 cv::VideoWriter::open VIDEOIO(CV_IMAGES): raised OpenCV exception:

split(src, spl);

Mat::zeros(S, spl[0].type())

merge(spl, res);

相关推荐
Lun3866buzha3 分钟前
✅ 军事目标检测与识别系统 Faster R-CNN实现 士兵坦克车辆武器爆炸物多类别检测 深度学习实战项目(建议收藏)计算机视觉(附源码)
深度学习·目标检测·计算机视觉
Lips61111 分钟前
第七章 贝叶斯分类器
人工智能·算法·机器学习
郝学胜-神的一滴18 分钟前
机器学习特征选择:深入理解移除低方差特征与sklearn的VarianceThreshold
开发语言·人工智能·python·机器学习·概率论·sklearn
HySpark20 分钟前
智能语音识别基于模型优化与部署技术的轻量化方案
人工智能·语音识别
却道天凉_好个秋21 分钟前
Tensorflow数据增强(一):图片的导入与显示
人工智能·python·tensorflow
一行注释也不写21 分钟前
【循环神经网络(RNN)】隐藏状态在序列任务中的应用
人工智能·rnn·深度学习
屹立芯创ELEADTECH24 分钟前
CoWoS封装技术全面解析:架构、演进与AI时代的基石作用
人工智能·架构
Coder_Boy_26 分钟前
基于SpringAI的在线考试系统-知识点管理与试题管理模块联合回归测试文档
前端·人工智能·spring boot·架构·领域驱动
黄焖鸡能干四碗26 分钟前
智慧电力解决方案,智慧电厂解决方案,电力运维方案
大数据·人工智能·安全·需求分析
飞Link28 分钟前
【计算机视觉】深度学习医疗影像实战:PathMNIST 数据集全解析
人工智能·深度学习·计算机视觉