方法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 <cmath>
#include <iomanip>
#include <algorithm>
#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[])
{
#pragma region min_max
float Tval = 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 = RawData[j][i]; //No problem !!!
Tval = RawDataMat.at<float>(j,i);
cout << "(j,i) = "<<j<<","<<i<<"\t"<<Tval<<endl;
}
}
double minv = 0.0, maxv = 0.0;
double* minp = &minv;
double* maxp = &maxv;
minMaxIdx(RawDataMat,minp,maxp);
cout << "Mat minv = " << minv << endl;
cout << "Mat maxv = " << maxv << endl;
#pragma endregion
return 0;
}