让OpenCV枚举所有摄像头-附如何建立Windows下的OpenCV工程

因公司项目需要,开始学一点OpenCV的东西。本篇博客要介绍的是,如何在Windows平台上建立一个OpenCV的项目,以及如何让OpenCV枚举本机上的所有的摄像头。


如何建立Windows平台上的OpenCV的工程

参考了这篇文章,但此文有点老了,讲述的主要是VS2008和VS2010,所以笔者自己的实践中会有一些新的东西。

1. IDE:Visual Studio 2013

2. 下载Windows平台的OpenCV安装文件。

    最新版本是3.1.0,于2015年圣诞节发布,大小仅110MB左右;而上一个版本3.0.0居然是269MB。不知道这其中有什么变化会导致如此巨大的size差异。

3. 安装OpenCV

    该过程比较简单,只要双击下载的exe文件,选择合适路径解压即可。这个下载的exe文件其实仅仅是个压缩文件。

4. 配置环境变量

    这一步比较重要。本人是在系统环境变量中配置了一个名为OPENCV_DIR的环境变量,其值为C:\opencv\build\x64\vc12

    x64代表64位的机器,vc12指的就是VS2013了。如果你的VS是2015,那要选择vc14. 

    然后在PATH中添加%OPENCV_DIR%\bin,这是让系统知道在哪里去找dll文件。

5. 开启Visual Studio,建立一个solution,建立工程的时候选择Win32 Console Application,然后要注意勾选Empty Project.

6. VS: Project -> Property

此时,可以在Configuration中选择New,添加一种新的build方式,也可以修改现有的Debug或Release模式。

以下假设是修改现有的Debug模式。

6.1 C++ -> General -> Additional Included Directories

      此处填上:$(OPENCV_DIR)\..\..\include

6.2 Linker -> Additional Library Directories

      此处填上:$(OPENCV_DIR)\lib

6.3 Linker -> Input -> Additional Dependencies

      此处在原有基础上增加各种lib文件,比如:opencv_ts300d.lib;opencv_world300d.lib;

至此,基本大功告成,剩下的就是添加代码了。


让OpenCV自己枚举所有摄像头

此时,在以上建立的工程中添加一个cpp文件,复制粘贴如下代码,参考文献为此篇文章:

#include 
#include 
#include 
#include 
using namespace cv;
using namespace std;

bool EnumerateCameras(vector &camIdx)
{
    camIdx.clear();
    struct CapDriver{
        int enumValue; string enumName; string comment;
    };
    // list of all CAP drivers (see highgui_c.h)
    vector drivers;
    drivers.push_back({ CV_CAP_MIL, "CV_CAP_MIL", "MIL proprietary drivers" });
    drivers.push_back({ CV_CAP_VFW, "CV_CAP_VFW", "platform native" });
    drivers.push_back({ CV_CAP_FIREWARE, "CV_CAP_FIREWARE", "IEEE 1394 drivers" });
    drivers.push_back({ CV_CAP_STEREO, "CV_CAP_STEREO", "TYZX proprietary drivers" });
    drivers.push_back({ CV_CAP_QT, "CV_CAP_QT", "QuickTime" });
    drivers.push_back({ CV_CAP_UNICAP, "CV_CAP_UNICAP", "Unicap drivers" });
    drivers.push_back({ CV_CAP_DSHOW, "CV_CAP_DSHOW", "DirectShow (via videoInput)" });
    drivers.push_back({ CV_CAP_MSMF, "CV_CAP_MSMF", "Microsoft Media Foundation (via videoInput)" });
    drivers.push_back({ CV_CAP_PVAPI, "CV_CAP_PVAPI", "PvAPI, Prosilica GigE SDK" });
    drivers.push_back({ CV_CAP_OPENNI, "CV_CAP_OPENNI", "OpenNI (for Kinect)" });
    drivers.push_back({ CV_CAP_OPENNI_ASUS, "CV_CAP_OPENNI_ASUS", "OpenNI (for Asus Xtion)" });
    drivers.push_back({ CV_CAP_ANDROID, "CV_CAP_ANDROID", "Android" });
    drivers.push_back({ CV_CAP_ANDROID_BACK, "CV_CAP_ANDROID_BACK", "Android back camera" }),
    drivers.push_back({ CV_CAP_ANDROID_FRONT, "CV_CAP_ANDROID_FRONT", "Android front camera" }),
    drivers.push_back({ CV_CAP_XIAPI, "CV_CAP_XIAPI", "XIMEA Camera API" });
    drivers.push_back({ CV_CAP_AVFOUNDATION, "CV_CAP_AVFOUNDATION", "AVFoundation framework for iOS" });
    drivers.push_back({ CV_CAP_GIGANETIX, "CV_CAP_GIGANETIX", "Smartek Giganetix GigEVisionSDK" });
    drivers.push_back({ CV_CAP_INTELPERC, "CV_CAP_INTELPERC", "Intel Perceptual Computing SDK" });

    std::string winName, driverName, driverComment;
    int driverEnum;
    Mat frame;
    bool found;
    std::cout << "Searching for cameras IDs..." << endl << endl;
    for (int drv = 0; drv < drivers.size(); drv++)
    {
        driverName = drivers[drv].enumName;
        driverEnum = drivers[drv].enumValue;
        driverComment = drivers[drv].comment;
        std::cout << "Testing driver " << driverName << "...";
        found = false;

        int maxID = 100; //100 IDs between drivers
        if (driverEnum == CV_CAP_VFW)
            maxID = 10; //VWF opens same camera after 10 ?!?
        else if (driverEnum == CV_CAP_ANDROID)
            maxID = 98; //98 and 99 are front and back cam
        else if ((driverEnum == CV_CAP_ANDROID_FRONT) || (driverEnum == CV_CAP_ANDROID_BACK))
            maxID = 1;

        for (int idx = 0; idx > frame;
                if (frame.empty())
                    std::cout << endl << driverName << "+" << idx << "\t opens: OK \t grabs: FAIL";
                else
                    std::cout << endl << driverName << "+" << idx << "\t opens: OK \t grabs: OK";
                // display the frame
                // imshow(driverName + "+" + to_string(idx), frame); waitKey(1);
            }
            cap.release();
        }
        if (!found) cout << "Nothing !" << endl;
        cout << endl;
    }
    cout << camIdx.size() << " camera IDs has been found ";
    cout << "Press a key..." << endl; cin.get();

    return (camIdx.size()>0); // returns success
}

