如何使用 C++ 中的 OpenCV 从默认摄像头获取视频?
在此,我们将了解如何访问默认摄像头并显示该摄像头的视频流。在笔记本电脑中,固定网络摄像头是默认摄像头。在台式机中,默认摄像头取决于摄像头所连接的串行端口序列。当我们想要从默认网络摄像头获取视频流时,我们无需了解该摄像头,并确保摄像头已连接。
以下程序从默认摄像头获取视频流,并将其实时显示在屏幕上。
范例
#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(0);//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;
}此程序将在显示器上显示默认摄像头的实时视频流。
输出

广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP