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

文章选自:

一、VideoCapture类

复制代码
用于从视频文件、图像序列或摄像头捕获视频的类。
cpp 复制代码
函数:
 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

复制代码
从本地读取视频进行显示
cpp 复制代码
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
  }
}

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

cpp 复制代码
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
}

三、结果显示

相关推荐
沫儿笙1 分钟前
Kasawaki川崎焊接机器人弧焊气体节约设备
人工智能·机器人
程序猿小郑1 分钟前
Quill 编辑器自定义视频模块:将 iframe 替换为 video 标签
编辑器·音视频
中年程序员一枚7 分钟前
cv.drawChessboardCorners 是 OpenCV 中用于可视化棋盘格角点检测
人工智能·opencv·计算机视觉
线束线缆组件品替网9 分钟前
TE Linx RF 物联网射频模块的 RF 线缆连接设计思路
数码相机·物联网·测试工具·电脑·音视频·pcb工艺
星环科技11 分钟前
什么是 LLMOps?一文解析大语言模型运维(LLMOps)
人工智能·深度学习
电商API_1800790524713 分钟前
进阶篇:电商商品评论情感分析 + 关键词挖掘(Python NLP 实战)
大数据·开发语言·网络·数据库·人工智能
绒绒毛毛雨14 分钟前
Tending Towards Stability : Convergence Challenges in Small Language Models
人工智能·深度学习·语言模型
星环科技15 分钟前
AI-Ready数据平台赋能企业数智化转型
人工智能
派葛穆15 分钟前
机器人-六轴机械臂的正运动学
人工智能·机器学习·机器人
zyxzyx4915 分钟前
AI 核心趋势:多模态融合、AI Agent 与低代码开发的落地场景与挑战
人工智能