How to use more than two cameras?

asked 2018-11-27 07:46:30 -0500

Vigen gravatar image

updated 2018-11-27 09:52:44 -0500

LBerger gravatar image

How to use more than two cameras?

image description

I tried on with C++, on Java, result one. More than two cameras bug. I try to just save images from the camera to a folder.

void CameraThread(int id) {

    int sensivity = 30;
    double maxArea = 30;

    int widthFrame = 320;
    int heightFrame = 160;

    // для работы со списком камер
    Mat *frameI = new Mat(heightFrame, widthFrame, CV_8UC3);
    Mat *frameCurrentI = new Mat(heightFrame, widthFrame, CV_8UC3);
    Mat *framePreviousI = new Mat(heightFrame, widthFrame, CV_8UC3);
    Mat *frameResultI = new Mat(heightFrame, widthFrame, CV_8UC3);


    Size *size = new Size(3, 3);
    Mat *v = new Mat();

    // open camera
    std::cout << "thread camera id " << id << std::endl;

    VideoCapture cap(id);

    cap.set(CAP_PROP_FRAME_WIDTH, widthFrame);
    cap.set(CAP_PROP_FRAME_HEIGHT, heightFrame);


    std::string filename; 
    filename.append("camera_");
    filename.append(std::to_string(id));
    filename.append(".jpg");

    Mat frame;

    while (true) {

        if (!cap.grab()) {
            printf("failed to grab from camera\n");
        }
        else
        {
            cap.read(frame);

            if (frame.empty()) {
                printf("failed to grab from camera\n");
            }
            else {
                imwrite(filename, frame);
            }
        }

        /*if (cap.read(frame)) {
            m.lock();
            cap >> frame;
            imwrite(filename, frame);
            m.unlock();
        } */

        //Sleep(1000);

    }
}

int main()
{
    std::thread t0(CameraThread, 2);
    t0.detach();

    std::thread t1(CameraThread, 1);
    t1.detach();

    std::thread t2(CameraThread, 2);
    t2.detach();

    std::thread t3(CameraThread, 3);
    t3.detach();

    std::thread t4(CameraThread, 4);
    t4.detach();

    std::thread t5(CameraThread, 5);
    t5.detach();

    std::thread t6(CameraThread, 6);
    t6.join();

    waitKey();
    return 0;
}
edit retag flag offensive close merge delete

Comments

Please remove screenshot and show us your code as text

LBerger gravatar imageLBerger ( 2018-11-27 07:57:13 -0500 )edit
1
  • a single VideoCapture can only be used from a single thread (you're re-using id 2)
  • an ordinary usb2 hub can "just so" handle the troughput of a single hd webcam. with 6 of it, you have a serious bandwidth (hardware) problem.

  • please NEVER use pointers to cv::Mat, or new here. (you've a memleak, due to this)

berak gravatar imageberak ( 2018-11-27 10:00:41 -0500 )edit
1

Thanks for answering so quickly. 2 - this is what I tested, it should be 0. If it is not possible, then I see one way, just go over all the cameras, take a screenshot and close it. at the expense of "new" the same thank you.

Vigen gravatar imageVigen ( 2018-11-29 12:24:05 -0500 )edit