int main()
{
    vector cam_vec;
    EnumerateCameras(cam_vec);

    return 0;
}

Ctrl+Shift+B进行build,然后Ctrl+F5运行。

以上代码是打算枚举出OpenCV所支持的所有种类(driver)的摄像头。具体种类和起始enum value如下:

Enum name Value Comment
CV_CAP_MIL 100 MIL proprietary drivers
CV_CAP_VFW 200 platform native
CV_CAP_FIREWARE 300 IEEE 1394 drivers
CV_CAP_STEREO 400 TYZX proprietary drivers
CV_CAP_QT 500 QuickTime
CV_CAP_UNICAP 600 Unicap drivers
CV_CAP_DSHOW 700 DirectShow (via videoInput)
CV_CAP_PVAPI 800 PvAPI, Prosilica GigE SDK
CV_CAP_OPENNI 900 OpenNI (for Kinect)
CV_CAP_OPENNI_ASUS 910 OpenNI (for Asus Xtion)
CV_CAP_ANDROID 1000 Android
CV_CAP_ANDROID_FRONT 1098 Android front camera
CV_CAP_ANDROID_BACK 1099 Android back camera
CV_CAP_XIAPI 1100 XIMEA Camera API
CV_CAP_AVFOUNDATION 1200 AVFoundation framework for iOS
CV_CAP_GIGANETIX 1300 Smartek Giganetix GigEVisionSDK
CV_CAP_MSMF 1400 Microsoft Media Foundation (via videoInput)
CV_CAP_INTELPERC 1500 Intel Perceptual Computing SDK

在笔者自己的机器上的执行结果如下(笔者的机器上有6个摄像头!):

***** VIDEOINPUT LIBRARY - 0.1995 - TFW07 *****

Searching for cameras IDs...

Testing driver CV_CAP_MIL...Nothing !

Testing driver CV_CAP_VFW...Nothing !

Testing driver CV_CAP_FIREWARE...Nothing !

Testing driver CV_CAP_STEREO...Nothing !

Testing driver CV_CAP_QT...Nothing !

Testing driver CV_CAP_UNICAP...Nothing !

