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