Skip to content

Commit

Permalink
Merge pull request #1511 from rolalaro/canny_stack
Browse files Browse the repository at this point in the history
Canny stack
  • Loading branch information
fspindle authored Nov 29, 2024
2 parents 81f0cf4 + 6f52d6a commit ae840eb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
2 changes: 2 additions & 0 deletions modules/core/include/visp3/core/vpCannyEdgeDetection.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ class VISP_EXPORT vpCannyEdgeDetection

/**
* \brief Set the minimum stack size, expressed in bytes, due to the recursive algorithm.
* If not called, the stack size is left at its default value when running the
* Canny edge detection algorithm.
*
* \note The stack size is changed back to its original value after
* before leaving the detect() function.
Expand Down
47 changes: 26 additions & 21 deletions modules/core/src/image/vpCannyEdgeDetection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ vpCannyEdgeDetection::vpCannyEdgeDetection()
, m_upperThreshold(-1.f)
, m_upperThresholdRatio(0.8f)
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
, m_minStackSize(65532000) // Maximum stack size on MacOS, see https://stackoverflow.com/a/13261334
, m_minStackSize(0) // Deactivated by default
#endif
, mp_mask(nullptr)
{
Expand All @@ -128,7 +128,7 @@ vpCannyEdgeDetection::vpCannyEdgeDetection(const int &gaussianKernelSize, const
, m_upperThreshold(upperThreshold)
, m_upperThresholdRatio(upperThresholdRatio)
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
, m_minStackSize(65532000) // Maximum stack size on MacOS, see https://stackoverflow.com/a/13261334
, m_minStackSize(0) // Deactivated by default
#endif
, m_storeListEdgePoints(storeEdgePoints)
, mp_mask(nullptr)
Expand All @@ -143,7 +143,7 @@ using json = nlohmann::json;

vpCannyEdgeDetection::vpCannyEdgeDetection(const std::string &jsonPath) :
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
m_minStackSize(65532000) // Maximum stack size on MacOS, see https://stackoverflow.com/a/13261334
m_minStackSize(0) // Deactivated by default
#endif
{
initFromJSON(jsonPath);
Expand Down Expand Up @@ -255,23 +255,26 @@ vpImage<unsigned char>
vpCannyEdgeDetection::detect(const vpImage<unsigned char> &I)
{
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
// Increase stack size due to the recursive algorithm
rlim_t initialStackSize;
struct rlimit rl;
int result;
result = getrlimit(RLIMIT_STACK, &rl);
if (result == 0) {
initialStackSize = rl.rlim_cur;
if (rl.rlim_cur < m_minStackSize) {
rl.rlim_cur = m_minStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result != 0) {
throw(vpException(vpException::fatalError, "setrlimit returned result = %d\n", result));
if (m_minStackSize > 0) {
// Check the current stack size
result = getrlimit(RLIMIT_STACK, &rl);
if (result == 0) {
initialStackSize = rl.rlim_cur;
if (rl.rlim_cur < m_minStackSize) {
// Increase stack size due to the recursive algorithm
rl.rlim_cur = m_minStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result != 0) {
throw(vpException(vpException::fatalError, "setrlimit returned result = %d\n", result));
}
}
}
}
else {
throw(vpException(vpException::fatalError, "getrlimit returned result = %d\n", result));
else {
throw(vpException(vpException::fatalError, "getrlimit returned result = %d\n", result));
}
}
#endif
// // Clearing the previous results
Expand Down Expand Up @@ -309,13 +312,15 @@ vpCannyEdgeDetection::detect(const vpImage<unsigned char> &I)
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));
if (m_minStackSize > 0) {
if (rl.rlim_cur > initialStackSize) {
// Reset stack size to its original value
rl.rlim_cur = initialStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result != 0) {
throw(vpException(vpException::fatalError, "setrlimit returned result = %d\n", result));

}
}
}
#endif
Expand Down
3 changes: 2 additions & 1 deletion tutorial/image/tutorial-canny.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ void checkEdgeList(const vpCannyEdgeDetection &cannyDetector, const vpImage<unsi
// Check if the edge points are uniquely present in the edge lists
std::vector<vpImagePoint> cpyListEdgePoints = listEdgePoints;
std::sort(cpyListEdgePoints.begin(), cpyListEdgePoints.end(), sortImagePoints);
std::unique(cpyListEdgePoints.begin(), cpyListEdgePoints.end());
std::vector<vpImagePoint>::iterator last = std::unique(cpyListEdgePoints.begin(), cpyListEdgePoints.end());
static_cast<void>(cpyListEdgePoints.erase(last, cpyListEdgePoints.end()));
if (listEdgePoints.size() != cpyListEdgePoints.size()) {
throw(vpException(vpException::fatalError, "There are duplicated points in the edge points list !"));
}
Expand Down

0 comments on commit ae840eb

Please sign in to comment.