Skip to content

Commit

Permalink
[FIX] Fixed Canny auto-threshold
Browse files Browse the repository at this point in the history
Throw an exception when the Canny's ratio are outside the expected boundaries
  • Loading branch information
LAGNEAU Romain committed Oct 30, 2024
1 parent 3d1ed82 commit 1843fcb
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion modules/core/include/visp3/core/vpImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,25 @@ class VISP_EXPORT vpImageFilter
const vpImage<OutType> *p_dIx = nullptr, const vpImage<OutType> *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<bool> *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<unsigned char> dI(h, w);
vpImage<OutType> dIx(h, w), dIy(h, w);
if ((p_dIx != nullptr) && (p_dIy != nullptr)) {
Expand Down Expand Up @@ -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<float>(bon, 1.f);
lowerThresh = lowerThresholdRatio * bon;
lowerThresh = std::max<float>(lowerThresh, std::numeric_limits<float>::epsilon());
return upperThresh;
}

Expand Down

0 comments on commit 1843fcb

Please sign in to comment.