diff --git a/modules/core/include/opencv2/core.hpp b/modules/core/include/opencv2/core.hpp index f58abd78fd..ccaf12f54c 100644 --- a/modules/core/include/opencv2/core.hpp +++ b/modules/core/include/opencv2/core.hpp @@ -799,7 +799,6 @@ In Python, multi-channel input is not supported at all due to a limitation in th binding generation process (there is no way to set minLoc and maxLoc to NULL). A workaround is to operate on each channel individually or to use NumPy to achieve the same functionality. -@note CV_16F/CV_16BF/CV_Bool/CV_64U/CV_64S/CV_32U are not supported for src. @param src input single-channel array. @param minVal pointer to the returned minimum value; NULL is used if not required. @param maxVal pointer to the returned maximum value; NULL is used if not required. @@ -857,7 +856,6 @@ a single-row or single-column matrix. In OpenCV (following MATLAB) each array ha dimensions, i.e. single-column matrix is Mx1 matrix (and therefore minIdx/maxIdx will be (i1,0)/(i2,0)) and single-row matrix is 1xN matrix (and therefore minIdx/maxIdx will be (0,j1)/(0,j2)). -@note CV_16F/CV_16BF/CV_Bool/CV_64U/CV_64S/CV_32U are not supported for src. @param src input single-channel array. @param minVal pointer to the returned minimum value; NULL is used if not required. @param maxVal pointer to the returned maximum value; NULL is used if not required. diff --git a/modules/core/include/opencv2/core/private.hpp b/modules/core/include/opencv2/core/private.hpp index 0ccacd6c4f..a1b7f41a89 100644 --- a/modules/core/include/opencv2/core/private.hpp +++ b/modules/core/include/opencv2/core/private.hpp @@ -122,6 +122,7 @@ namespace cv /* IEEE754 constants and macros */ #define CV_TOGGLE_FLT(x) ((x)^((int)(x) < 0 ? 0x7fffffff : 0)) #define CV_TOGGLE_DBL(x) ((x)^((int64)(x) < 0 ? CV_BIG_INT(0x7fffffffffffffff) : 0)) +#define CV_TOGGLE_FP8(x) ((x)^((schar)(x) < 0 ? 0x7f : 0)) inline void* cvAlignPtr( const void* ptr, int align = 32 ) { diff --git a/modules/core/src/minmax.dispatch.cpp b/modules/core/src/minmax.dispatch.cpp index c70ed0facb..e607fb6280 100644 --- a/modules/core/src/minmax.dispatch.cpp +++ b/modules/core/src/minmax.dispatch.cpp @@ -371,6 +371,13 @@ static void minMaxIdx_(cv::InputArray _src, double* minVal, dminval = minval.i, dmaxval = maxval.i; else if( depth == CV_32F || depth == CV_16F || depth == CV_16BF ) dminval = minval.f, dmaxval = maxval.f; + else if( depth == CV_8F_E4M3FN || depth == CV_8F_E4M3FNUZ ) + { + // the fp8 kernel returns toggled values (see minMaxIdx8f): toggle them back and decode + const float* lut = depth == CV_8F_E4M3FN ? fp8_t::decodeLUT() : fp8a_t::decodeLUT(); + dminval = lut[(uchar)CV_TOGGLE_FP8(minval.i)]; + dmaxval = lut[(uchar)CV_TOGGLE_FP8(maxval.i)]; + } else if( depth == CV_64F ) dminval = minval.d, dmaxval = maxval.d; else if( depth == CV_64S || depth == CV_32U ) diff --git a/modules/core/src/minmax.simd.hpp b/modules/core/src/minmax.simd.hpp index d61a86f749..e39665fabf 100644 --- a/modules/core/src/minmax.simd.hpp +++ b/modules/core/src/minmax.simd.hpp @@ -374,6 +374,23 @@ DEFINE_MINMAXIDX_FUNC_NOSIMD(minMaxIdx64u, uint64, uint64) DEFINE_MINMAXIDX_FUNC_NOSIMD(minMaxIdx64s, int64, int64) DEFINE_MINMAXIDX_FUNC_NOSIMD(minMaxIdx32u, unsigned, int64) +// fp8 values are sign-magnitude, like float: CV_TOGGLE_FP8 maps them to int8 values in the +// same order, so the vectorized s8 kernel can find the extrema. The values it returns are +// toggled; minMaxIdx() toggles them back and decodes them. +static void minMaxIdx8f(const uchar* src, const uchar* mask, int* minVal, int* maxVal, + size_t* minIdx, size_t* maxIdx, int len, size_t startIdx) +{ + const int BLOCK_SIZE = 1024; + schar buf[BLOCK_SIZE]; + for (int i = 0; i < len; i += BLOCK_SIZE) + { + const int n = std::min(len - i, BLOCK_SIZE); + for (int j = 0; j < n; j++) + buf[j] = (schar)CV_TOGGLE_FP8(src[i + j]); + minMaxIdx8s(buf, mask ? mask + i : nullptr, minVal, maxVal, minIdx, maxIdx, n, startIdx + i); + } +} + MinMaxIdxFunc getMinMaxIdxFunc(int depth) { static MinMaxIdxFunc minMaxIdxTab[CV_DEPTH_MAX] = @@ -391,6 +408,8 @@ MinMaxIdxFunc getMinMaxIdxFunc(int depth) minMaxIdxWrap, minMaxIdxWrap, minMaxIdxWrap, + minMaxIdxWrap, + minMaxIdxWrap, 0 }; diff --git a/modules/core/test/test_arithm.cpp b/modules/core/test/test_arithm.cpp index 706054342f..aa03a2b807 100644 --- a/modules/core/test/test_arithm.cpp +++ b/modules/core/test/test_arithm.cpp @@ -4167,7 +4167,7 @@ TEST_P(MinMaxSupportedMatDepth, minMaxIdx) INSTANTIATE_TEST_CASE_P( MinMaxLoc, MinMaxSupportedMatDepth, - testing::Values(perf::MatDepth(CV_16F), CV_16BF, CV_Bool, CV_64U, CV_64S, CV_32U) + testing::Values(perf::MatDepth(CV_16F), CV_16BF, CV_Bool, CV_64U, CV_64S, CV_32U, CV_8F_E4M3FN, CV_8F_E4M3FNUZ) ); typedef testing::TestWithParam MinMaxAccuracyNewTypes; @@ -4189,6 +4189,8 @@ TEST_P(MinMaxAccuracyNewTypes, accuracy) max_val = (double)std::numeric_limits::max() - 1.0; break; case CV_64U: fill_val = 1e15; min_val = 0.0; max_val = 1e18; break; case CV_64S: fill_val = 0.0; min_val = -1e17; max_val = 1e17; break; + case CV_8F_E4M3FN: fill_val = 1.5; min_val = -96.0; max_val = 448.0; break; + case CV_8F_E4M3FNUZ: fill_val = 1.5; min_val = -96.0; max_val = 240.0; break; default: FAIL() << "Unexpected depth " << depth; } @@ -4205,6 +4207,8 @@ TEST_P(MinMaxAccuracyNewTypes, accuracy) case CV_32U: src.at(p.y, p.x) = (uint32_t)v; break; case CV_64U: src.at(p.y, p.x) = (uint64_t)v; break; case CV_64S: src.at(p.y, p.x) = (int64_t)v; break; + case CV_8F_E4M3FN: src.at(p.y, p.x) = cv::fp8_t((float)v); break; + case CV_8F_E4M3FNUZ: src.at(p.y, p.x) = cv::fp8a_t((float)v); break; default: FAIL() << "Unexpected depth " << depth; } }; @@ -4270,6 +4274,10 @@ TEST_P(MinMaxAccuracyNewTypes, accuracy_with_mask) case CV_64S: fill_val = 0.0; masked_min = -1e10; masked_max = 1e10; outlier_min = -1e17; outlier_max = 1e17; break; + case CV_8F_E4M3FN: + case CV_8F_E4M3FNUZ: + fill_val = 1.0; masked_min = -10.0; masked_max = 10.0; + outlier_min = -200.0; outlier_max = 200.0; break; default: FAIL() << "Unexpected depth " << depth; } @@ -4284,6 +4292,8 @@ TEST_P(MinMaxAccuracyNewTypes, accuracy_with_mask) case CV_32U: src.at(p.y, p.x) = (uint32_t)v; break; case CV_64U: src.at(p.y, p.x) = (uint64_t)v; break; case CV_64S: src.at(p.y, p.x) = (int64_t)v; break; + case CV_8F_E4M3FN: src.at(p.y, p.x) = cv::fp8_t((float)v); break; + case CV_8F_E4M3FNUZ: src.at(p.y, p.x) = cv::fp8a_t((float)v); break; default: FAIL() << "Unexpected depth " << depth; } }; @@ -4315,7 +4325,7 @@ TEST_P(MinMaxAccuracyNewTypes, accuracy_with_mask) INSTANTIATE_TEST_CASE_P( MinMaxAcc, MinMaxAccuracyNewTypes, - testing::Values(CV_16BF, CV_Bool, CV_64U, CV_64S, CV_32U) + testing::Values(CV_16BF, CV_Bool, CV_64U, CV_64S, CV_32U, CV_8F_E4M3FN, CV_8F_E4M3FNUZ) ); typedef testing::TestWithParam LUTAccuracyNewDstTypes;