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

方法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

猜你喜欢

转载自blog.csdn.net/qq_44924694/article/details/130524190
今日推荐