How to get the max value from n dimensional array in OpenCV

此生再无相见时 提交于 2019-12-21 02:36:21

问题


I am trying to get max value from a 3-d Mat, but minmaxIdx and mixmaxloc both failed to do this.

int sz[] = {BIN, BIN, BIN};
Mat accumarray(3, sz, CV_8U, Scalar::all(0)) ;
double testMaxval = 0;
int minIdx = accumarray.dims ;
minMaxIdx(accumarray, NULL, &testMaxval,NULL,minIdx ,NULL) ;
cout<<testMaxval<<endl ;

This code wouldn't work, so Can I use max(), minmaxidx(), or minmaxloc() to get the max value efficiently without manually process the entire n-dimensional array?


回答1:


Following code works for me with OpenCV 2.3.1:

int sz[] = {3, 3, 3};
Mat accumarray(3, sz, CV_8U, Scalar::all(0));
accumarray.at<uchar>(0, 1, 2) = 20;
double testMaxval;
int maxIdx[3];
minMaxIdx(accumarray, 0, &testMaxval, 0, maxIdx);
cout << testMaxval << endl ;
cout << maxIdx[0] << ", " << maxIdx[1] << ", " << maxIdx[2] << endl;



回答2:


Use Mat() instead of NULL for Mask or you will vioulate an assertion Mask.empty()

Mat m;
double min, max;
int minInd, maxInd;
cv::minMaxIdx(m, &min, &max, &minInd, &maxInd, Mat());


来源:https://stackoverflow.com/questions/7253589/how-to-get-the-max-value-from-n-dimensional-array-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!