为什么在 C++ 上使用 OpenCV 时 CPU 占用率较高?比 Python 上的

发布于 2025-01-18 05:57:08 字数 1331 浏览 2 评论 0原文

我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

近箐 2025-01-25 05:57:08

感谢@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.
enter image description here

If I set the fourcc on python to MJPG the CPU usage spikes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文