Ask Your Question

Revision history [back]

Can't get images from 2 cameras

Hello guys, my problem is that I can't get get images from 2 cameras. They are identical and resolution 320x240. Encoding is MJPEG which is compressed one. The code I use:

// OpenCV libraries
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/core.hpp"
#include "opencv2/shape.hpp"
#include <opencv2/aruco.hpp>

// Std libraries
#include <iostream>
#include <string>
#include <cstdlib>
#include <stdio.h>
#include <cmath>
#include <ctime>
#include <signal.h>
#include <algorithm>
#include <unistd.h>

//Namespaces
using namespace std;
using namespace cv;


bool isSingleBoard = true;
VideoCapture capL, capR;
VideoWriter leftWriter, rightWriter;

//Signal handling for SIGINT ( CTRL + C )
void my_handler(int s){
    if(!isSingleBoard)
        destroyAllWindows();
    capL.release(); // opencv camera release
    capR.release(); // opencv camera release
    //SDL_Quit(); // OpenGL release
    exit(0); 
}

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

    cv::Mat cameraMatrixL = cv::Mat(3,3, CV_64F);
    cv::Mat distCoeffsL = cv::Mat(1,4, CV_64F);

    cv::Mat cameraMatrixR = cv::Mat(3,3, CV_64F);
    cv::Mat distCoeffsR = cv::Mat(1,4, CV_64F);

    int width = 320, height = 240; // Change as you desire, care that most of the cameras are not supporting all ratios!

    // Signal is needed because capture should be released in any case. I use sigint for closing, I handled it.
    struct sigaction sigIntHandler;
    sigIntHandler.sa_handler = my_handler;
    sigemptyset(&sigIntHandler.sa_mask);
    sigIntHandler.sa_flags = 0;
    sigaction(SIGINT, &sigIntHandler, NULL);

    if(!isSingleBoard){
        capL = VideoCapture("/dev/video1", cv::CAP_V4L);
        capR = VideoCapture("/dev/video2", cv::CAP_V4L);
    }
    else{
        capL = VideoCapture("/dev/video0", cv::CAP_V4L);
        capR = VideoCapture("/dev/video1", cv::CAP_V4L);
    }

    capL.set(CV_CAP_PROP_FPS, 10);
    capR.set(CV_CAP_PROP_FPS, 10);

    capL.set(CV_CAP_PROP_FRAME_WIDTH,width);
    capL.set(CV_CAP_PROP_FRAME_HEIGHT,height);

    capR.set(CV_CAP_PROP_FRAME_WIDTH,width);
    capR.set(CV_CAP_PROP_FRAME_HEIGHT,height);

    // For unique video names.
    time_t seconds;
    seconds = time(NULL);

    string leftname, rightname;
    leftname = string("left_") + to_string(seconds) + string(".avi");
    rightname = string("right_") + to_string(seconds) + string(".avi");

    // CV_FOURCC('M','J','P','G')
    leftWriter = VideoWriter(leftname,CV_FOURCC('M','J','P','G'), 10, Size(width,height));
    rightWriter = VideoWriter(rightname,CV_FOURCC('M','J','P','G'), 10, Size(width,height));

    // BLACK BOX RIGHT CAMERA
    cameraMatrixR.at<double>(0,0) = 8.7030170819798286e+02;
    cameraMatrixR.at<double>(0,1) = 0.;
    cameraMatrixR.at<double>(0,2) = 320.;
    cameraMatrixR.at<double>(1,0) = 0.;
    cameraMatrixR.at<double>(1,1) = 8.7030170819798286e+02;
    cameraMatrixR.at<double>(1,2) = 240.;
    cameraMatrixR.at<double>(2,0) = 0.;
    cameraMatrixR.at<double>(2,1) = 0.;
    cameraMatrixR.at<double>(2,2) = 1.;

    distCoeffsR.at<double>(0,0) = -4.7851541557875366e-01;
    distCoeffsR.at<double>(0,1) = 1.0494014645561520e+00;
    distCoeffsR.at<double>(0,2) = 0.;
    distCoeffsR.at<double>(0,3) = 0.;
    distCoeffsR.at<double>(0,4) = -3.0666278646347642e+00;


    // BLACK BOX LEFT CAMERA
    cameraMatrixL.at<double>(0,0) = 8.6171166794321493e+02;
    cameraMatrixL.at<double>(0,1) = 0.;
    cameraMatrixL.at<double>(0,2) = 320.;
    cameraMatrixL.at<double>(1,0) = 0.;
    cameraMatrixL.at<double>(1,1) = 8.6171166794321493e+02;
    cameraMatrixL.at<double>(1,2) = 240.;
    cameraMatrixL.at<double>(2,0) = 0.;
    cameraMatrixL.at<double>(2,1) = 0.;
    cameraMatrixL.at<double>(2,2) = 1.;

    distCoeffsL.at<double>(0,0) = -4.0097392416691702e-01;
    distCoeffsL.at<double>(0,1) = 1.0448547619923672e-03;
    distCoeffsL.at<double>(0,2) = 0.;
    distCoeffsL.at<double>(0,3) = 0.;
    distCoeffsL.at<double>(0,4) = 1.7389061309477344e-01;




    if(!capL.isOpened())  // check if we succeeded
    {
        std::cerr << "ERROR: Could not open the left camera." << std::endl;
        return -1;
    }
    if(!capR.isOpened()){
        std::cerr << "ERROR: Could not open the right camera." << std::endl;
        return -1;
    }


    while(true){
        // Definitions
        Mat imgL;
        Mat imgR;


        cout << "start" << endl;
        // Get the images sync
        if(!capL.grab() ){
            cerr << "Left video camera is not working at the moment. Capture Error!" << endl;
            exit(1);
        }
        if(!capR.grab()){
            cerr << "Right video camera is not working at the moment. Capture Error!" << endl;
            exit(1);
        }

        // Process the images
        capL.retrieve(imgL);
        capR.retrieve(imgR);

        // Write the frame
        leftWriter << imgL;
        rightWriter << imgR;

        // Show
        if(!isSingleBoard){
            cv::imshow("Left Stream", imgL);
            cv::imshow("Right Stream", imgR);
        }

        // Get input. You can do whatever want here.
        if(!isSingleBoard){
            char key = waitKey(1);
            if(key == 'q')
                break;
        }

    }

    // Release the cams and destroy the windows
    if(!isSingleBoard){
        destroyAllWindows();
    }
    capL.release();
    capR.release();

    leftWriter.release();
    rightWriter.release();


    return 0;
}

The problem I get:

VIDIOC_STREAMON: No space left on device
Right video camera is not working at the moment. Capture Error!

I connected the cameras to Odroid C2 and I have 500 mb/s bandwith so it shouldn't be an issue. I can give any necessary info you want. I tried to drop fps to lowest as you can see. Also normally my resolution was 640x480 at the beginning. I even dropped it to 80x60 and it doesn't work. Thanks in advance.

Can't get images from 2 cameras

Hello guys, my problem is that I can't get get images from 2 cameras. They are identical and resolution 320x240. Encoding is MJPEG which is compressed one.

The problem I get:

VIDIOC_STREAMON: No space left on device
Right video camera is not working at the moment. Capture Error!

The code I use:

// OpenCV libraries
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/core.hpp"
#include "opencv2/shape.hpp"
#include <opencv2/aruco.hpp>

// Std libraries
#include <iostream>
#include <string>
#include <cstdlib>
#include <stdio.h>
#include <cmath>
#include <ctime>
#include <signal.h>
#include <algorithm>
#include <unistd.h>

//Namespaces
using namespace std;
using namespace cv;


bool isSingleBoard = true;
VideoCapture capL, capR;
VideoWriter leftWriter, rightWriter;

//Signal handling for SIGINT ( CTRL + C )
void my_handler(int s){
    if(!isSingleBoard)
        destroyAllWindows();
    capL.release(); // opencv camera release
    capR.release(); // opencv camera release
    //SDL_Quit(); // OpenGL release
    exit(0); 
}

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

    cv::Mat cameraMatrixL = cv::Mat(3,3, CV_64F);
    cv::Mat distCoeffsL = cv::Mat(1,4, CV_64F);

    cv::Mat cameraMatrixR = cv::Mat(3,3, CV_64F);
    cv::Mat distCoeffsR = cv::Mat(1,4, CV_64F);

    int width = 320, height = 240; // Change as you desire, care that most of the cameras are not supporting all ratios!

    // Signal is needed because capture should be released in any case. I use sigint for closing, I handled it.
    struct sigaction sigIntHandler;
    sigIntHandler.sa_handler = my_handler;
    sigemptyset(&sigIntHandler.sa_mask);
    sigIntHandler.sa_flags = 0;
    sigaction(SIGINT, &sigIntHandler, NULL);

    if(!isSingleBoard){
        capL = VideoCapture("/dev/video1", cv::CAP_V4L);
        capR = VideoCapture("/dev/video2", cv::CAP_V4L);
    }
    else{
        capL = VideoCapture("/dev/video0", cv::CAP_V4L);
        capR = VideoCapture("/dev/video1", cv::CAP_V4L);
    }

    capL.set(CV_CAP_PROP_FPS, 10);
    capR.set(CV_CAP_PROP_FPS, 10);

    capL.set(CV_CAP_PROP_FRAME_WIDTH,width);
    capL.set(CV_CAP_PROP_FRAME_HEIGHT,height);

    capR.set(CV_CAP_PROP_FRAME_WIDTH,width);
    capR.set(CV_CAP_PROP_FRAME_HEIGHT,height);

    // For unique video names.
    time_t seconds;
    seconds = time(NULL);

    string leftname, rightname;
    leftname = string("left_") + to_string(seconds) + string(".avi");
    rightname = string("right_") + to_string(seconds) + string(".avi");

    // CV_FOURCC('M','J','P','G')
    leftWriter = VideoWriter(leftname,CV_FOURCC('M','J','P','G'), 10, Size(width,height));
    rightWriter = VideoWriter(rightname,CV_FOURCC('M','J','P','G'), 10, Size(width,height));

    // BLACK BOX RIGHT CAMERA
    cameraMatrixR.at<double>(0,0) = 8.7030170819798286e+02;
    cameraMatrixR.at<double>(0,1) = 0.;
    cameraMatrixR.at<double>(0,2) = 320.;
    cameraMatrixR.at<double>(1,0) = 0.;
    cameraMatrixR.at<double>(1,1) = 8.7030170819798286e+02;
    cameraMatrixR.at<double>(1,2) = 240.;
    cameraMatrixR.at<double>(2,0) = 0.;
    cameraMatrixR.at<double>(2,1) = 0.;
    cameraMatrixR.at<double>(2,2) = 1.;

    distCoeffsR.at<double>(0,0) = -4.7851541557875366e-01;
    distCoeffsR.at<double>(0,1) = 1.0494014645561520e+00;
    distCoeffsR.at<double>(0,2) = 0.;
    distCoeffsR.at<double>(0,3) = 0.;
    distCoeffsR.at<double>(0,4) = -3.0666278646347642e+00;


    // BLACK BOX LEFT CAMERA
    cameraMatrixL.at<double>(0,0) = 8.6171166794321493e+02;
    cameraMatrixL.at<double>(0,1) = 0.;
    cameraMatrixL.at<double>(0,2) = 320.;
    cameraMatrixL.at<double>(1,0) = 0.;
    cameraMatrixL.at<double>(1,1) = 8.6171166794321493e+02;
    cameraMatrixL.at<double>(1,2) = 240.;
    cameraMatrixL.at<double>(2,0) = 0.;
    cameraMatrixL.at<double>(2,1) = 0.;
    cameraMatrixL.at<double>(2,2) = 1.;

    distCoeffsL.at<double>(0,0) = -4.0097392416691702e-01;
    distCoeffsL.at<double>(0,1) = 1.0448547619923672e-03;
    distCoeffsL.at<double>(0,2) = 0.;
    distCoeffsL.at<double>(0,3) = 0.;
    distCoeffsL.at<double>(0,4) = 1.7389061309477344e-01;




    if(!capL.isOpened())  // check if we succeeded
    {
        std::cerr << "ERROR: Could not open the left camera." << std::endl;
        return -1;
    }
    if(!capR.isOpened()){
        std::cerr << "ERROR: Could not open the right camera." << std::endl;
        return -1;
    }


    while(true){
        // Definitions
        Mat imgL;
        Mat imgR;


        cout << "start" << endl;
        // Get the images sync
        if(!capL.grab() ){
            cerr << "Left video camera is not working at the moment. Capture Error!" << endl;
            exit(1);
        }
        if(!capR.grab()){
            cerr << "Right video camera is not working at the moment. Capture Error!" << endl;
            exit(1);
        }

        // Process the images
        capL.retrieve(imgL);
        capR.retrieve(imgR);

        // Write the frame
        leftWriter << imgL;
        rightWriter << imgR;

        // Show
        if(!isSingleBoard){
            cv::imshow("Left Stream", imgL);
            cv::imshow("Right Stream", imgR);
        }

        // Get input. You can do whatever want here.
        if(!isSingleBoard){
            char key = waitKey(1);
            if(key == 'q')
                break;
        }

    }

    // Release the cams and destroy the windows
    if(!isSingleBoard){
        destroyAllWindows();
    }
    capL.release();
    capR.release();

    leftWriter.release();
    rightWriter.release();


    return 0;
}

The problem I get:

VIDIOC_STREAMON: No space left on device
Right video camera is not working at the moment. Capture Error!

I connected the cameras to Odroid C2 and I have 500 mb/s bandwith so it shouldn't be an issue. I can give any necessary info you want. I tried to drop fps to lowest as you can see. Also normally my resolution was 640x480 at the beginning. I even dropped it to 80x60 and it doesn't work. Thanks in advance.

Can't get images from 2 cameras

Hello guys, my problem is that I can't get get images from 2 cameras. They are identical and resolution 320x240. Encoding is MJPEG which is compressed one.

The problem I get:

VIDIOC_STREAMON: No space left on device
Right video camera is not working at the moment. Capture Error!

My df -i results:

Filesystem     Inodes IUsed  IFree IUse% Mounted on
udev           183230  1425 181805    1% /dev
tmpfs          219824  1652 218172    1% /run
/dev/mmcblk0p2 966656 72370 894286    8% /
tmpfs          219824     1 219823    1% /dev/shm
tmpfs          219824     2 219822    1% /run/lock
tmpfs          219824    16 219808    1% /sys/fs/cgroup
/dev/mmcblk0p1      0     0      0     - /media/boot
tmpfs          219824     4 219820    1% /run/user/0

The code I use:

// OpenCV libraries
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/core.hpp"
#include "opencv2/shape.hpp"
#include <opencv2/aruco.hpp>

