Search code examples
c++windowsopencvvideo-capture

c++ video capture using opencv


I try to capture video using Logitech C920 webcam with full hd resolution. It provides 30 fps with this resolution.

It works with windows camera application at 30 fps but whatever I try, I could not get this fps rate with opencv videoCapture.

Note: I use windows 10 and vs15. I tried with different usb ports, opencv versions and codecs. Result is same, ~5 fps.

I measured fps ignoring first 10 frames. Here are my calculations: only read = "5.04fps" , read+imshow = "4.97fps" and read+imshow+write = "4.91fps"

void main()
{
    mainStream.open(0);
    mainStream.set(CV_CAP_PROP_FRAME_WIDTH, 1920);
    mainStream.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
    mainStream.set(CV_CAP_PROP_FPS, 30);
    mainStream.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G'));
    mainWriter.open("outputnew2.avi", CV_FOURCC('M', 'J', 'P', 'G'), 30, cv::Size(frameW, frameH), true);
    namedWindow("frame", 1);

    while (true){
        Mat frame;
        mainStream >> frame;
        imshow("frame", frame);
        if (waitKey(5) == 27)
            break;
        mainWriter << frame;
    }
    mainStream.release();
    mainWriter.release();
}

Solution

  • First of all:

    The imshow method is very slow (in a pretty relative scope). Try to measure the real fps while you do not show the image and do not write the image to a file.

    After that is done, you can check the real fps and determine which one of the two options (showing or writing) is slowing down your achieved fps rate.

    Please post results of the achieved fps rate without showing or writing the image.

    Edit:

    Alright, you nearly always get 5 fps, which is kind of slow. Does the saved video (or images) match the resolution you wanted? Are they really 1920x1080 ?

    In that case, do the measured times differ from release and debug build?

    Edit2:

    If the same code works with other usb cams (and they produce more fps than the C920) my immediate suspect is the C920 itself (or its driver at least). Does it help if you deinstall the driver for it (eventually reboot) and install the newest driver again?

    Another thing: Do the measured fps change if you do not request 30 but maybe like 20 fps?

    Edit3:

    It seems it was a driver issue (merged from comments). Reinstalling the driver is one method to adress this