Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canny stack #1511

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
, 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

Check warning on line 106 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L106

Added line #L106 was not covered by tests
#endif
, mp_mask(nullptr)
{
Expand All @@ -128,7 +128,7 @@
, 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

Check warning on line 131 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L131

Added line #L131 was not covered by tests
#endif
, m_storeListEdgePoints(storeEdgePoints)
, mp_mask(nullptr)
Expand All @@ -143,7 +143,7 @@

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

Check warning on line 146 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L146

Added line #L146 was not covered by tests
#endif
{
initFromJSON(jsonPath);
Expand Down Expand Up @@ -255,23 +255,26 @@
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 warning on line 261 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L261

Added line #L261 was not covered by tests
// Check the current stack size
result = getrlimit(RLIMIT_STACK, &rl);
if (result == 0) {
initialStackSize = rl.rlim_cur;
if (rl.rlim_cur < m_minStackSize) {

Check warning on line 266 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L263-L266

Added lines #L263 - L266 were not covered by tests
// 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));

Check warning on line 271 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L268-L271

Added lines #L268 - L271 were not covered by tests
}
}
}
}
else {
throw(vpException(vpException::fatalError, "getrlimit returned result = %d\n", result));
else {
throw(vpException(vpException::fatalError, "getrlimit returned result = %d\n", result));

Check warning on line 276 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L276

Added line #L276 was not covered by tests
}
}
#endif
// // Clearing the previous results
Expand Down Expand Up @@ -309,13 +312,15 @@
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) {

Check warning on line 316 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L315-L316

Added lines #L315 - L316 were not covered by tests
// 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));

Check warning on line 321 in modules/core/src/image/vpCannyEdgeDetection.cpp

View check run for this annotation

Codecov / codecov/patch

modules/core/src/image/vpCannyEdgeDetection.cpp#L318-L321

Added lines #L318 - L321 were not covered by tests

}
}
}
#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
Loading