Ask Your Question

Revision history [back]

Capture from two webcam

i have two web cams ( Laptop's cam and an USB webcam )

i tried to capture from two cams at the same time with the code below.

it is working but one of the cams is lagging.

when i make two exe file ( one exe capturing from cam 0 other from cam 1 ) and run two exe at the same time, both cam streaming without lagging.

any advice to this problem ?

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, const char * argv[])
{
    VideoCapture cap0(0);
    VideoCapture cap1(1);

    Mat frame0, frame1;

    while (cap0.read(frame0))
    {
        cap1 >> frame1;

        if (!frame0.empty())
        {
            imshow("cap0", frame0);
        }

        if (!frame1.empty())
        {
            imshow("cap1", frame1);
        }

        int key = waitKey(10);
        if (key == 27)
            break;
    }
}

one

Capture from two webcam

i have two web cams ( Laptop's cam and an USB webcam )

i tried to capture from two cams at the same time with the code below.

it is working but one of the cams is lagging.

when i make two exe file ( one exe capturing from cam 0 other from cam 1 ) and run two exe at the same time, both cam streaming without lagging.

any advice to this problem ?

EDIT: i edited the code according to @pklab 's comment.

yes! i get better result with this modification.

but still not good as two running exe :(

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, const char * argv[])
{
    VideoCapture cap0(0);
    VideoCapture cap1(1);

    cap0.set(CV_CAP_PROP_FPS, 15);
    cap1.set(CV_CAP_PROP_FPS, 15);

    Mat frame0, frame1;

    while (cap0.read(frame0))
for (;;)
    {
        cap1 >> frame1;

        if (!(cap0.grab() && cap0.grab())) {
            std::cerr << "Unable to grab from one or both cameras";
            break;
        }

        cap0.retrieve(frame0);
        cap1.retrieve(frame1);

        if (!frame0.empty())
        {
            imshow("cap0", frame0);
        }

        if (!frame1.empty())
        {
            imshow("cap1", frame1);
        }

        int key = waitKey(10);
        if (key == 27)
            break;
    }
}

one

Capture from two webcam

i have two web cams ( Laptop's cam and an USB webcam )

i tried to capture from two cams at the same time with the code below.

it is working but one of the cams is lagging.

when i make two exe file ( one exe capturing from cam 0 other from cam 1 ) and run two exe at the same time, both cam streaming without lagging.

any advice to this problem ?

EDIT: i edited the code according to @pklab 's comment.

yes! i get better result with this modification.

but still not good as two running exe :(

Note:

running on Windows 10

Video I/O: DirectShow

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, const char * argv[])
{
    VideoCapture cap0(0);
    VideoCapture cap1(1);

    cap0.set(CV_CAP_PROP_FPS, 15);
    cap1.set(CV_CAP_PROP_FPS, 15);

    Mat frame0, frame1;

    for (;;)
    {

        if (!(cap0.grab() && cap0.grab())) {
            std::cerr << "Unable to grab from one or both cameras";
            break;
        }

        cap0.retrieve(frame0);
        cap1.retrieve(frame1);

        if (!frame0.empty())
        {
            imshow("cap0", frame0);
        }

        if (!frame1.empty())
        {
            imshow("cap1", frame1);
        }

        int key = waitKey(10);
        if (key == 27)
            break;
    }
}

Capture from two webcam

i have two web cams ( Laptop's cam and an USB webcam )

i tried to capture from two cams at the same time with the code below.

it is working but one of the cams is lagging.

when i make two exe file ( one exe capturing from cam 0 other from cam 1 ) and run two exe at the same time, both cam streaming without lagging.

any advice to this problem ?

EDIT: i edited the code according to @pklab 's comment.

yes! i get better result with this modification.

but still not good as two running exe :(

Note:

running on Windows 10

Video I/O: DirectShow

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, const char * argv[])
{
    VideoCapture cap0(0);
    VideoCapture cap1(1);

    cap0.set(CV_CAP_PROP_FPS, 15);
    cap1.set(CV_CAP_PROP_FPS, 15);

    Mat frame0, frame1;

    for (;;)
    {

        if (!(cap0.grab() && cap0.grab())) {
            std::cerr << "Unable to grab from one or both cameras";
            break;
        }

        cap0.retrieve(frame0);
        cap1.retrieve(frame1);

        if (!frame0.empty())
        {
            imshow("cap0", frame0);
        }

        if (!frame1.empty())
        {
            imshow("cap1", frame1);
        }

        int key = waitKey(10);
        if (key == 27)
            break;
    }
}