【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几秒前
自建爬虫、免费股票量化数据源、商业 量化API:三种股票数据获取方式的完整对比
大数据·人工智能
Light602 分钟前
豆包收回了那只“手“,Agent手机开始拼协议
人工智能·智能手机·mcp协议·权限边界·agent手机·appfunctions·工具投毒
Yolanda_20229 分钟前
PyTorch 线性层nn.Linear底层数学原理
人工智能·pytorch·python
音视频牛哥13 分钟前
从 RTSP、RTMP 到 WHIP:SmartMediaKit 的技术演进与工程实践
音视频·whip推流·whep播放器·rtsp转whip推流·rtmp转whip推流·安卓摄像头推送whip·webrtc推流
公子小六17 分钟前
基于.NET的Windows窗体编程之WinForms自定义控件(3)
windows·c#·.net·音视频·winforms
长弓三石18 分钟前
钉钉与飞书机器人接入企业级智能体过程及排障实录
人工智能·机器人·钉钉·飞书·企业级harness
火山引擎和TA的超级拍档们20 分钟前
Seedance 平遥专场:AI 为动画人架起一座桥
人工智能
jiangyoushushu23 分钟前
FLAC手机播不了?转成MP3会更方便
音视频·音频
Industio_触觉智能26 分钟前
把NPU检测直接嵌进采集终端|RK3576 AI采集终端全链路拆解教程-上
人工智能·嵌入式硬件·硬件架构·硬件工程·智能硬件·npu·rk3576
专注仿真30 分钟前
多智能体强化学习实战:从 Dec-POMDP 到 QMIX/MADDPG/MAPPO 的可运行 PyTorch 实现
人工智能·pytorch·python