Testing driver CV_CAP_DSHOW...SETUP: Setting up device 0
SETUP: USB Camera-OV580
SETUP: Couldn't find preview pin using SmartTee
SETUP: Default Format is set to 1104x828
SETUP: trying specified format RGB24 @ 640x480
SETUP: trying format RGB24 @ 640x480
SETUP: trying format RGB32 @ 640x480
SETUP: trying format RGB555 @ 640x480
SETUP: trying format RGB565 @ 640x480
SETUP: trying format YUY2 @ 640x480
SETUP: trying format YVYU @ 640x480
SETUP: trying format YUYV @ 640x480
SETUP: trying format IYUV @ 640x480
SETUP: trying format UYVY @ 640x480
SETUP: trying format YV12 @ 640x480
SETUP: trying format YVU9 @ 640x480
SETUP: trying format Y411 @ 640x480
SETUP: trying format Y41P @ 640x480
SETUP: trying format Y211 @ 640x480
SETUP: trying format AYUV @ 640x480
SETUP: trying format MJPG @ 640x480
SETUP: trying format Y800 @ 640x480
SETUP: trying format Y800 @ 640x480
SETUP: trying format Y800 @ 640x480
SETUP: trying format I420 @ 640x480
SETUP: couldn't find requested size - searching for closest matching size
SETUP: closest supported size is YUY2 @ 1104 828
SETUP: Capture callback set
SETUP: Device is setup and ready to capture.


CV_CAP_DSHOW+0   opens: OK       grabs: OK
SETUP: Disconnecting device 0
SETUP: freeing Grabber Callback
SETUP: freeing Grabber
SETUP: freeing Control
SETUP: freeing Media Type
SETUP: removing filter NullRenderer...
SETUP: filter removed NullRenderer
SETUP: removing filter Sample Grabber...
SETUP: filter removed Sample Grabber
SETUP: removing filter AVI Decompressor...
SETUP: filter removed AVI Decompressor
SETUP: removing filter Smart Tee...
SETUP: filter removed Smart Tee
SETUP: removing filter USB Camera-OV580...
SETUP: filter removed USB Camera-OV580
SETUP: freeing Capture Graph
SETUP: freeing Main Graph
SETUP: Device 0 disconnected and freed

SETUP: Setting up device 1
SETUP: Forward Facing camera
SETUP: Couldn't find preview pin using SmartTee
SETUP: Default Format is set to 320x240
SETUP: trying specified format RGB24 @ 640x480
SETUP: Capture callback set
SETUP: Device is setup and ready to capture.


CV_CAP_DSHOW+1   opens: OK       grabs: OK
SETUP: Disconnecting device 1
SETUP: freeing Grabber Callback
SETUP: freeing Grabber
SETUP: freeing Control
SETUP: freeing Media Type
SETUP: removing filter NullRenderer...
SETUP: filter removed NullRenderer
SETUP: removing filter Sample Grabber...
SETUP: filter removed Sample Grabber
SETUP: removing filter Smart Tee...
SETUP: filter removed Smart Tee
SETUP: removing filter Forward Facing camera...
SETUP: filter removed Forward Facing camera
SETUP: freeing Capture Graph
SETUP: freeing Main Graph
SETUP: Device 1 disconnected and freed

SETUP: Setting up device 2
SETUP: Downward Facing camera
SETUP: Couldn't find preview pin using SmartTee
SETUP: Default Format is set to 320x240
SETUP: trying specified format RGB24 @ 640x480
SETUP: Capture callback set
SETUP: Device is setup and ready to capture.


CV_CAP_DSHOW+2   opens: OK       grabs: OK
SETUP: Disconnecting device 2
SETUP: freeing Grabber Callback
SETUP: freeing Grabber
SETUP: freeing Control
SETUP: freeing Media Type
SETUP: removing filter NullRenderer...
SETUP: filter removed NullRenderer
SETUP: removing filter Sample Grabber...
SETUP: filter removed Sample Grabber
SETUP: removing filter Smart Tee...
SETUP: filter removed Smart Tee
SETUP: removing filter Downward Facing camera...
SETUP: filter removed Downward Facing camera
SETUP: freeing Capture Graph
SETUP: freeing Main Graph
SETUP: Device 2 disconnected and freed

SETUP: Setting up device 3
SETUP: Intel(R) RealSense(TM) 3D Camera (Front F200) RGB
SETUP: Couldn't find preview pin using SmartTee
SETUP: Default Format is set to 320x180
SETUP: trying specified format RGB24 @ 640x480
SETUP: trying format RGB24 @ 640x480
SETUP: trying format RGB32 @ 640x480
SETUP: trying format RGB555 @ 640x480
SETUP: trying format RGB565 @ 640x480
SETUP: trying format YUY2 @ 640x480
SETUP: Capture callback set
SETUP: Device is setup and ready to capture.


