当前位置: 首页 > article >正文

【opencv入门教程】9.视频加载

文章选自:
请添加图片描述

一、VideoCapture类

用于从视频文件、图像序列或摄像头捕获视频的类。

函数:
 CV_WRAP VideoCapture();
 @brief 默认构造函数
 
 CV_WRAP explicit VideoCapture(const String& filename, int apiPreference = CAP_ANY);
 @brief 使用 API 首选项打开视频文件、捕获设备或 IP 视频流进行视频捕获
 @param filename 可以是:
    - 视频文件名称(如 `video.avi`)
    - 图像序列(如 `img_%02d.jpg`,将读取类似于 `img_00.jpg, img_01.jpg, img_02.jpg, ...` 的样本)
    - 视频流 URL(如 `protocol://host:port/script_name?script_params|auth`)
    - GStreamer 管道字符串,格式为 gst-launch 工具格式,如果使用 GStreamer 作为后端
      请注意,每个视频流或IP摄像头都有自己的URL方案。
 @param apiPreference 首选的捕获 API 后端。可用于强制执行特定的读取器实现,如果有多个可用:例如 cv::CAP_FFMPEG 或 cv::CAP_IMAGES 或 cv::CAP_DSHOW。
 @sa cv::VideoCaptureAPIs
 
 CV_WRAP explicit VideoCapture(const String& filename, int apiPreference, const std::vector<int>& params)
 @brief 使用 API 首选项和参数打开视频文件、捕获设备或 IP 视频流进行视频捕获
 @param  `params` 参数允许指定额外的参数,编码为成对的(paramId_1, paramValue_1, paramId_2, paramValue_2, ...)`。
 
 CV_WRAP explicit VideoCapture(int index, int apiPreference = CAP_ANY);
 @brief 打开摄像头进行视频捕获
 @param index 要打开的视频捕捉设备的ID。要使用默认后端打开默认摄像头,只需传递 0。
    (对于向后兼容性,当 apiPreference 为 CAP_ANY 时,camera_id + domain_offset(CAP_*)的用法是有效的)
 @param apiPreference 首选的捕获 API 后端。可用于强制执行特定的读取器实现,如果有多个可用:例如 cv::CAP_DSHOW 或 cv::CAP_MSMF 或 cv::CAP_V4L。

 CV_WRAP explicit VideoCapture(int index, int apiPreference, const std::vector<int>& params);
 @brief 使用 API 首选项和参数打开摄像头进行视频捕获
 @param `params` 参数允许指定额外的参数,编码为成对(paramId_1, paramValue_1, paramId_2, paramValue_2, ...)`。请参阅 cv::VideoCaptureProperties
 

二、测试code

从本地读取视频进行显示
void Samples::LoadVideoFunc()
{
  //【1】读入视频
  VideoCapture capture;
  capture.open("./vtest.avi");
  //【2】循环显示每一帧
  while (1) {
    Mat frame;        //定义一个Mat变量,用于存储每一帧的图像
    capture >> frame; //读取当前帧

    //若视频播放完成,退出循环
    if (frame.empty()) {
      break;
    }

    imshow("Video", frame); //显示当前帧
    waitKey(30);                    //延时30ms
  }
}

从设备上读取视频进行显示

void Samples::VideoFunc()
{
  Mat frame;
  //--- 初始化video
  VideoCapture cap;
  // 打开默认相机
  // cap.open(0);
  // OR advance usage: select any API backend
  int deviceID = 0;             // 0 = open default camera
  int apiID = cv::CAP_ANY;      // 0 = autodetect default API
  // open selected camera using selected API
  cap.open(deviceID, apiID);
  // 检测是否打开
  if (!cap.isOpened()) {
    cerr << "ERROR! Unable to open camera\n";
    //return -1;
  }

  //--- GRAB AND WRITE LOOP
  cout << "Start grabbing" << endl
    << "Press any key to terminate" << endl;
  for (;;)
  {
    // wait for a new frame from camera and store it into 'frame'
    cap.read(frame);
    // check if we succeeded
    if (frame.empty()) {
      cerr << "ERROR! blank frame grabbed\n";
      break;
    }
    // show live and wait for a key with timeout long enough to show images
    imshow("Live", frame);
    if (waitKey(5) >= 0)
      break;
  }
  // the camera will be deinitialized automatically in VideoCapture destructor
}

三、结果显示

在这里插入图片描述


http://www.kler.cn/a/427422.html

相关文章:

  • SecrureCRT设置每行的长度:
  • MySQL数据库(4)-基础->高阶查询
  • 乾元通渠道商中标福州市人防信息化建设项目
  • 魔改版kali分享(新增50多种渗透工具)
  • docker学习笔记(四)--DockerFile
  • 002-NoSQL介绍
  • spark3 sql优化:同一个表关联多次,优化方案
  • Web安全深度剖析
  • URL访问网址的全过程
  • [C#]利用opencvsharp 已知原图和mask掩码图像,抠出原图中人物,背景设置为透明色
  • 方案拆解 | 打击矩阵新规频出!2025矩阵营销该怎么玩?
  • 蓝桥杯2117砍竹子(简单易懂 包看包会版)
  • 常见限流算法介绍 和 Spring Cloud Sentinel使用方式
  • 企业级资源监控方案落地:Prometheus+Grafana+Export
  • 代码随想录-算法训练营day35(贪心算法05:无重叠区间,划分字母区间,合并区间)
  • oracle 数组分组
  • 电子应用设计方案-43:智能手机充电器系统方案设计
  • node.js常用的模块和中间件?
  • DAY168内网对抗-基石框架篇单域架构域内应用控制成员组成用户策略信息收集环境搭建
  • RBA评分等级和标准