如何计算摄像头在OpenCV 2.3? [英] How to count cameras in OpenCV 2.3?

查看:194
本文介绍了如何计算摄像头在OpenCV 2.3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得可用的相机数量。

I want to get the number of available cameras.

我试图计算像这样的相机:

I tried to count cameras like this:

for(int device = 0; device<10; device++) 
{
    VideoCapture cap(device);
    if (!cap.isOpened())
        return device;          
}

如果我连接了相机,它从未失败。
所以我试图预览不同的设备,但我总是得到我的相机的图像。

If I have a camera connected, it never failed to open. So I tried to preview different devices but I get always the image of my camera.

如果我连接第二台相机,设备0是相机1和设备1-10是相机2。

If I connect a second camera, device 0 is camera 1 and device 1-10 are camera 2.

我认为DirectShow设备有一个问题。

I think there is a problem with DirectShow devices.

如何解决这个问题?或者是否有像OpenCV1 cvcamGetCamerasCount()

How to solve this problem? Or is there a function like in OpenCV1 cvcamGetCamerasCount()?

的功能我正在使用Windows 7和USB摄像头。 / p>

I am using Windows 7 and USB cameras.

推荐答案

即使这里是一个旧的文章OpenCV 2 / C ++的解决方案

Even if it's an old post here a solution for OpenCV 2/C++

/**
 * Get the number of camera available
 */
int countCameras()
{
   cv::VideoCapture temp_camera;
   int maxTested = 10;
   for (int i = 0; i < maxTested; i++){
     cv::VideoCapture temp_camera(i);
     bool res = (!temp_camera.isOpened());
     temp_camera.release();
     if (res)
     {
       return i;
     }
   }
   return maxTested;
}

在Windows 7 x64下测试:

Tested under Windows 7 x64 with :


  • OpenCV 3 [自订版]

  • OpenCV 2.4.9

  • OpenCV 2.4.8

使用0到3部Usb相机

With 0 to 3 Usb Cameras

这篇关于如何计算摄像头在OpenCV 2.3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