SoFunction
Updated on 2025-03-10

Opencv implements pixel statistics example code

In OpenCV, it is common to count the pixel information of an image (such as pixel value distribution, maximum value, minimum value, mean value, etc.). The following are some commonly used methods and functions to count pixel information of images:

1. Basic information on statistics of pixel values

  • Maximum, minimum, mean, standard deviation: Usecv::minMaxLoc()andcv::meanStdDev()Functions can quickly calculate the maximum, minimum, mean and standard deviation of an image.
#include <opencv2/>
#include <iostream>

int main() {
    cv::Mat image = cv::imread("", cv::IMREAD_GRAYSCALE); // Read grayscale images    if (()) {
        std::cerr << "Error: Could not load image!" << std::endl;
        return -1;
    }

    double minVal, maxVal;
    cv::Point minLoc, maxLoc;
    cv::minMaxLoc(image, &minVal, &maxVal, &minLoc, &maxLoc);

    cv::Scalar mean, stddev;
    cv::meanStdDev(image, mean, stddev);

    std::cout << "Min value: " << minVal << " at " << minLoc << std::endl;
    std::cout << "Max value: " << maxVal << " at " << maxLoc << std::endl;
    std::cout << "Mean: " << mean[0] << std::endl;
    std::cout << "Stddev: " << stddev[0] << std::endl;

    return 0;
}

2. Histogram of statistics pixel values

  • Histogram calculation: Usecv::calcHist()Functions can calculate the histogram of an image and are used to analyze the distribution of pixel values.
#include <opencv2/>
#include <iostream>

int main() {
    cv::Mat image = cv::imread("", cv::IMREAD_GRAYSCALE); // Read grayscale images    if (()) {
        std::cerr << "Error: Could not load image!" << std::endl;
        return -1;
    }

    // Define histogram parameters    int histSize = 256; // The number of bins of histograms    float range[] = {0, 256}; // Pixel value range    const float* histRange = {range};
    bool uniform = true, accumulate = false;

    cv::Mat hist;
    cv::calcHist(&image, 1, 0, cv::Mat(), hist, 1, &histSize, &histRange, uniform, accumulate);

    // Print histogram    for (int i = 0; i < histSize; i++) {
        std::cout << "Bin " << i << ": " << <float>(i) << std::endl;
    }

    return 0;
}

3. Statistics the sum of pixel values

  • Sum of pixel values: Usecv::sum()The function can calculate the sum of all pixel values ​​in the image.
cv::Scalar sum = cv::sum(image);
std::cout << "Sum of pixel values: " << sum[0] << std::endl;

4. Statistics the number of non-zero pixels

  • Non-zero pixel statistics: Usecv::countNonZero()Functions can count the number of non-zero pixels in an image.
int nonZeroCount = cv::countNonZero(image);
std::cout << "Non-zero pixel count: " << nonZeroCount << std::endl;

5. Statistical distribution of pixel values ​​(divided channels)

  • For multi-channel images (such as RGB images), pixel information for each channel can be counted separately.
cv::Mat image = cv::imread("", cv::IMREAD_COLOR); // Read color imagesstd::vector&lt;cv::Mat&gt; channels;
cv::split(image, channels); // Separation channel
for (int i = 0; i &lt; (); i++) {
    double minVal, maxVal;
    cv::minMaxLoc(channels[i], &amp;minVal, &amp;maxVal);
    std::cout &lt;&lt; "Channel " &lt;&lt; i &lt;&lt; " - Min: " &lt;&lt; minVal &lt;&lt; ", Max: " &lt;&lt; maxVal &lt;&lt; std::endl;
}

6. Statistics the percentage of pixel values

If you need to count the percentage of pixel values ​​(such as 95% of pixel values ​​are less than a certain threshold), you can do this by calculating the cumulative distribution function (CDF) through histogram.

cv::Mat hist;
cv::calcHist(&amp;image, 1, 0, cv::Mat(), hist, 1, &amp;histSize, &amp;histRange, uniform, accumulate);

// Calculate the cumulative distribution functionfor (int i = 1; i &lt; histSize; i++) {
    &lt;float&gt;(i) += &lt;float&gt;(i - 1);
}

// Normalizationhist /= ();

// Find 95% of pixel value thresholdfloat threshold = 0.95;
int pixelValueThreshold = 0;
for (int i = 0; i &lt; histSize; i++) {
    if (&lt;float&gt;(i) &gt;= threshold) {
        pixelValueThreshold = i;
        break;
    }
}
std::cout &lt;&lt; "95% of pixel values are below: " &lt;&lt; pixelValueThreshold &lt;&lt; std::endl;

Through the above methods, the pixel information of OpenCV images can be effectively counted and analyzed. These statistics are very important for tasks such as image processing, analysis and feature extraction. Depending on the specific needs, you can choose the appropriate method for pixel statistics.

This is the end of this article about the example code for opencv to implement pixel statistics. For more related content on opencv pixel statistics, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!