Find maximum value of a cv::Mat

c++, computer-vision, iterator, opencv, pointers

Solution

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;

Problem

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`.

Original source

Related problems