Search code examples
c++macosopencvvideo-capture

CVcapture From 2 WebCams


I am taking input from the camera. to be more clear i added a photo: 2 cameras that connected on the same usb port enter image description here

with OpenCV as the following :

#define CamLeft 2
#define CamRight 0
#define WIN_L "win_l"
#define WIN_R "win_r"

int main(int argc, const char * argv[])
{
   VideoCapture capLeft(CamLeft);
   bool opened = capLeft.isOpened();
   if(!opened /*|| !capRight.isOpened()*/)  // check if we succeeded
      return -1;

   Mat edges;
   namedWindow(WIN_L,1);
   for(;;)
   {
       Mat frameL;
       Mat frameR;
       capLeft >> frameL; // get a new frame from camera
       cvtColor(frameL, edges, CV_RGB2RGBA);
       imshow(WIN_L, edges);
         if(waitKey(30) >= 0) break;
   }
   return 0;
}

So I am creating a window named "win_l" stands for window left and process video capture. It works well. Now I upgraded my code to support another camera like this:

int main(int argc, const char * argv[])
{
    VideoCapture capLeft(CamLeft);
    VideoCapture capRight(CamRight);
    bool opened = capLeft.isOpened();
    if(!opened /*|| !capRight.isOpened()*/)  // check if we succeeded
        return -1;

    Mat edges;
    namedWindow(WIN_L,1);
    namedWindow(WIN_R,1);
    for(;;)
    {
        Mat frameL;
        Mat frameR;
        capLeft >> frameL; // get a new frame from camera
        cvtColor(frameL, edges, CV_RGB2RGBA);
        imshow(WIN_L, edges);
        imshow(WIN_R, edges);
        if(waitKey(30) >= 0) break;
    }
    return 0;
}

But then I don't see the debugger hit this line: bool opened.... is it the correct way to take capture from 2 cameras?


Solution

  • I suspect USB bandwidth allocating problem. Not very sure if that is the cause though, but it is usually the case for 2 separate camera with individual USB cable each.

    But still, do give it a shot. Some methods to overcome that problem: 1)put a Sleep(ms) in between the lines of your capture line. 2)Use lower resolution which would reduce the bandwidth used by each camera. 3)Use MJPEG format(compressed frames).

    EDIT:

    I came across this website, WEBCAM AND USB DRIVERS, thought you might like to read it.

    Anyway, I am not sure if both cameras are able to run concurrently with each other only through one USB port and the reason is as mentioned(I actually forgotten about it. My bad):

    USB: Universal Serial Bus, as the name suggest, it is a serial data transmission, meaning, that it can only send data from one device to/and from the computer.

    What happening in your code is a deadlock, causing your program to not progress, even on one camera.

    2 reasons that causes this:

    1)I suspect that the driver for each camera is consistently overwriting each other, sending an I/O Request Packet (IRP), and the "pipe" keeps on receiving and processing each request, and the system keep deallocating each resource immediately after it is allocated. This goes on and on.

    2)There is starvation from the 2nd camera, where if it is not allocated the resource, the program cannot progress. Hence the system is stuck in a loop where the 2nd camera keep requesting for the resource.

    However, it would be weird if somebody invented this 2 webcam using a single serial bus when each camera needs a channel/pipe of it's own to communicate with the system. Maybe you would like to check with the manufacturer straight? Maybe they came up with a workaround, hence they invented this camera?

    If not, the only purpose for this invention is pretty redundant. I can only think of a security purpose, one configured for night view and one configured for day view.