// Std libraries
#include <iostream>
#include <string>
#include <cstdlib>
#include <stdio.h>
#include <cmath>
#include <ctime>
#include <signal.h>
#include <algorithm>
#include <unistd.h>

//Namespaces
using namespace std;
using namespace cv;


bool isSingleBoard = true;
VideoCapture capL, capR;
VideoWriter leftWriter, rightWriter;

//Signal handling for SIGINT ( CTRL + C )
void my_handler(int s){
    if(!isSingleBoard)
        destroyAllWindows();
    capL.release(); // opencv camera release
    capR.release(); // opencv camera release
    //SDL_Quit(); // OpenGL release
    exit(0); 
}

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

    cv::Mat cameraMatrixL = cv::Mat(3,3, CV_64F);
    cv::Mat distCoeffsL = cv::Mat(1,4, CV_64F);

    cv::Mat cameraMatrixR = cv::Mat(3,3, CV_64F);
    cv::Mat distCoeffsR = cv::Mat(1,4, CV_64F);

    int width = 320, height = 240; // Change as you desire, care that most of the cameras are not supporting all ratios!

    // Signal is needed because capture should be released in any case. I use sigint for closing, I handled it.
    struct sigaction sigIntHandler;
    sigIntHandler.sa_handler = my_handler;
    sigemptyset(&sigIntHandler.sa_mask);
    sigIntHandler.sa_flags = 0;
    sigaction(SIGINT, &sigIntHandler, NULL);

    if(!isSingleBoard){
        capL = VideoCapture("/dev/video1", cv::CAP_V4L);
        capR = VideoCapture("/dev/video2", cv::CAP_V4L);
    }
    else{
        capL = VideoCapture("/dev/video0", cv::CAP_V4L);
        capR = VideoCapture("/dev/video1", cv::CAP_V4L);
    }

    capL.set(CV_CAP_PROP_FPS, 10);
    capR.set(CV_CAP_PROP_FPS, 10);

    capL.set(CV_CAP_PROP_FRAME_WIDTH,width);
    capL.set(CV_CAP_PROP_FRAME_HEIGHT,height);

    capR.set(CV_CAP_PROP_FRAME_WIDTH,width);
    capR.set(CV_CAP_PROP_FRAME_HEIGHT,height);

    // For unique video names.
    time_t seconds;
    seconds = time(NULL);

    string leftname, rightname;
    leftname = string("left_") + to_string(seconds) + string(".avi");
    rightname = string("right_") + to_string(seconds) + string(".avi");

    // CV_FOURCC('M','J','P','G')
    leftWriter = VideoWriter(leftname,CV_FOURCC('M','J','P','G'), 10, Size(width,height));
    rightWriter = VideoWriter(rightname,CV_FOURCC('M','J','P','G'), 10, Size(width,height));

    // BLACK BOX RIGHT CAMERA
    cameraMatrixR.at<double>(0,0) = 8.7030170819798286e+02;
    cameraMatrixR.at<double>(0,1) = 0.;
    cameraMatrixR.at<double>(0,2) = 320.;
    cameraMatrixR.at<double>(1,0) = 0.;
    cameraMatrixR.at<double>(1,1) = 8.7030170819798286e+02;
    cameraMatrixR.at<double>(1,2) = 240.;
    cameraMatrixR.at<double>(2,0) = 0.;
    cameraMatrixR.at<double>(2,1) = 0.;
    cameraMatrixR.at<double>(2,2) = 1.;

    distCoeffsR.at<double>(0,0) = -4.7851541557875366e-01;
    distCoeffsR.at<double>(0,1) = 1.0494014645561520e+00;
    distCoeffsR.at<double>(0,2) = 0.;
    distCoeffsR.at<double>(0,3) = 0.;
    distCoeffsR.at<double>(0,4) = -3.0666278646347642e+00;


    // BLACK BOX LEFT CAMERA
    cameraMatrixL.at<double>(0,0) = 8.6171166794321493e+02;
    cameraMatrixL.at<double>(0,1) = 0.;
    cameraMatrixL.at<double>(0,2) = 320.;
    cameraMatrixL.at<double>(1,0) = 0.;
    cameraMatrixL.at<double>(1,1) = 8.6171166794321493e+02;
    cameraMatrixL.at<double>(1,2) = 240.;
    cameraMatrixL.at<double>(2,0) = 0.;
    cameraMatrixL.at<double>(2,1) = 0.;
    cameraMatrixL.at<double>(2,2) = 1.;

    distCoeffsL.at<double>(0,0) = -4.0097392416691702e-01;
    distCoeffsL.at<double>(0,1) = 1.0448547619923672e-03;
    distCoeffsL.at<double>(0,2) = 0.;
    distCoeffsL.at<double>(0,3) = 0.;
    distCoeffsL.at<double>(0,4) = 1.7389061309477344e-01;




    if(!capL.isOpened())  // check if we succeeded
    {
        std::cerr << "ERROR: Could not open the left camera." << std::endl;
        return -1;
    }
    if(!capR.isOpened()){
        std::cerr << "ERROR: Could not open the right camera." << std::endl;
        return -1;
    }


    while(true){
        // Definitions
        Mat imgL;
        Mat imgR;


        cout << "start" << endl;
        // Get the images sync
        if(!capL.grab() ){
            cerr << "Left video camera is not working at the moment. Capture Error!" << endl;
            exit(1);
        }
        if(!capR.grab()){
            cerr << "Right video camera is not working at the moment. Capture Error!" << endl;
            exit(1);
        }

        // Process the images
        capL.retrieve(imgL);
        capR.retrieve(imgR);

        // Write the frame
        leftWriter << imgL;
        rightWriter << imgR;

        // Show
        if(!isSingleBoard){
            cv::imshow("Left Stream", imgL);
            cv::imshow("Right Stream", imgR);
        }

        // Get input. You can do whatever want here.
        if(!isSingleBoard){
            char key = waitKey(1);
            if(key == 'q')
                break;
        }

    }

    // Release the cams and destroy the windows
    if(!isSingleBoard){
        destroyAllWindows();
    }
    capL.release();
    capR.release();

    leftWriter.release();
    rightWriter.release();


    return 0;
}

I connected the cameras to Odroid C2 and I have 500 mb/s bandwith so it shouldn't be an issue. I can give any necessary info you want. I tried to drop fps to lowest as you can see. Also normally my resolution was 640x480 at the beginning. I even dropped it to 80x60 and it doesn't work. Thanks in advance.