What's the difference between minmaxloc and minmaxidx ?

The documentation gives no information about their difference.

edit retag close merge delete

Sort by » oldest newest most voted

* "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;

more

Thank you very much, I missed that.

( 2015-01-10 11:45:31 -0500 )edit

Could then please accept the answer? It's easier that way to find solutions if someone has a similar problem in the future.

( 2015-01-11 06:05:24 -0500 )edit
1

I totally agree this is not clearly stated in the documentation and therefore believe it is not sup's fault. OpenCV documentation lacks good descriptive examples. This is the truth. You should not blame users for this lack!

( 2015-11-20 09:57:18 -0500 )edit

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].

more

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.

( 2017-10-27 13:45:22 -0500 )edit