cv::FindContours() spiking CPU usage to the limit
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.
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
Well, those results are what I would expect to happen. Don't understand why I am facing this, really annoying.
try to disable ipp when you build opencv
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%.
Ipp in debug mode generate a lot of exception. Update to 3.3.0-rc