Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcWang/fa36b249107e18bfc811 to your computer and use it in GitHub Desktop.
Save MarcWang/fa36b249107e18bfc811 to your computer and use it in GitHub Desktop.

Get a list of Camera Devices using OpenCV

使用方式為依序輸入每一個device,直到回傳 null or false代表找不到裝置為止

C API

CvCapture *cap;
int device_counts = 0;
while ( true ) {
    cap = cvCreateCameraCapture( device_counts++ );
    if ( cap == NULL ) {
        break;
    }
    cvReleaseCapture(&cap);
}
cvReleaseCapture(&cap);
std::cout << "devices count : " << device_counts - 1 << std::endl;

C++ API

cv::VideoCapture camera;
int device_counts = 0;
while ( true ) {
    if ( !camera.open(device_counts++) ) {
        break;
    }
}
camera.release();
std::cout << "devices count : " << device_counts - 1 << std::endl;

更多OpenCV文章請參考:OpenCV Tutorial (學習筆記)

@HelloHellBoy
Copy link

How to Compile this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment