# What's the difference between minmaxloc and minmaxidx ?

The documentation gives no information about their difference.

This forum is disabled, please visit https://forum.opencv.org

What's the difference between minmaxloc and minmaxidx ?

The documentation gives no information about their difference.

add a comment

4

** "The documentation gives no information about their difference." **

Or you have just not seen the explanation in the documentation and it's your fault...

The docu is e.g. here: http://docs.opencv.org/modules/core/d...

For minmaxloc:

```
minLoc – pointer to the returned minimum location (in 2D case);
```

for minmaxIdx:

```
pointer to the returned minimum location (in nD case);
```

You use MinMaxLoc if you have simple 2d-Array (e.g. a CV_8UC1 image) and MinMaxIdx if you have more dimensions.

```
cv::Mat foo1(3,3,CV_8UC1);
foo1.setTo(1);
foo1.at<uchar>(1,1) = 2;
double a,b;
cv::Point p1,p2;
cv::minMaxLoc(foo1,&a,&b,&p1,&p2);
// 1 2 [0, 0] [1, 1]
cout << a << " " << b << " " << p1 << " " << p2 << endl;
int arr[3] = {4,3,2};
Mat foo2(3, arr, CV_8UC1);
foo2.setTo(0);
foo2.at<uchar>(2,1,0) = 2;
int minInd[3];
int maxInd[3];
cv::minMaxIdx(foo2, &a, &b, minInd, maxInd, Mat());
// 0 2 [0 0 0] [2 1 0]
cout << a << " " << b << " [" << minInd[0] << " " << minInd[1] << " " << minInd[2] << "] [" << maxInd[0] << " " << maxInd[1] << " " << maxInd[2] << "]" << endl;
```

1

Additionally there is a subtlety about minMaxIdx. The index locations are given as int*, which must be of sufficient dimension to hold the computed indices. In particular, an input Mat has at least 2 dimensions (even if one of them is 1), and so the min and max index pointers must reserve space for 2 ints. If you do not do this, you will find out about it at run time, when minMaxIdx overwrites something.

As you can probably tell, I found out the hard way, when trying to use minMaxIdx to find the maximum of a histogram. The histogram has 1 logical dimension, but it's computed as a cv::Mat which has 2 dimensions, and so the indices for min and max must be int[2].

Can't upvote, but this is a very important difference. I spent hours debugging this before I looked at the suspiciously at the minMaxIdx call. What we wanted was a function that would return a one-dimensional index, like total().

minMaxLoc() is more explicit and safe so I would recommend always using it unless you have a >2 dimensional matrix.

Asked: **
2015-01-09 18:43:16 -0600
**

Seen: **12,859 times**

Last updated: **Jan 10 '15**

Source image having 2 or more part matched with a template image

Where can i find Min, Max kernel function in Cuda?

getting subpixel with MatchTemplate

minVal of minmaxLoc is not what I read by direct reading at matchLoc point

minMaxLoc crashed using opencl when location is needed

cv::minMaxLoc argument problem

How do I interpret the information in the location output matrix from cuda::findMinMaxLoc?

Copyright OpenCV foundation, 2012-2018. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.