OpenCv 010---图像像素值统计

1 前备知识

(1)标准方差

简单来说,标准差是一组数据平均值分散程度的一种度量。一个较大的标准差,代表大部分数值和其平均值之间差异较大;一个较小的标准差,代表这些数值较接近平均值。

2 所用到的主要OpenCv API

/** @brief Finds the global minimum and maximum in an array.

The function cv::minMaxLoc finds the minimum and maximum element values and their positions. The

@param src input single-channel array.
@param minVal pointer to the returned minimum value; NULL is used if not required.
@param maxVal pointer to the returned maximum value; NULL is used if not required.
@param minLoc pointer to the returned minimum location (in 2D case); NULL is used if not required.
@param maxLoc pointer to the returned maximum location (in 2D case); NULL is used if not required.
@param mask optional mask used to select a sub-array.
@sa max, min, compare, inRange, extractImageCOI, mixChannels, split, Mat::reshape
*/

CV_EXPORTS_W void minMaxLoc(InputArray src, CV_OUT double* minVal,
                            CV_OUT double* maxVal = 0, CV_OUT Point* minLoc = 0,
                            CV_OUT Point* maxLoc = 0, InputArray mask = noArray());

/** Calculates a mean and standard deviation of array elements.

@param src input array that should have from 1 to 4 channels so that the results can be stored in
Scalar_ 's.
@param mean output parameter: calculated mean value.
@param stddev output parameter: calculated standard deviation.
@param mask optional operation mask.
@sa countNonZero, mean, norm, minMaxLoc, calcCovarMatrix
*/

CV_EXPORTS_W void meanStdDev(InputArray src, OutputArray mean, OutputArray stddev,
                             InputArray mask=noArray());

3 程序代码

#include"opencv2opencv.hpp"
#include"iostream"

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    Mat srcGray = imread("G:\CVworkstudy\program_wwx\研习社140课时\ZhaiZhigang140\lena.jpg", IMREAD_GRAYSCALE);
    if (srcGray.empty())
    {
        printf("Could not load image...
");
        return -1;
    }
    namedWindow("grayImg");
    imshow("grayImg", srcGray);
    double minVal, maxVal;
    Point minLoc, maxLoc;
    minMaxLoc(srcGray, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
    printf("MinVal:%.2f,MaxVal:%.2f
", minVal, maxVal);
    printf("MinLoc:(%d,%d)", minLoc.x, minLoc.y);
    printf("MaxLoc:(%d,%d)
", maxLoc.x, maxLoc.y);

    Mat srcRgb = imread("G:\CVworkstudy\program_wwx\研习社140课时\ZhaiZhigang140\lena.jpg");
    if (srcRgb.empty())
    {
        printf("Could not load Image...
");
        return -1;
    }
    namedWindow("RgbImg");
    imshow("RgbImg", srcRgb);
    Mat means,stdDevs;
    meanStdDev(srcRgb, means, stdDevs);
    printf("blue channel>>> mean:%.2f,stdDev:%.2f
", means.at<double>(0, 0), stdDevs.at<double>(0, 0));
    printf("green channel>>>mean:%.2f,stdDev:%.2f
", means.at<double>(1, 0), stdDevs.at<double>(1, 0));
    printf("red channel>>>mean:%.2f,stdDev:%.2f
", means.at<double>(2, 0),stdDevs.at<double>(2,0));
    waitKey(0);
    return 0;
}

4 运行结果

5 扩展及注意事项

NULL

One day,I will say "I did it"
原文地址:https://www.cnblogs.com/Vince-Wu/p/11149381.html