OpenCV:minMaxLoc vs minMaxIdx

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

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值