【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 天前
【回眸】Minicart 电商购物车核心功能落地指南
人工智能
一隅论数智1 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
AI的探索之旅1 天前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
AlbertZein1 天前
Step-5-Preview 上手实测:3D 游戏、金融分析、网页设计一次跑完
人工智能·aigc
LaughingZhu1 天前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
美狐美颜SDK开放平台1 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
wukangjupingbb1 天前
智能网联汽车安全能力框架
人工智能
龙亘川1 天前
明月照湾区,智启新赛道:从顶流文旅IP盛会看智慧文旅升级路径
人工智能·智慧城市·开源软件·数据可视化
飞猫的边缘AI1 天前
边缘AI应用:家用AI摄像头怎么做数据训练?
人工智能·边缘计算·ai算法·边缘ai