From faa259ab344f247c88bd9c4259f77aff3a27aa49 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov <2536374+asmorkalov@users.noreply.github.com> Date: Tue, 7 May 2024 20:46:17 +0300 Subject: [PATCH] Merge pull request #25553 from asmorkalov:as/HAL_min_max_idx Fix HAL interface for hal_ni_minMaxIdx #25553 Fixes https://github.com/opencv/opencv/issues/25540 The original implementation call HAL with the same parameters independently from amount of channels. The patch uses HAL correctly for the case cn > 1. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/core/include/opencv2/core.hpp | 7 ++----- modules/core/src/minmax.cpp | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/modules/core/include/opencv2/core.hpp b/modules/core/include/opencv2/core.hpp index be800333f8..f6372ac03e 100644 --- a/modules/core/include/opencv2/core.hpp +++ b/modules/core/include/opencv2/core.hpp @@ -912,11 +912,8 @@ CV_EXPORTS_W void reduceArgMax(InputArray src, OutputArray dst, int axis, bool l The function cv::minMaxIdx finds the minimum and maximum element values and their positions. The extremums are searched across the whole array or, if mask is not an empty array, in the specified -array region. The function does not work with multi-channel arrays. If you need to find minimum or -maximum elements across all the channels, use Mat::reshape first to reinterpret the array as -single-channel. Or you may extract the particular channel using either extractImageCOI, or -mixChannels, or split. In case of a sparse matrix, the minimum is found among non-zero elements -only. +array region. In case of a sparse matrix, the minimum is found among non-zero elements +only. Multi-channel input is supported without mask and extremums indexes (should be nullptr). @note When minIdx is not NULL, it must have at least 2 elements (as well as maxIdx), even if src is a single-row or single-column matrix. In OpenCV (following MATLAB) each array has at least 2 dimensions, i.e. single-column matrix is Mx1 matrix (and therefore minIdx/maxIdx will be diff --git a/modules/core/src/minmax.cpp b/modules/core/src/minmax.cpp index ff3786886e..b5aa4c8462 100644 --- a/modules/core/src/minmax.cpp +++ b/modules/core/src/minmax.cpp @@ -1510,9 +1510,19 @@ void cv::minMaxIdx(InputArray _src, double* minVal, Mat src = _src.getMat(), mask = _mask.getMat(); + int _minIdx, _maxIdx; + int* min_offset = (cn == 1) ? minIdx : &_minIdx; + int* max_offset = (cn == 1) ? maxIdx : &_maxIdx; if (src.dims <= 2) - CALL_HAL(minMaxIdx, cv_hal_minMaxIdx, src.data, src.step, src.cols, src.rows, src.depth(), minVal, maxVal, - minIdx, maxIdx, mask.data); + { + CALL_HAL(minMaxIdx, cv_hal_minMaxIdx, src.data, src.step, src.cols*cn, src.rows, src.depth(), + minVal, maxVal, min_offset, max_offset, mask.data); + } + else if (src.isContinuous()) + { + CALL_HAL(minMaxIdx, cv_hal_minMaxIdx, src.data, 0, (int)src.total()*cn, 1, src.depth(), + minVal, maxVal, min_offset, max_offset, mask.data); + } CV_OVX_RUN(!ovx::skipSmallImages(src.cols, src.rows), openvx_minMaxIdx(src, minVal, maxVal, minIdx, maxIdx, mask))