如何使用 C++ 中的 OpenCV 从其他摄像机捕获视频?
在本主题中,我们将确定如何使用 OpenCV 从其他摄像机捕获视频。访问除默认摄像机外的其他摄像机与访问默认摄像机类似。仅有一点不同,即我们必须分配摄像机号,而不是使用“VideoCapture cap(0)”。摄像机号根据 USB 端口的顺序确定。如果摄像机连接到第三个 USB 端口,则摄像机号为 3。
以下程序访问第三个摄像机,并显示从摄像机获取的实时视频流。
示例
#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(3);//Declaring an object to capture stream of frames from third 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;
}
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;
}输出

广告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP