Ask Your Question

cv::FindContours() spiking CPU usage to the limit

asked 2017-08-01 12:44:35 -0500

Pedro Batista gravatar image

updated 2017-08-02 04:55:30 -0500

While developing some stuff that uses blob analysis , I realized my application was dropping frames. After evaluating what might be the cause, I singled out the findContours call as the culprit.

I created this small code snippet to understand what was going on,

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
int main()
    while ((char) cv::waitKey(30) != 'q')
        cv::Size size = cv::Size(640, 480);

        cv::Mat testFindContours = cv::Mat::zeros(size, CV_8UC1);

        std::vector<std::vector<cv::Point> > contours;
        std::vector<cv::Vec4i> hierarchyObj;

        cv::findContours(testFindContours, contours, hierarchyObj, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);

        cv::imshow("Black window", testFindContours);

    return 0;

Weirdly enough, just by using findContours on a 640x480 completely black image, my CPU spikes to ~300% usage (more than one CPU cores at full speed).


The number on the most right-hand side is the CPU usage % (from "top" in linux)

Even weirder, is when I reduced the Size to 320x240, now the CPU usage stayes below 2%


I started playing a bit with the size, and I have the impression that there is a specific size at which the CPU spikes up to 300%, but I didn't go far enough to check which size that is exactly, so I am not completely sure about this.

What am I missing here? Is it normal that findContours even consumes anything on a completely black image? Might this be a bug?

The CPU I am doing these tests in is a intel core i7 3.4 ghz and opencv version 3.2.0. I'm compiling this code in Debug configuration.

Best regards.

edit retag flag offensive close merge delete



I copy and paste your program and I cannot see any problem I have got 17% of cpu with animage of 20480*15360. for 640x480 it is only 0.4% in debug mode

LBerger gravatar imageLBerger ( 2017-08-01 13:59:56 -0500 )edit

Well, those results are what I would expect to happen. Don't understand why I am facing this, really annoying.

Pedro Batista gravatar imagePedro Batista ( 2017-08-02 04:59:24 -0500 )edit

try to disable ipp when you build opencv

LBerger gravatar imageLBerger ( 2017-08-02 05:15:33 -0500 )edit

For some reason in particular? Will try that later.

It is interesting that I can use any opencv functions (morphology, etc) on huge images and my processor keeps below 50%. As soon as I use a findContours() call it goes up by 300%.

Pedro Batista gravatar imagePedro Batista ( 2017-08-04 06:02:12 -0500 )edit

Ipp in debug mode generate a lot of exception. Update to 3.3.0-rc

LBerger gravatar imageLBerger ( 2017-08-04 07:39:09 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-08-12 08:30:32 -0500

Pedro Batista gravatar image

Compiled OpenCV 3.3 from source and this issue was solved.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-08-01 12:44:35 -0500

Seen: 129 times

Last updated: Aug 12