如何使用 C++ 在 OpenCV 中从计算机加载视频?
在本主题中,我们将了解如何加载视频文件并在 OpenCV 中播放,并且我们必须使用在前一主题中学习过的类似方法。唯一的区别是,不是将数字作为“VideoCapture”类对象的实参,而是必须将视频路径作为实参。
以下程序演示如何在 OpenCV 中使用 C++ 从计算机加载视频。
示例
#include<opencv2/opencv.hpp>//OpenCV header to use VideoCapture class//
#include<iostream>
using namespace std;
using namespace cv;
int main() {
Mat myImage;//Declaring a matrix to load the frames//
namedWindow("Video Player");//Declaring the video to show the video//
VideoCapture cap("video.mp4");//Declaring an object to capture stream of frames from default camera//
if (!cap.isOpened()){ //This section prompt an error message if no video stream is found//
cout << "No video stream detected" << endl;
system("pause");
return-1;
}
while (true){ //Taking an everlasting loop to show the video//
cap >> myImage;
if (myImage.empty()){ //Breaking the loop if no video frame is detected//
break;
}
imshow("Video Player", myImage);//Showing the video//
char c = (char)waitKey(25);//Allowing 25 milliseconds frame processing time and initiating break condition//
if (c == 27){ //If 'Esc' is entered break the loop//
break;
}
}
cap.release();//Releasing the buffer memory//
return 0;
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP