Frame Rate Limitation Issue When Reading Video Stream with OpenCV in C++

Hello everyone,

I am encountering a problem when using the OpenCV library in C++ to read video streams from a capture card. My video input specification is 1080p at 120fps. When using OpenCV in Python, I can successfully read the video stream at approximately 120fps. However, in C++, I’m only able to read the stream at approximately 60fps.

Here is the code snippet I am using:
opencv 4.10.0 release

int fps_test() {
    cv::VideoCapture cap(0);

    cap.set(cv::CAP_PROP_FRAME_WIDTH, 1920);
    cap.set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
    cap.set(cv::CAP_PROP_FPS, 120);
    cap.set(cv::CAP_PROP_FOCUS, cv::VideoWriter::fourcc('N', 'V', '1', '2'));

    cv::Mat frame;
    int framesCount = 0;
    auto startTime = std::chrono::high_resolution_clock::now();

    while (true) {
        if (!cap.read(frame)) {
            break;
        }

        framesCount++;

        auto currentTime = std::chrono::high_resolution_clock::now();
        if (std::chrono::duration_cast<std::chrono::seconds>(currentTime - startTime).count() >= 1) {
            std::cout << "FPS: " << framesCount << std::endl;
            framesCount = 0;
            startTime = currentTime;
        }

        //cv::imshow("Camera", frame);
        if (cv::waitKey(1) == 27) break;
    }

    cap.release();
    cv::destroyAllWindows();

    return 0;

I have checked the settings of the video capture object and ensured that there are no parameters limiting the frame rate. Additionally, I have verified the compatibility of the hardware and drivers. Is there a specific configuration or setting in C++ that could help resolve this frame rate limitation? Or are there any other suggestions to ensure that I can achieve the full 120fps video stream in C++?

I appreciate any help and advice!

Thank you!

FOCUS is wrong. did you mean CAP_PROP_FOURCC?

could you show the Python code that gives you 120 fps?

how does the performance differ if you leave various set() calls out of the program?

what’s the version of the OpenCV you’re using from Python? what’s the version you’re using from C++?

are both running on the same system? does any of them run in a VM or other virtualization/containerization?

I changed
cap.set(cv::CAP_PROP_FOCUS, cv::VideoWriter::fourcc('N', 'V', '1', '2'));
to
cap.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('N', 'V', '1', '2'));
but the actual effect did not change.

  • C++ Version: C++17
  • OpenCV Version: 4.10.0
  • Python Version: 3.6.5
  • opencv-python Version: 4.5.3.56

Here is the python code:

import cv2
import time

cap = cv2.VideoCapture(0)


print('before', cap.get(cv2.CAP_PROP_FPS))
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

if not cap.isOpened():
    exit()

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
cap.set(cv2.CAP_PROP_FPS, 120)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('N', 'V', '1', '2'))

print('after', cap.get(cv2.CAP_PROP_FPS))
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

try:
    start_time = time.time()
    frame_count = 0
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        frame_count += 1
        elapsed_time = time.time() - start_time
        if elapsed_time >= 1:
            fps = frame_count / elapsed_time
            print(f"fps: {fps:.2f}")
            frame_count = 0
            start_time = time.time()
except KeyboardInterrupt:
    pass

cap.release()
cv2.destroyAllWindows()

If set() is not used, the camera will run at the default 480p 60fps.
All of the above code is running in the same environment.