Ask Your Question
2

What's the difference between minmaxloc and minmaxidx ?

asked 2015-01-09 18:43:16 -0500

sup gravatar image

The documentation gives no information about their difference.

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
3

answered 2015-01-10 05:45:11 -0500

updated 2015-01-10 06:00:07 -0500

* "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;
edit flag offensive delete link more

Comments

Thank you very much, I missed that.

sup gravatar imagesup ( 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.

FooBar gravatar imageFooBar ( 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!

Hamphrey gravatar imageHamphrey ( 2015-11-20 09:57:18 -0500 )edit
1

answered 2017-08-10 05:37:26 -0500

Dan Talmage gravatar image

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

edit flag offensive delete link more

Comments

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.

ykozlov gravatar imageykozlov ( 2017-10-27 13:45:22 -0500 )edit
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2015-01-09 18:43:16 -0500

Seen: 8,905 times

Last updated: Jan 10 '15