Giới thiệu
Truy cập camera khác camera mặc định: VideoCapture cap(index) với index = thứ tự cổng USB (0 = default, 1, 2, 3...).
Chương trình truy cập camera thứ 3
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
Mat myImage;
namedWindow("Video Player");
VideoCapture cap(3); // Camera thứ 3 (cổng USB 3)
if (!cap.isOpened()) {
cout << "Không phát hiện luồng video" << endl;
system("pause"); return -1;
}
while (true) {
cap >> myImage;
if (myImage.empty()) break;
imshow("Video Player", myImage);
if (waitKey(25) == 27) break;
}
cap.release();
return 0;
}
Kết quả
Video từ camera kết nối cổng USB thứ 3 hiển thị real-time.