minMaxLoc和minMaxIdx函数的功能是一样的,计算src里的最大值、最小值、返回最大最小的索引
1. 函数格式
//! finds global minimum and maximum array elements and returns their values and their locations
CV_EXPORTS_W void minMaxLoc(InputArray src, CV_OUT double* minVal,
CV_OUT double* maxVal=0, CV_OUT Point* minLoc=0,
CV_OUT Point* maxLoc=0, InputArray mask=noArray());
CV_EXPORTS void minMaxIdx(InputArray src, double* minVal, double* maxVal,
int* minIdx=0, int* maxIdx=0, InputArray mask=noArray());
2. 两个函数的区别在于设置的参数不同
2.1 minMaxIdx
【注】:minIdx和maxIdx的类型没有给出明确的说明和示例,需要将其定义为int型的含有两个数的数组,e.g. int idx_min[2] = {255,255}, idx_max[2] = {255, 255};,这是因为OpenCV中数组至少是二维的,即使是一行或者一列的数组它们的点的坐标也应该有两个值。
minMaxLoc针对单通道图像,minMaxIdx则不限制(不过输出的坐标会变成三维)。
int main()
{
#pragma region min_max
float data[2][3] = { { 4.0,1.0,3.0 },{ 8.0,7.0,9.0 } };
Mat src(2, 3, CV_32FC1, data);
float val = 0.0;
for (int j = 0; j < 2; j++)//row
{
for (int i = 0; i < 3; i++)//col
{
val = src.ptr<float>(j)[i];
cout << "(i,j) = " << i << "," << j << "\t" << val << endl;
}
}
double minv, maxv;
int idx_max[2], idx_min[2];
minMaxIdx(src, &minv, &maxv, idx_min,idx_max);
cout << "minv = " << minv << endl;
cout << "idx_min = " << idx_min[0] << "," << "\t" << idx_min[1] << endl;
cout << "maxv = " << maxv << endl;
cout << "idx_max = " << idx_max[0] << "," << "\t" << idx_max[1] << endl;
#pragma endregion
return 0;
}
2.2 minMaxLoc单通道图像
int main()
{
#pragma region min_max
float data[2][3] = { { 4.0,1.0,3.0 },{ 8.0,7.0,9.0 } };
Mat src(2, 3, CV_32FC1, data);
float val = 0.0;
for (int j = 0; j < 2; j++)//row
{
for (int i = 0; i < 3; i++)//col
{
val = src.ptr<float>(j)[i];
cout << "(i,j) = " << i << "," << j << "\t" << val << endl;
}
}
//double minv, maxv;
//int idx_max[2], idx_min[2];
//minMaxIdx(src, &minv, &maxv, idx_min,idx_max);
//cout << "minv = " << minv << endl;
//cout << "idx_min = " << idx_min[0] << "," << "\t" << idx_min[1] << endl;
//cout << "maxv = " << maxv << endl;
//cout << "idx_max = " << idx_max[0] << "," << "\t" << idx_max[1] << endl;
double minv, maxv;
Point pt_min, pt_max;
minMaxLoc(src, &minv, &maxv, &pt_min, &pt_max);
cout << "minv = " << minv << endl;
cout << "idx_min = " << pt_min << endl;
cout << "maxv = " << maxv << endl;
cout << "idx_max = " << pt_max << endl;
#pragma endregion
return 0;
}
参考文章:
1. https://www.cnblogs.com/feifanrensheng/p/9133408.html
2. https://blog.csdn.net/tianzhaixing2013/article/details/21390505