Skip to content

Commit 175bcb1

Browse files
authored
Merge pull request opencv#21258 from eplankin:fix_threshold_to_zero_ipp_bug
Fixed threshold(THRESH_TOZERO) at imgproc(IPP) * Fixed opencv#16085: imgproc(IPP): wrong result from threshold(THRESH_TOZERO) * 1. Added test cases with float where all bits of mantissa equal 1, min and max float as inputs 2. Used nextafterf instead of cast to hex * Used float value in test instead of hex and casts * Changed input value in test
1 parent f071207 commit 175bcb1

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

modules/imgproc/src/thresh.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,16 +774,14 @@ thresh_32f( const Mat& _src, Mat& _dst, float thresh, float maxval, int type )
774774
}
775775
setIppErrorStatus();
776776
break;
777-
#if 0 // details: https://github.com/opencv/opencv/pull/16085
778777
case THRESH_TOZERO:
779-
if (0 <= CV_INSTRUMENT_FUN_IPP(ippiThreshold_LTVal_32f_C1R, src, (int)src_step*sizeof(src[0]), dst, (int)dst_step*sizeof(dst[0]), sz, thresh + FLT_EPSILON, 0))
778+
if (0 <= CV_INSTRUMENT_FUN_IPP(ippiThreshold_LTVal_32f_C1R, src, (int)src_step*sizeof(src[0]), dst, (int)dst_step*sizeof(dst[0]), sz, nextafterf(thresh, std::numeric_limits<float>::infinity()), 0))
780779
{
781780
CV_IMPL_ADD(CV_IMPL_IPP);
782781
return;
783782
}
784783
setIppErrorStatus();
785784
break;
786-
#endif
787785
case THRESH_TOZERO_INV:
788786
if (0 <= CV_INSTRUMENT_FUN_IPP(ippiThreshold_GTVal_32f_C1R, src, (int)src_step*sizeof(src[0]), dst, (int)dst_step*sizeof(dst[0]), sz, thresh, 0))
789787
{

modules/imgproc/test/test_thresh.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,34 @@ TEST(Imgproc_Threshold, regression_THRESH_TOZERO_IPP_16085)
443443
EXPECT_EQ(0, cv::norm(result, NORM_INF));
444444
}
445445

446+
TEST(Imgproc_Threshold, regression_THRESH_TOZERO_IPP_21258)
447+
{
448+
Size sz(16, 16);
449+
float val = nextafterf(16.0f, 0.0f); // 0x417fffff, all bits in mantissa are 1
450+
Mat input(sz, CV_32F, Scalar::all(val));
451+
Mat result;
452+
cv::threshold(input, result, val, 0.0, THRESH_TOZERO);
453+
EXPECT_EQ(0, cv::norm(result, NORM_INF));
454+
}
455+
456+
TEST(Imgproc_Threshold, regression_THRESH_TOZERO_IPP_21258_Min)
457+
{
458+
Size sz(16, 16);
459+
float min_val = -std::numeric_limits<float>::max();
460+
Mat input(sz, CV_32F, Scalar::all(min_val));
461+
Mat result;
462+
cv::threshold(input, result, min_val, 0.0, THRESH_TOZERO);
463+
EXPECT_EQ(0, cv::norm(result, NORM_INF));
464+
}
465+
466+
TEST(Imgproc_Threshold, regression_THRESH_TOZERO_IPP_21258_Max)
467+
{
468+
Size sz(16, 16);
469+
float max_val = std::numeric_limits<float>::max();
470+
Mat input(sz, CV_32F, Scalar::all(max_val));
471+
Mat result;
472+
cv::threshold(input, result, max_val, 0.0, THRESH_TOZERO);
473+
EXPECT_EQ(0, cv::norm(result, NORM_INF));
474+
}
475+
446476
}} // namespace

0 commit comments

Comments
 (0)