Connecting 2 camera for stereo vision

asked 2017-11-21 04:45:56 -0500

Vasyl gravatar image

updated 2017-11-21 04:48:05 -0500

berak gravatar image

Hello. I do not know what to do when one camera is connected is working perfectly. But when I connect the second camera, the system outputs one frame and freezes.

    OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /opt/opencv/modules/highgui/src/window.cpp, line 331
terminate called after throwing an instance of 'cv::Exception'
  what():  /opt/opencv/modules/highgui/src/window.cpp:331: error: (-215) size.width>0 && size.height>0 in function imshow

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "opencv2/opencv.hpp"
git clone
mkdir build
cd build
make -j
sudo make install

int main(int argc, char **argv)
    //const char *pipeline = "tcambin serial=10710002 ! video/x-raw, format=GRAY8, framerate=30/1, width=640, height=480  ! videoconvert ! appsink";
    //const char *pipeline = "v4l2src ! video/x-raw, format=GRAY8, width=640, height=480, framerate=30/1 ! videoconvert ! appsink";
    //const char *pipeline = "videotestsrc ! appsink";
    const char *pipeline_left =  "tcamsrc serial=19710151 ! video/x-bayer,width=640, height=480 ! bayer2rgb ! videoconvert ! appsink";
    const char *pipeline_right = "tcamsrc serial=19710152 ! video/x-bayer,width=640, height=480 ! bayer2rgb ! videoconvert ! appsink";

    cv::VideoCapture *cap_1 = new cv::VideoCapture(pipeline_left);
    cv::VideoCapture *cap_2 = new cv::VideoCapture(pipeline_right);

    if (!cap_1->isOpened())
        printf("Error can't create video capture left");
        return 1;
    if (!cap_2->isOpened())
        printf("Error can't create video capture right");
        return 1;


        cv::Mat frame_left;
        cv::Mat frame_right;

        *cap_1 >> frame_left; // get a new frame from camera
        *cap_2 >> frame_right;

        cv::imshow("Left",  frame_left);
        cv::imshow("Right", frame_right);

        char c= cv::waitKey(20);
        if(c ==' ')
    delete cap_1;
    delete cap_2;


edit retag flag offensive close merge delete


one of your captures failed / was closed. add checks like:

if (frame_left.empty()) // print error, and break

then, it's time to analyze which one it was, and why.

berak gravatar imageberak ( 2017-11-21 04:50:41 -0500 )edit

If the cameras are used separately, they work. I do not know where to look for an error

Vasyl gravatar imageVasyl ( 2017-11-21 05:00:15 -0500 )edit

I use a similar stereo vision system and opencv code, it works without problem.

Did you try to open cameras directly using the V4L path? Something like:

VideoCapture cap1(0);
VideoCapture cap1("/dev/video0");

If you are afraid not to mix up the cameras, you can use the /dev/by-path/... ot /dev/by-id/... path.

kbarni gravatar imagekbarni ( 2017-11-22 10:01:27 -0500 )edit