Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One simple method is to try open webcams until you fail. The number of successes is the number of webcams.

How is how I would do this:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"


/** @function main */
int main(int argc, const char** argv)
{
    int numOfWebCams = 0;
    cv::VideoCapture cap[MAX_NUM_OF_CAMERAS];
    cv::Mat frame;
    for (size_t i = 0; i < MAX_NUM_OF_CAMERAS; i++)
    {
        cap[i].open(i);
        bool bSuccess = true;
        if (!cap[i].isOpened())  // check if we succeeded
            bSuccess &= false;

        bSuccess &= cap[i].read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
            std::cout << "The number of active webcams is: " << numOfWebCams << std::endl;
            break;
        }
        else
            ++numOfWebCams;
    }
    return 0;
}