From 1843fcb78a1ceb19a287373a1f3493e093b57e81 Mon Sep 17 00:00:00 2001 From: LAGNEAU Romain Date: Wed, 30 Oct 2024 15:20:20 +0100 Subject: [PATCH] [FIX] Fixed Canny auto-threshold Throw an exception when the Canny's ratio are outside the expected boundaries --- .../core/include/visp3/core/vpImageFilter.h | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/modules/core/include/visp3/core/vpImageFilter.h b/modules/core/include/visp3/core/vpImageFilter.h index 66868f7e2b..631ca565a4 100644 --- a/modules/core/include/visp3/core/vpImageFilter.h +++ b/modules/core/include/visp3/core/vpImageFilter.h @@ -332,13 +332,25 @@ class VISP_EXPORT vpImageFilter const vpImage *p_dIx = nullptr, const vpImage *p_dIy = nullptr, const unsigned int &gaussianKernelSize = 5, const OutType &gaussianStdev = 2.f, const unsigned int &apertureGradient = 3, - const float &lowerThresholdRatio = 0.6, const float &upperThresholdRatio = 0.8, + const float &lowerThresholdRatio = 0.6f, const float &upperThresholdRatio = 0.8f, const vpCannyFilteringAndGradientType &filteringType = CANNY_GBLUR_SOBEL_FILTERING, const vpImage *p_mask = nullptr) { const unsigned int w = I.getWidth(); const unsigned int h = I.getHeight(); + if ((lowerThresholdRatio <= 0.f) || (lowerThresholdRatio >= 1.f)) { + std::stringstream errMsg; + errMsg << "Lower ratio (" << lowerThresholdRatio << ") " << (lowerThresholdRatio < 0.f ? "should be greater than 0 !" : "should be lower than 1 !"); + throw(vpException(vpException::fatalError, errMsg.str())); + } + + if ((upperThresholdRatio <= 0.f) || (upperThresholdRatio >= 1.f)) { + std::stringstream errMsg; + errMsg << "Upper ratio (" << upperThresholdRatio << ") " << (upperThresholdRatio < 0.f ? "should be greater than 0 !" : "should be lower than 1 !"); + throw(vpException(vpException::fatalError, errMsg.str())); + } + vpImage dI(h, w); vpImage dIx(h, w), dIy(h, w); if ((p_dIx != nullptr) && (p_dIy != nullptr)) { @@ -387,8 +399,14 @@ class VISP_EXPORT vpImageFilter } ++i; } + if (notFound) { + std::stringstream errMsg; + errMsg << "Could not find a bin for which " << upperThresholdRatio * 100.f << "\% of the pixels had a gradient lower than the upper threshold."; + throw(vpException(vpException::fatalError, errMsg.str())); + } float upperThresh = std::max(bon, 1.f); lowerThresh = lowerThresholdRatio * bon; + lowerThresh = std::max(lowerThresh, std::numeric_limits::epsilon()); return upperThresh; }