This cam doesn't support synchronization between left and right... from specs: L/R shutters don’t synchronize(Max deviation 16.5 ms)
So you will have minimum 15.6 ms of delay between images. To get the best from your cam you could use 2 grabbing threads but it would be really complicated to keep sync.
Considered your hardware I would suggest to use standard grabbing loop with grab()
and retrieve()
sequence instead of >>
or read()
... from the doc: The primary use of the grab()
function is in multi-camera environments, especially when the cameras do not have hardware synchronization
EDIT about transfer rate
You can't get 2 color cams 640x480@30fs on same USB2 port because transfer rate is too high for a single USB2 connection.
For 1 cam you need:
transferRate = 3chan * width * height * fps/BytesForMByte = 3*640*480*30/1024^2 = 26.37MB/s
Because the 2 cams share same USB port your requirement is 52MB/s.
Theoretical max speed for USB2 is 480Mbit/s (60MByte/s). Real speed depends on controller quality, on data packet overhead and if it's shared with other devices (hubs, keyboard, mouse,...). In addition, common USB2 is half duplex so receiving delays transmission and reverse. On my experience, typical transfer rate for USB2 is less than 35MB/s. That's why it doesn't work.
Conclusion: to use a stereo cam on a single USB2 connection we have to reduce data (lower resolution or fps or channels)... let me try to reduce fps:
maxFps <= (maxUsbSpeed * 1024^2) / (cams*3chan*width*height)
maxFps <= (35 * 1024^2) / (2*3*640*480) <= 19fps
Below is a sample code to grab from 2 cams and save the mosaic video file
void Main_GrabFrom2CamWriteMosaic()
{
// SELECT CAM ID FOR YOUR CAMS
int camL = 0, camR = 1;
// CHOOSE YOUR BEST DRIVER
int driver = CV_CAP_DSHOW; //use DSHOW or VFW or MSMF
// OPEN THE DEVICES
VideoCapture capL(camL + driver);
VideoCapture capR(camR+ driver);
//check if we succeeded
if (! (capL.isOpened() && capR.isOpened())) {
cerr << "Unable to open the cameras" << endl;
capL.release(); capR.release();
return;
}
//SET SAME SETTINGS ON BOTH CAM
Size frameSize(640, 480);
double fps = 15;
capL.set(CV_CAP_PROP_FRAME_WIDTH, frameSize.width);
capL.set(CV_CAP_PROP_FRAME_HEIGHT, frameSize.height);
capL.set(CV_CAP_PROP_FPS, fps); //desired FPS
capR.set(CV_CAP_PROP_FRAME_WIDTH, frameSize.width);
capR.set(CV_CAP_PROP_FRAME_HEIGHT, frameSize.height);
capR.set(CV_CAP_PROP_FPS, fps); //desired FPS
// GRAB ONCE TO GET FRAME INFO (we'll lost this frames)
Mat imgL, imgR;
if (!(capL.read(imgL) && capR.read(imgR))) {
std::cerr << "Unable to grab from some CAM";
capL.release(); capR.release();
return;
}
if ((imgL.size() != imgR.size()) || (imgL.type() != imgR.type())) {
cerr << "The cameras uses different framesize or type" << endl;
capL.release(); capR.release();
return;
}
// ENSURE RIGHT TYPE AND SIZE
frameSize = imgL.size();
// this will hold images from both cams
Mat frame(Size(2 * frameSize.width, frameSize.height), imgL.type());
// define ROI to compose mosaic
Mat frameL(frame(Rect(0, 0, frameSize.width, frameSize.height)));
Mat frameR(frame(Rect(frameSize.width, 0, frameSize.width, frameSize.height)));
// IN CASE YOU ...
(more)