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;
}
For most images, pixel values are integers that range from 0 (black) to 255 (white).
Vec3b is the abbreviation for "vector with 3 byte entries" Here those byte entries are unsigned char values to represent values between 0 .. 255. Each byte typically represents the intensity of a single color channel, so on default, Vec3b is a single RGB (or better BGR) pixel.
CV_8UC3 - 3 channel array with 8 bit unsigned integers. CV_8UC4 - 4 channel array with 8 bit unsigned integers. CV_8UC(n) - n channel array with 8 bit unsigned integers (n can be from 1 to 512) )
The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc.
cv::minMaxIdx
is really simple to use. It looks complicated in the docs, but you can omit most of the parameters:
Mat m = ...;
double min, max;
minMaxIdx(m, &min, &max);
printf("min: %f, max: %f\n", min, max);
Moreover, cv::minMaxIdx
is more than 10 times faster than std::max_element
. This is because cv::minMaxIdx
is optimized for handling cv::Mat
data and it uses multiple threads if possible.
If you also need the locations of the minimum and maximum in the image, you can use cv::minMaxLoc
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With