ที่นี่ เราจะเข้าใจวิธีเข้าถึงกล้องเริ่มต้นและแสดงสตรีมวิดีโอจากกล้องนั้น ในแล็ปท็อป เว็บแคมแบบตายตัวคือกล้องเริ่มต้น ในเดสก์ท็อป กล้องเริ่มต้นจะขึ้นอยู่กับลำดับของพอร์ตอนุกรมที่กล้องเชื่อมต่ออยู่ เมื่อเราต้องการจับภาพสตรีมวิดีโอจากเว็บแคมเริ่มต้น เราไม่จำเป็นต้องรู้อะไรเกี่ยวกับกล้องและตรวจสอบให้แน่ใจว่าได้เชื่อมต่อกล้องแล้ว
โปรแกรมต่อไปนี้ใช้สตรีมวิดีโอจากกล้องเริ่มต้นและแสดงบนหน้าจอแบบเรียลไทม์
ตัวอย่าง
#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;
} โปรแกรมนี้จะแสดงสตรีมวิดีโอเริ่มต้นของกล้องแบบเรียลไทม์บนจอภาพ
ผลลัพธ์
