如何使用 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