OpenCV求(图像)矩阵中最大值,最小值函数minMaxLoc() vs minMaxIdx()

转载请注明出处。
文章地址:https://blog.csdn.net/duiwangxiaomi/article/details/97937706?spm=1001.2014.3001.5501

  minMaxLoc()和minMaxIdx()函数的功能是一样的,两个函数的区别在于设置的参数不同,而且minMaxLoc()针对单通道图像,minMaxIdx()则不限制(不过输出的坐标会变成三维)。

函数功能

  (1)计算矩阵Mat中最大值、最小值、返回最大最小的索引
  (2)延伸一下,可以计算图像Mat中灰度最大值、最小值、返回最大最小的索引

函数原型

//! 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());
//minMaxLoc()参数说明:

// src:输入矩阵Mat(图像)。
// minVal:最小值,可輸入NULL表示不需要。
// maxVal :最大值,可輸入NULL表示不需要。
// minLoc:最小值的位置,可输入NULL表示不需要,Point类型。
// maxLoc:最大值的位置,可输入NULL表示不需要,Point类型。
// mask:可有可无的掩模。
//! finds global minimum and maximum array elements and returns their values and their locations
CV_EXPORTS void minMaxIdx(InputArray src, double* minVal, double* maxVal,
												int* minIdx=0, int* maxIdx=0, InputArray mask=noArray());
// minMaxIdx()参数说明:

// src:输入矩阵Mat(图像)。
// minVal:最小值,可輸入NULL表示不需要。
// maxVal :最大值,可輸入NULL表示不需要。
// minIdx:最小值所在位置索引(i,j)
// maxIdx:最大值所在位置索引 (i,j)
// 【注】:minIdx和maxIdx的类型没有给出明确的说明和示例,需要将其定义为int型的含有两个数的数组,
// 		e.g. int idx_min[2] = {255,255}, idx_max[2]= {255, 255};,这是因为OpenCV中数组至少是二维的,
//		即使是一行或者一列的数组它们的点的坐标也应该有两个值。 
// mask:可有可无的掩模。

说明: 参数若不需要,则置为NULL或者0,即可.

函数实现:

1.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;
}

2.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;
}

注:文中程序部分复制自参考链接1。

参考链接1
参考链接2

  • 8
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值