From 6adf6a0dd88c462f54cf5fa6b0d4e16321c222a8 Mon Sep 17 00:00:00 2001 From: LAGNEAU Romain Date: Wed, 30 Oct 2024 15:31:12 +0100 Subject: [PATCH] [FIX] Fix Canny auto threshold Ensure that lower ratio < upper ratio --- modules/core/include/visp3/core/vpImageFilter.h | 8 +++++++- modules/core/src/image/vpImageFilter_canny.cpp | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/core/include/visp3/core/vpImageFilter.h b/modules/core/include/visp3/core/vpImageFilter.h index 631ca565a4..ee33b70e7c 100644 --- a/modules/core/include/visp3/core/vpImageFilter.h +++ b/modules/core/include/visp3/core/vpImageFilter.h @@ -351,6 +351,12 @@ class VISP_EXPORT vpImageFilter throw(vpException(vpException::fatalError, errMsg.str())); } + if (lowerThresholdRatio >= upperThresholdRatio) { + std::stringstream errMsg; + errMsg << "Lower ratio (" << lowerThresholdRatio << ") should be lower than the upper ratio (" << upperThresholdRatio << ")"; + throw(vpException(vpException::fatalError, errMsg.str())); + } + vpImage dI(h, w); vpImage dIx(h, w), dIy(h, w); if ((p_dIx != nullptr) && (p_dIy != nullptr)) { @@ -1509,6 +1515,6 @@ class VISP_EXPORT vpImageFilter } #endif -}; + }; END_VISP_NAMESPACE #endif diff --git a/modules/core/src/image/vpImageFilter_canny.cpp b/modules/core/src/image/vpImageFilter_canny.cpp index d09fa59ce2..6e7b77b1d6 100644 --- a/modules/core/src/image/vpImageFilter_canny.cpp +++ b/modules/core/src/image/vpImageFilter_canny.cpp @@ -215,6 +215,12 @@ float vpImageFilter::computeCannyThreshold(const cv::Mat &cv_I, const cv::Mat *p throw(vpException(vpException::fatalError, errMsg.str())); } + if (lowerThresholdRatio >= upperThresholdRatio) { + std::stringstream errMsg; + errMsg << "Lower ratio (" << lowerThresholdRatio << ") should be lower than the upper ratio (" << upperThresholdRatio << ")"; + throw(vpException(vpException::fatalError, errMsg.str())); + } + double w = cv_I.cols; double h = cv_I.rows; int bins = 256;