Joe Longstreet
Joe Longstreet

Reputation: 374

Capturing a frame from multiple usb cameras - OpenCV, C++

I'm attempting to capture a single frame from several USB cameras connected to a USB hub. I've written something which I believe should work, but it doesn't :(. The final result produces the correct number of images based on the argument, but all except the final are "blank".

#include <opencv2/highgui/highgui.hpp>

#include <stdio.h>
#include <iostream>
using namespace cv;


int main(int argc, char** argv){

  int numberOfCameras = atoi(argv[1]);
  std::vector<Mat> frames (numberOfCameras-1);

  for(int i=0; i<numberOfCameras; i++){
    VideoCapture cap(i);
    cap.set(CV_CAP_PROP_FRAME_WIDTH,320);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT,240);
    Mat frame; cap >> frame;

    frames.push_back(frame);
  }

  for(int i=0; i<numberOfCameras; i++){
    string fileName = std::to_string(i) + ".jpg";
    std::cout << fileName << std::endl;
    imwrite(fileName, frames[i]);
  }
}

What's going on here? I'm kinda new to C++ and typed languages in general. Am I misunderstanding the use of a vector? Is the above the best method of accomplishing my task?

I'll be connecting several more cameras (a total of 50) and am curious as to the repercussions of storing 50 frames in memory. Can I even open call capture on that many cameras?

Upvotes: 0

Views: 5215

Answers (3)

Mohamed Tahir
Mohamed Tahir

Reputation: 131

In a project I'm doing, I have tried to use four USB cameras simultaneously, but it didn't work.

I only managed to connect two cameras simultaneously.

You basically have two options:

1- To juggle the cameras, i.e. capture one camera, read one frame, then release it and move on to the next camera ....

2- Keep one camera captured at all times (the one that is the most important to you) and juggle the rest as stated in (1).

Although, you could try connecting an external power supply to your USB hub, which may help you get three cameras captured simultaneously, but I very much doubt that you'll be able to capture more than four cameras with OpenCV no matter what.

I have tried to do that with different cameras, with different (separate) USB ports and different USB hubs. The maximum I could get is four different cameras each connected to a different USB port, and it didn't always work.

Considerations: Power, bandwidth and OpenCV's own limitations.

BTW: I'm using OpenCV 2.4.8 with Windows 7 64-bit on an Intel i5 laptop with 4GB of RAM.

Upvotes: 2

thomas_haximus
thomas_haximus

Reputation: 1531

It could very well be that you are limited by your hardware. USB camera's have the tendency to take as much bandwidth from a USB bus as possible, so connecting more than one by using a USB hub might not be a good idea.

Try to connect only two camera's first and leave the hub out of your setup for now. if you are lucky your system has two separate USB buses and you can find them by trying to connect the camera cables in all possible ways. Check your mainboard/laptop manual to see if it has separate buses. You can also find a tutorial for your operating system to see how you can find out if they are present on the system.

If you have two buses and are still unable to get images from two cams, the problem might be in your code. You can read more in this SO post

Upvotes: 2

berak
berak

Reputation: 39796

the frames from a webcam are special, as they point to (static) memory inside the videocapture / driver.

since you create and destroy the VideoCapture in your loop, the frames stored in your vector will be invalid (the memory they're pointing to is gone)

to make your plan work, you need to clone() the frame before storing it (so it keeps its own copy of the pixels)

frames.push_back(frame.clone()); 

Upvotes: 4

Related Questions