C++ 如何计算 OpenCV 2.3 中的摄像头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7322939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:47:14  来源:igfitidea点击:

How to count cameras in OpenCV 2.3?

c++opencv

提问by sirlunchalot

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.

如果我连接了相机,它永远不会无法打开。所以我试图预览不同的设备,但我总是得到我相机的图像。

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

如果我连接第二个摄像头,设备 0 是摄像头 1,设备 1-10 是摄像头 2。

I think there is a problem with DirectShow devices.

我认为 DirectShow 设备有问题。

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

如何解决这个问题呢?或者是否有像 OpenCV1 这样的功能cvcamGetCamerasCount()

I am using Windows 7 and USB cameras.

我正在使用 Windows 7 和 USB 摄像头。

回答by Andrey Kamaev

OpenCV still has no API to enumerate the cameras or get the number of available devices. See this ticketon OpenCV bug tracker for details.

OpenCV 仍然没有 API 来枚举摄像头或获取可用设备的数量。有关详细信息,请参阅OpenCV 错误跟踪器上的此票证

Behavior of VideoCapture is undefined for device numbers greater then number of devices connected and depends from API used to communicate with your camera. See OpenCV 2.3 (C++,QtGui), Problem Initializing some specific USB Devices and Setupsfor the list of APIs used in OpenCV.

对于大于连接的设备数量的设备编号,VideoCapture 的行为未定义,并且取决于用于与您的相机通信的 API。有关OpenCV 中使用的 API 列表,请参阅OpenCV 2.3 (C++,QtGui), Problem Initializing some specific USB Devices and Setups

回答by BlouBlou

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

即使这是一个旧帖子,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;
}

Tested under Windows 7 x64 with :

在 Windows 7 x64 下测试:

  • OpenCV 3 [Custom Build]
  • OpenCV 2.4.9
  • OpenCV 2.4.8
  • OpenCV 3 [自定义构建]
  • OpenCV 2.4.9
  • OpenCV 2.4.8

With 0 to 3 Usb Cameras

带 0 到 3 个 USB 摄像头

回答by Artur

This is a very old post but I found that under Python 2.7 on Ubuntu 14.04 and OpenCv 3 none of the solutions here worked for me. Instead I came up with something like this in Python:

这是一篇很老的帖子,但我发现在 Ubuntu 14.04 和 OpenCv 3 上的 Python 2.7 下,这里没有任何解决方案对我有用。相反,我在 Python 中想出了这样的东西:

import cv2

def clearCapture(capture):
    capture.release()
    cv2.destroyAllWindows()

def countCameras():
    n = 0
    for i in range(10):
        try:
            cap = cv2.VideoCapture(i)
            ret, frame = cap.read()
            cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            clearCapture(cap)
            n += 1
        except:
            clearCapture(cap)
            break
    return n

print countCameras()

Maybe someone will find this useful.

也许有人会发现这很有用。

回答by NinjasAtWork

I do this in Python:

我在 Python 中这样做:

def count_cameras():
    for i in range(10):
        temp_camera = cv.CreateCameraCapture(i-1)
        temp_frame = cv.QueryFrame(temp_camera)
        del(temp_camera)
        if temp_frame==None:
            del(temp_frame)
            return i-1 #MacbookPro counts embedded webcam twice

Sadly Opencv opens the Camera object anyway, even if there is nothing there, but if you try to extract its content, there will be nothing to attribute to. You can use that to check your number of cameras. It works in every platform I tested so it is good.

遗憾的是,Opencv 无论如何都会打开 Camera 对象,即使那里什么都没有,但是如果您尝试提取其内容,则将没有任何可归因的内容。您可以使用它来检查您的摄像机数量。它适用于我测试过的每个平台,所以它很好。

The reason for returning i-1is that MacBookPro Counts its own embedded camera twice.

i-1退货的原因是 MacBookPro 对自己的嵌入式摄像头进行了两次计数。

回答by Arjun Singh

I have also faced similar kind of issue. I solved the problem by using videoInput.h library instead of Opencv for enumerating the cameras and passed the index to Videocapture object. It solved my problem.

我也遇到过类似的问题。我通过使用 videoInput.h 库而不是 Opencv 来枚举摄像机并将索引传递给 Videocapture 对象解决了这个问题。它解决了我的问题。

回答by FooBar167

Python 3.6:

蟒蛇 3.6:

import cv2

# Get the number of cameras available
def count_cameras():
    max_tested = 100
    for i in range(max_tested):
        temp_camera = cv2.VideoCapture(i)
        if temp_camera.isOpened():
            temp_camera.release()
            continue
        return i

print(count_cameras())