opencv学习笔记——minMaxIdx函数的含义及用法

时间:2022-10-03 21:03:06

opencv中有时需要对Mat数据需要对其中的数据求取最大值和最小值。opencv提供了直接的函数

CV_EXPORTS_W void minMaxLoc(InputArray src, CV_OUT double* minVal,
CV_OUT double* maxVal=, CV_OUT Point* minLoc=,
CV_OUT Point* maxLoc=, InputArray mask=noArray());
CV_EXPORTS void minMaxIdx(InputArray src, double* minVal, double* maxVal,
int* minIdx=, int* maxIdx=, InputArray mask=noArray());

实现代码如下所示:

    #include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm> #include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/contrib/contrib.hpp" using namespace std;
using namespace cv; int main(int argc, char* argv[])
{
#pragma region min_max float Tval = 0.0;
float RawData[][] = {{4.0,1.0,3.0},{8.0,7.0,9.0}};
Mat RawDataMat(,,CV_32FC1,RawData); for (int j = ; j < ; j++)
{
for (int i = ; i < ; i++)
{
//Tval = RawData[j][i]; //No problem !!!
Tval = RawDataMat.at<float>(j,i);
cout << "(j,i) = "<<j<<","<<i<<"\t"<<Tval<<endl;
}
} double minv = 0.0, maxv = 0.0;
double* minp = &minv;
double* maxp = &maxv; minMaxIdx(RawDataMat,minp,maxp); cout << "Mat minv = " << minv << endl;
cout << "Mat maxv = " << maxv << endl; #pragma endregion return ;
}