Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions modules/core/src/minmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1510,18 +1510,29 @@ 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*cn, src.rows, src.depth(),
minVal, maxVal, min_offset, max_offset, mask.data);
CALL_HAL(minMaxIdx, cv_hal_minMaxIdx, src.data, src.step, src.cols*cn, src.rows,
src.depth(), minVal, maxVal, minIdx, maxIdx, 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);
int res = cv_hal_minMaxIdx(src.data, 0, (int)src.total()*cn, 1, src.depth(),
minVal, maxVal, minIdx, maxIdx, mask.data);

if (res == CV_HAL_ERROR_OK)
{
if (minIdx)
ofs2idx(src, minIdx[0], minIdx);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asmorkalov I've done some more testing and I believe it should be minIdx[1] and maxIdx[1]. I thought that minIdx[0] would correspond to the X direction, but it seems that in OpenCV it corresponds to the row. According do the docs:

single-row matrix is 1xN matrix (and therefore minIdx/maxIdx will be (0,j1)/(0,j2))

If I change these indices from 0 to 1 then my tests pass.

Copy link
Contributor

@savuor savuor May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mplatings It's not clear from the docs if minIdx and maxIdx should contain pointers to an offset to convert it to array indices or to indices themselves.
In the first case (which is how OpenCV tab functions from getMinmaxTab() actually work) there should be 0th index and no such thing as minIdx[i] or maxIdx[i] is ever possible for every i > 0.
In the second case you're right since HAL call gets 1-dimensional array and puts Y index to minIdx[0] and X to minIdx[1] (the same for maxIdx). Since it's just one dimensional, Y index should always be zero which looks impractical to me but looks like this is how it is implemented now.
I think we should choose between these two options first.

By the way, can you tell me what tests are failing? Looks like OpenCV tests do not cover that case.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KleidiCV's tests fail with the head of the OpenCV 4.x branch. Specifically this: https://gitlab.arm.com/kleidi/kleidicv/-/blob/main/conformity/opencv/test_min_max.cpp?ref_type=heads#L59

In KleidiCV 0.1.0 we did not enable minmax in the HAL so from our point of view it's fine for us to change how the HAL should work. If the HAL code should return an offset instead of 2-dimensional indices then that makes KleidiCV's code simpler.

So I'm happy to either change these indices from 0 to 1, or to require that the HAL returns an offset and we will change KleidiCV to match.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let's do like this so that HAL should return offset.
I'll prepare PR to OpenCV which documents such behaviour.

if (maxIdx)
ofs2idx(src, maxIdx[0], maxIdx);
return;
}
else if (res != CV_HAL_ERROR_NOT_IMPLEMENTED)
{
CV_Error_(cv::Error::StsInternal,
("HAL implementation minMaxIdx ==> " CVAUX_STR(cv_hal_minMaxIdx) " returned %d (0x%08x)", res, res));
}
}

CV_OVX_RUN(!ovx::skipSmallImages<VX_KERNEL_MINMAXLOC>(src.cols, src.rows),
Expand Down
27 changes: 26 additions & 1 deletion modules/core/test/test_arithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,32 @@ TEST(Core_minMaxIdx, regression_9207_2)
EXPECT_EQ(14, maxIdx[1]);
}

TEST(Core_MinMaxIdx, MatND)
{
const int shape[3] = {5,5,3};
cv::Mat src = cv::Mat(3, shape, CV_8UC1);
src.setTo(1);
src.data[1] = 0;
src.data[5*5*3-2] = 2;

int minIdx[3];
int maxIdx[3];
double minVal, maxVal;

cv::minMaxIdx(src, &minVal, &maxVal, minIdx, maxIdx);

EXPECT_EQ(0, minVal);
EXPECT_EQ(2, maxVal);

EXPECT_EQ(0, minIdx[0]);
EXPECT_EQ(0, minIdx[1]);
EXPECT_EQ(1, minIdx[2]);

EXPECT_EQ(4, maxIdx[0]);
EXPECT_EQ(4, maxIdx[1]);
EXPECT_EQ(1, maxIdx[2]);
}

TEST(Core_Set, regression_11044)
{
Mat testFloat(Size(3, 3), CV_32FC1);
Expand Down Expand Up @@ -2807,7 +2833,6 @@ TEST(Core_MinMaxIdx, rows_overflow)
}
}


TEST(Core_Magnitude, regression_19506)
{
for (int N = 1; N <= 64; ++N)
Expand Down