'Why the CPU usage is higher when using OpenCV on C++ than on Python
I am using Ubuntu 20.04.4, and I compiled OpenCV as release mode. Whenever I read frames, it consumes quite a lot of my CPU. I tested this in other machines as well. However, using a very similar script on python, it uses much less CPU. I found this question that seems to have a similar problem as mine. Although I am using the Release version.
Also, my python seems to be using the same OpenCV version as the one I compiled: 4.5.5.
Here is the C++ test code:
#include "opencv2/opencv.hpp"
int main(){
cv::VideoCapture vo = cv::VideoCapture(2);
//Set fourc for better performance.
vo.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M','J','P','G'));
vo.set(cv::CAP_PROP_FPS,30);
//Setting buffersize to one will make vi.read() blocking until next frame is available.
vo.set(cv::CAP_PROP_BUFFERSIZE,1);
vo.set(cv::CAP_PROP_FRAME_WIDTH,1920);
vo.set(cv::CAP_PROP_FRAME_HEIGHT,1080);
cv::Mat frame;
while (vo.isOpened())
{
vo.read(frame);
}
}
And the python code:
import cv2
vo = cv2.VideoCapture(2)
vo.set(cv2.CAP_PROP_FPS,30)
vo.set(cv2.CAP_PROP_BUFFERSIZE,1)
vo.set(cv2.CAP_PROP_FRAME_WIDTH,1920)
vo.set(cv2.CAP_PROP_FRAME_HEIGHT,1080)
while(vo.isOpened()):
ret, frame = vo.read()
The python script consumes around 10% of my CPU while the C++ consumes around 30%. I work in an environment where CPU resource is critical. I'd like to know if there is any way to decrease this usage. Am I missing something?
Solution 1:[1]
Thanks for @ChristophRackwitz. Apparently, it was the fourcc configuration that was causing the high CPU usage. Using a resolution of 1920x1080 will cramp FPS to 5 using the default YUYV encoding. This is likely why I got lower CPU usage using python.

If I set the fourcc on python to MJPG the CPU usage spikes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hirosam |
