为什么在 C++ 上使用 OpenCV 时 CPU 占用率较高?比 Python 上的
我使用的是 Ubuntu 20.04.4,并且我将 OpenCV 编译为发布模式。每当我读取帧时,它都会消耗大量的 CPU。我也在其他机器上测试过这一点。然而,在 python 上使用非常相似的脚本,它使用的 CPU 少得多。我发现这个问题似乎有和我类似的问题。虽然我用的是Release版本。
另外,我的 python 似乎使用与我编译的版本相同的 OpenCV 版本:4.5.5。
下面是 C++ 测试代码:
#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);
}
}
和 python 代码:
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()
python 脚本消耗了大约 10% 的 CPU,而 C++ 消耗了大约 30%。我在 CPU 资源至关重要的环境中工作。我想知道是否有任何方法可以减少这种使用。我错过了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢@christophrackwitz。显然,正是FourCC配置引起了高CPU使用。使用1920x1080的分辨率将使用默认的

YUYV
编码将FPS限制在5。这可能就是为什么我使用Python获得较低的CPU使用情况的原因。如果我将python上的fourcc设置为
mjpg
cpu用法尖峰。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.