How to compute maximum pixel value of Mat in OpenCV

c++, opencv

Solution

You could always use the std max_element function with iterators from opencv

std::max_element(Mat.begin<double>(),Mat.end<double>());

Problem

This should be obvious, I thought. But I can't find easy way to find maximum among all pixels in a Mat of OpenCV. Of course, I can do following for each pixel type. But general max function would be still useful. ``` double cvMax(cv::Mat& mat) { float max=0; float* pData=(float*)mat.data; for(int i=0;i<mat.rows;i++) { for(int j=0;j<mat.cols;j++) { float value = pData[j+i*mat.cols]; if(value>max) { max=value; } } } return max; } ```

Original source

Related problems