【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
}

三、结果显示

相关推荐
盼小辉丶3 分钟前
TensorFlow深度学习实战——去噪自编码器详解与实现
人工智能·深度学习·tensorflow
胖达不服输11 分钟前
「日拱一码」020 机器学习——数据处理
人工智能·python·机器学习·数据处理
吴佳浩1 小时前
Python入门指南-AI模型相似性检测方法:技术原理与实现
人工智能·python·llm
kebijuelun1 小时前
百度文心 4.5 大模型详解:ERNIE 4.5 Technical Report
人工智能·深度学习·百度·语言模型·自然语言处理·aigc
算家计算1 小时前
ComfyUI-v0.3.43本地部署教程:新增 Omnigen 2 支持,复杂图像任务一步到位!
人工智能·开源
新智元1 小时前
毕业 7 年,身价破亿!清北 AI 天团血洗硅谷,奥特曼被逼分天价股份
人工智能·openai
新智元1 小时前
刚刚,苹果大模型团队负责人叛逃 Meta!华人 AI 巨星 + 1,年薪飙至 9 位数
人工智能·openai
Cyltcc2 小时前
如何安装和使用 Claude Code 教程 - Windows 用户篇
人工智能·claude·visual studio code
吹风看太阳2 小时前
机器学习16-总体架构
人工智能·机器学习
moonsims3 小时前
全国产化行业自主无人机智能处理单元-AI飞控+通信一体化模块SkyCore-I
人工智能·无人机