Opencv videocapture can not open 4 cameras simultaneously

55 views Asked by At

I have four usb camers mounted via usb hub. they are video4,video6,video8 and video10. When I open the camera one by one, they works fine.

When I open three cameras together, they still works fine.

But when I open four cameras together, the fourth camera can not capture anything.

Here is my code:

#include <opencv2/opencv.hpp>
int main()
{
    cv::VideoCapture cap1(10, cv::CAP_V4L);
    cap1.set(cv::CAP_PROP_FRAME_WIDTH, 352);
    cap1.set(cv::CAP_PROP_FRAME_HEIGHT, 288);
    cap1.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));

    cv::VideoCapture cap2(8, cv::CAP_V4L);
    cap2.set(cv::CAP_PROP_FRAME_WIDTH, 352);
    cap2.set(cv::CAP_PROP_FRAME_HEIGHT, 288);
    cap2.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));

    cv::VideoCapture cap3(6, cv::CAP_V4L2);
    cap3.set(cv::CAP_PROP_FRAME_WIDTH, 352);
    cap3.set(cv::CAP_PROP_FRAME_HEIGHT, 288);
    cap3.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));

    cv::VideoCapture cap4(4, cv::CAP_V4L2);
    cap4.set(cv::CAP_PROP_FRAME_WIDTH, 352);
    cap4.set(cv::CAP_PROP_FRAME_HEIGHT, 288);
    cap4.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
    while (true)
    {
        cv::Mat img1, img2, img3, img4;
        cap1.grab();
        cap1.retrieve(img1);
        cap2.grab();
        cap2.retrieve(img2);
        cap3.grab();
        cap3.retrieve(img3);
        cap4.grab();
        cap4.retrieve(img4);
        
     
        

        if (img1.cols > 0)
        {
            cv::imshow("1", img1);
            cv::waitKey(50);
        }
        if (img2.cols > 0)
        {
            cv::imshow("2", img2);
            cv::waitKey(50);
        }
        if (img3.cols > 0)
        {
            cv::imshow("3", img3);
            cv::waitKey(50);
        }
        if (img4.cols > 0)
        {
            cv::imshow("4", img4);
            cv::waitKey(50);
        }

    }

    return 1;
}

the output like this: outputimg

only first three images can be obtained.the fourth camera capture nothing.

Do you have any suggestions? Thanks for your attentation.

my usb devices is shown in this figure: lsusb -t lsusb

I have tried to open camera by V4L2, and change the resolution. the fourth camera still not work.

0

There are 0 answers