Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find maximum value of a cv::Mat

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

The Problem : *maxValue is always returning 0.

From this S.O. thread, I understand that 'max_element return iterators, not values. This is why I use *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.

like image 634
Ríomhaire Avatar asked Apr 11 '13 17:04

Ríomhaire


1 Answers

You should use the OpenCV built-in function minMaxLoc instead of std function.

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; 
like image 66
shivakumar Avatar answered Oct 04 '22 09:10

shivakumar