OpenCV寻找cv::Mat中的最大值和最小值

本文介绍了三种在OpenCV中寻找图像像素最大值和最小值的方法:使用STL的`std::min_element`和`std::max_element`,OpenCV的`cv::minMaxLoc`函数,以及手动遍历`cv::Mat`矩阵。每种方法都展示了相应的代码示例。
摘要由CSDN通过智能技术生成

方法1: std 中 algorithm

#include <iostream>     // std::cout
#include <algorithm>    // std::min_element, std::max_element
 
cv::Mat img = cv::imread("path-to-image/juice.tiff");
 
// 假设图片数据类型位float
float maxValue = *max_element(img.begin<float>(), img.end<float>());
float minValue = *min_element(img.begin<float>(), img.end<float>());

方法2: cv中minMaxLoc

#include <iostream>
#include <opencv2/opencv.hpp>int main()
{
    // std::cout << "Hello World!\n";
    cv::Mat image = cv::imread("path-to-image/juice.png");
    cv::Mat image_re = image.reshape(1);
    double minValue, maxValue;    // 最大值,最小值
    cv::Point  minIdx, maxIdx;    // 最小值坐标,最大值坐标     
    cv::minMaxLoc(image_re, &minValue, &maxValue, &minIdx, &maxIdx);
    std::cout << "最大值:" << maxValue <<"最小值:"<<minValue<<std::endl;
    std::cout << "最大值位置:" << maxIdx << "最小值位置:" << minIdx;   
    cv::waitKey(0);
}

方法3: 遍历Mat

#include <iostream>
#include <cstdlib>
 
#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[])
{
	float Tval = 0.0;
	float maxV = 0.0;
	float RawData[2][3] = {{4.0,1.0,3.0},{8.0,7.0,9.0}};
	Mat RawDataMat(2,3,CV_32FC1,RawData);
 
	for (int j = 0; j < 2; j++)
	{
		for (int i = 0; i < 3; i++)
		{	
			
			Tval = RawDataMat.at<float>(j,i);
			if(maxV < Tval)
				mxaV = Tval;
		}
	}
	return 0;
}

版权声明:本文为CSDN博主「惊鸿一博」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/shyjhyp11/article/details/109486647

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值