C++ 查找 cv::Mat 的最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15955305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:55:30  来源:igfitidea点击:

Find maximum value of a cv::Mat

c++pointersopencviteratorcomputer-vision

提问by Ríomhaire

I am trying to find the maximum pixel value of a cv::Mat.

我试图找到 a 的最大像素值cv::Mat

The Problem : *maxValueis always returning 0.

问题:*maxValue总是返回0

From this S.O. thread, I understand that 'max_elementreturn iterators, not values. This is why I use *maxValue'

这个 SO thread,我明白'max_element返回迭代器,而不是值。这就是为什么我使用*maxValue'

cv::Mat imageMatrix;

double  sigmaX = 0.0;
int ddepth = CV_16S; //  ddepth – The desired depth of the destination image


cv::GaussianBlur( [self cvMatFromUIImage:imageToProcess], imageMatrix, cv::Size(3,3), sigmaX);

cv::Laplacian(imageMatrix, imageMatrix, ddepth, 1);

std::max_element(imageMatrix.begin(),imageMatrix.end());

std::cout << "The maximum value is : " << *maxValue << std::endl;

Note : If min_elementis substituted in place of max_element, and minValuein place of maxValue, *minValuewill always return 0.

注意:如果min_element被替换为代替max_element,并且minValue代替maxValue*minValue将始终返回0

回答by shivakumar

You should use the OpenCV built-in function minMaxLocinstead of stdfunction.

您应该使用 OpenCV 内置函数minMaxLoc而不是std函数。

Mat m;
//Initialize m
double minVal; 
double maxVal; 
Point minLoc; 
Point maxLoc;

minMaxLoc( m, &minVal, &maxVal, &minLoc, &maxLoc );

cout << "min val: " << minVal << endl;
cout << "max val: " << maxVal << endl;