I have a C++ project using Qt 5.1.1 and OpenCV 2.4.6. The image processing algorithm runs in a separate thread. All works fine but if I call OpenCV function findContours()
the program crashes with a stack overflow message (right in the first time this function is called, not like it was already called several times before) "Unhandled exception at 0x56ec9a47 in SARA.exe: 0xC00000FD: Stack overflow."
I found someone with the same problem but in his case it was only a matter of changing the project to Visual Studio 2010...but in my case, my project is already in VS2010.
The algoritm runs fine if I create a separate console project, that just calls the image processing algorithm, but the same code inside a thread in my Qt project show a stack overflow! If I remove the findContours()
function, all woks as it should. In both projects I use the same libs and debug dlls (versioned as xxx246d.dll), and I am compiling the program as debug.
I tried to make the stack larger by changing the Properties -> Configuration Properties -> Linker -> System -> Stack Reserve Size option, but then the program still crashes, with a different message, saying "Unhandled exception at 0x76e5c41f in SARA.exe: Microsoft C++ exception: Concurrency::scheduler_resource_allocation_error at memory location 0x14c7adc8.."
I don't think it's a code problem, since it runs fine as a console aplication, but if anyone wants to see it, it goes as:
QImage SaraVisualControl::findCircles(void)
{
Mat imgInput = imread("M:/Desktop/PsEyeRight1.jpg", CV_LOAD_IMAGE_COLOR);
Mat roiInput(imgInput, Rect(Point(205, 72), Point(419,285)));
Mat imgContours = roiInput.clone();
cvtColor(imgContours, imgContours, CV_BGR2GRAY);
GaussianBlur(imgContours, imgContours, Size(3, 3), 0, 0, 4);
threshold(imgContours, imgContours, 150, 255, THRESH_BINARY); // Ou ler o Otsu uma vez e usar ele
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(imgContours, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE, Point(0, 0)); // Program crashes here!
vector<RotatedRect> ellipses;
RotatedRect ellipse;
for(int i = 0; i < contours.size(); i++)
{
if(contours[i].size() >= 5)
{
ellipse = fitEllipse(contours[i]);
ellipses.push_back(ellipse);
}
else
{
Point2f center;
float radius = 0.0;
minEnclosingCircle(contours[i], center, radius);
ellipses.push_back(RotatedRect(center, Size2f(radius, radius), 0.0));
}
}
cvtColor(imgContours, roiInput, CV_GRAY2BGR);
int baseLine = 0;
const double fontScale = 0.5;
const int thickness = 1;
for(int i = 0; i < ellipses.size(); i++)
{
cv::ellipse(roiInput, ellipses[i], CV_RGB(255, 0, 0), 1, 8);
Size textSize = getTextSize(std::to_string((long long)i + 1), FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, &baseLine);
putText(roiInput, std::to_string((long long)i + 1), Point(ellipses[i].center.x - (textSize.width/2), ellipses[i].center.y + (textSize.height/2)), FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale,
Scalar(255, 255, 255), thickness, 8, false);
}
return QImage((uchar*)roiInput.data, roiInput.cols, roiInput.rows, QImage::Format_RGB32);
}