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
Find maximum value of a cv::Mat
提问by Ríomhaire
I am trying to find the maximum pixel value of a cv::Mat
.
我试图找到 a 的最大像素值cv::Mat
。
The Problem : *maxValue
is always returning 0
.
问题:*maxValue
总是返回0
。
From this S.O. thread, I understand that 'max_element
return 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_element
is substituted in place of max_element
, and minValue
in place of maxValue
, *minValue
will always return 0
.
注意:如果min_element
被替换为代替max_element
,并且minValue
代替maxValue
,*minValue
将始终返回0
。
回答by shivakumar
You should use the OpenCV built-in function minMaxLoc
instead of std
function.
您应该使用 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;