CV_CAP_DSHOW+3   opens: OK       grabs: OK
SETUP: Disconnecting device 3
SETUP: freeing Grabber Callback
SETUP: freeing Grabber
SETUP: freeing Control
SETUP: freeing Media Type
SETUP: removing filter NullRenderer...
SETUP: filter removed NullRenderer
SETUP: removing filter Sample Grabber...
SETUP: filter removed Sample Grabber
SETUP: removing filter AVI Decompressor...
SETUP: filter removed AVI Decompressor
SETUP: removing filter Smart Tee...
SETUP: filter removed Smart Tee
SETUP: removing filter Intel(R) RealSense(TM) 3D Camera (Front F200) RGB...
SETUP: filter removed Intel(R) RealSense(TM) 3D Camera (Front F200) RGB
SETUP: freeing Capture Graph
SETUP: freeing Main Graph
SETUP: Device 3 disconnected and freed

SETUP: Setting up device 4
SETUP: HP High Definition 1MP Webcam
SETUP: Couldn't find preview pin using SmartTee
SETUP: Default Format is set to 640x480
SETUP: trying specified format RGB24 @ 640x480
SETUP: trying format RGB24 @ 640x480
SETUP: trying format RGB32 @ 640x480
SETUP: trying format RGB555 @ 640x480
SETUP: trying format RGB565 @ 640x480
SETUP: trying format YUY2 @ 640x480
SETUP: Capture callback set
SETUP: Device is setup and ready to capture.


CV_CAP_DSHOW+4   opens: OK       grabs: OK
SETUP: Disconnecting device 4
SETUP: freeing Grabber Callback
SETUP: freeing Grabber
SETUP: freeing Control
SETUP: freeing Media Type
SETUP: removing filter NullRenderer...
SETUP: filter removed NullRenderer
SETUP: removing filter Sample Grabber...
SETUP: filter removed Sample Grabber
SETUP: removing filter AVI Decompressor...
SETUP: filter removed AVI Decompressor
SETUP: removing filter Smart Tee...
SETUP: filter removed Smart Tee
SETUP: removing filter HP High Definition 1MP Webcam...
SETUP: filter removed HP High Definition 1MP Webcam
SETUP: freeing Capture Graph
SETUP: freeing Main Graph
SETUP: Device 4 disconnected and freed

SETUP: Setting up device 5
SETUP: Intel(R) RealSense(TM) 3D Camera (Front F200) Depth
SETUP: Couldn't find preview pin using SmartTee
SETUP: Default Format is set to 640x480
SETUP: trying specified format RGB24 @ 640x480
SETUP: trying format RGB24 @ 640x480
SETUP: trying format RGB32 @ 640x480
SETUP: trying format RGB555 @ 640x480
SETUP: trying format RGB565 @ 640x480
SETUP: trying format YUY2 @ 640x480
SETUP: Capture callback set
SETUP: Device is setup and ready to capture.


CV_CAP_DSHOW+5   opens: OK       grabs: OK
SETUP: Disconnecting device 5
SETUP: freeing Grabber Callback
SETUP: freeing Grabber
SETUP: freeing Control
SETUP: freeing Media Type
SETUP: removing filter NullRenderer...
SETUP: filter removed NullRenderer
SETUP: removing filter Sample Grabber...
SETUP: filter removed Sample Grabber
SETUP: removing filter AVI Decompressor...
SETUP: filter removed AVI Decompressor
SETUP: removing filter Smart Tee...
SETUP: filter removed Smart Tee
SETUP: removing filter Intel(R) RealSense(TM) 3D Camera (Front F200) Depth...
SETUP: filter removed Intel(R) RealSense(TM) 3D Camera (Front F200) Depth
SETUP: freeing Capture Graph
SETUP: freeing Main Graph
SETUP: Device 5 disconnected and freed


Testing driver CV_CAP_MSMF...Nothing !

Testing driver CV_CAP_PVAPI...Nothing !

Testing driver CV_CAP_OPENNI...Nothing !

Testing driver CV_CAP_OPENNI_ASUS...Nothing !

Testing driver CV_CAP_ANDROID...Nothing !

Testing driver CV_CAP_ANDROID_BACK...Nothing !

Testing driver CV_CAP_ANDROID_FRONT...Nothing !

Testing driver CV_CAP_XIAPI...Nothing !

Testing driver CV_CAP_AVFOUNDATION...Nothing !

Testing driver CV_CAP_GIGANETIX...Nothing !

Testing driver CV_CAP_INTELPERC...Nothing !

6 camera IDs has been found Press a key...

注:所有"SET UP:"开头的行,皆是VideoCapture类的构造函数执行过程中所打印的。


参考文献:

1. 如何在Windows上建立OpenCV的工程:http://docs.opencv.org/3.1.0/d6/d8a/tutorial_windows_visual_studio_Opencv.html

2. 如何枚举摄像头:http://answers.opencv.org/question/77871/i-get-no-video-from-the-camera-that-is-connected-with-firewire/?answer=77900#post-id-77900


你可能感兴趣的:(OpenCV,C++,图像处理)