ภาพรวม
สาธิตการเข้าถึง กล้องที่ไม่ใช่ Default Camera โดยระบุ Camera Index ใน VideoCapture(index)
Camera Index
0= Default Camera (Built-in Webcam หรือ USB Port แรก)1, 2, 3...= กล้อง USB ต่อตามลำดับ Port- ลำดับขึ้นกับ OS และ Driver
โค้ดตัวอย่าง C++ (ใช้กล้องตัวที่ 3)
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
Mat myImage;
namedWindow("Video Player");
VideoCapture cap(3); // กล้องตัวที่ 3 (USB Port ลำดับที่ 3)
if (!cap.isOpened()) {
cout << "No video stream detected" << endl;
system("pause");
return -1;
}
while (true) {
cap >> myImage;
if (myImage.empty()) break;
imshow("Video Player", myImage);
char c = (char)waitKey(25);
if (c == 27) break;
}
cap.release();
return 0;
}
หมายเหตุ
โค้ดต้นฉบับมีบรรทัดซ้ำ (imshow และ waitKey ซ้ำ 2 ครั้ง) - แก้ไขแล้วในตัวอย่างนี้
ผลลัพธ์
แสดงวิดีโอจากกล้อง USB ตัวที่ 3 (ถ้ามี)
