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"opencv2\opencv.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...\n");
        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\n", minVal, maxVal);
    printf("MinLoc:(%d,%d)", minLoc.x, minLoc.y);
    printf("MaxLoc:(%d,%d)\n", maxLoc.x, maxLoc.y);

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

4 運行結果

 

 

5 擴展及注意事項

NULL


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM