Method 1: algorithm in std
#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>());
method2: 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);
}
Method 3: Traverse 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;
}
Copyright statement: This article is an original article of CSDN blogger "Jinghong Yibo", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprinting.
Original link: https://blog.csdn.net/shyjhyp11/article/details/109486647