From cceeca3052bf1e38dd48dd422e9273c39bce76ce Mon Sep 17 00:00:00 2001 From: lqy123000 Date: Wed, 7 Nov 2018 00:13:48 +0800 Subject: [PATCH] Merge pull request #12916 from lqy123000:bugfix_templmatch * avoid rounding errors * imgproc: replace condition in matchTemplate --- modules/imgproc/src/templmatch.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/imgproc/src/templmatch.cpp b/modules/imgproc/src/templmatch.cpp index 1dabdb0b05..b5a08f087a 100644 --- a/modules/imgproc/src/templmatch.cpp +++ b/modules/imgproc/src/templmatch.cpp @@ -947,7 +947,12 @@ static void common_matchTemplate( Mat& img, Mat& templ, Mat& result, int method, if( isNormed ) { - t = std::sqrt(MAX(wndSum2 - wndMean2,0))*templNorm; + double diff2 = MAX(wndSum2 - wndMean2, 0); + if (diff2 <= std::min(0.5, 10 * FLT_EPSILON * wndSum2)) + t = 0; // avoid rounding errors + else + t = std::sqrt(diff2)*templNorm; + if( fabs(num) < t ) num /= t; else if( fabs(num) < t*1.125 )