diff --git a/modules/core/src/image/vpCannyEdgeDetection.cpp b/modules/core/src/image/vpCannyEdgeDetection.cpp index e7ffd1471b..e8a5b5f01f 100644 --- a/modules/core/src/image/vpCannyEdgeDetection.cpp +++ b/modules/core/src/image/vpCannyEdgeDetection.cpp @@ -33,6 +33,12 @@ #include +#ifdef VISP_USE_MSVC +#pragma comment(linker, "/STACK:256000000") // Increase max recursion depth +#elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX +#include +#endif + #if (VISP_CXX_STANDARD == VISP_CXX_STANDARD_98) // Check if cxx98 namespace { @@ -239,7 +245,26 @@ vpCannyEdgeDetection::detect(const vpImage &I_color) vpImage vpCannyEdgeDetection::detect(const vpImage &I) { - // // Clearing the previous results +#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX + // Increase stack size due to the recursive algorithm + const rlim_t kStackSize = 256 * 1024 * 1024; // min stack size = 256 MB + rlim_t initialStackSize; + struct rlimit rl; + int result; + result = getrlimit(RLIMIT_STACK, &rl); + if (result == 0) { + if (rl.rlim_cur < kStackSize) { + initialStackSize = rl.rlim_cur; + rl.rlim_cur = kStackSize; + result = setrlimit(RLIMIT_STACK, &rl); + if (result != 0) { + throw(vpException(vpException::fatalError, "setrlimit returned result = %d\n", result)); + + } + } + } +#endif +// // Clearing the previous results m_edgeMap.resize(I.getHeight(), I.getWidth(), 0); m_edgeCandidateAndGradient.clear(); m_edgePointsCandidates.clear(); @@ -272,6 +297,18 @@ vpCannyEdgeDetection::detect(const vpImage &I) // // Step 5: edge tracking performEdgeTracking(); + +#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX + // Reset stack size to its original value + if (rl.rlim_cur > initialStackSize) { + rl.rlim_cur = initialStackSize; + result = setrlimit(RLIMIT_STACK, &rl); + if (result != 0) { + throw(vpException(vpException::fatalError, "setrlimit returned result = %d\n", result)); + + } + } +#endif return m_edgeMap; }