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
*/
1CV_EXPORTS_W void minMaxLoc(InputArray src, CV_OUT double* minVal, 2 CV_OUT double* maxVal = 0, CV_OUT Point* minLoc = 0, 3 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
*/
1CV_EXPORTS_W void meanStdDev(InputArray src, OutputArray mean, OutputArray stddev, 2 InputArray mask=noArray());
3 程序代码
1#include"opencv2\opencv.hpp" 2#include"iostream" 3 4using namespace std; 5using namespace cv; 6 7int main(int argc, char** argv) 8{ 9 Mat srcGray = imread("G:\\CVworkstudy\\program_wwx\\研习社140课时\\ZhaiZhigang140\\lena.jpg", IMREAD_GRAYSCALE); 10 if (srcGray.empty()) 11 { 12 printf("Could not load image...\n"); 13 return -1; 14 } 15 namedWindow("grayImg"); 16 imshow("grayImg", srcGray); 17 double minVal, maxVal; 18 Point minLoc, maxLoc; 19 minMaxLoc(srcGray, &minVal, &maxVal, &minLoc, &maxLoc, Mat()); 20 printf("MinVal:%.2f,MaxVal:%.2f\n", minVal, maxVal); 21 printf("MinLoc:(%d,%d)", minLoc.x, minLoc.y); 22 printf("MaxLoc:(%d,%d)\n", maxLoc.x, maxLoc.y); 23 24 Mat srcRgb = imread("G:\\CVworkstudy\\program_wwx\\研习社140课时\\ZhaiZhigang140\\lena.jpg"); 25 if (srcRgb.empty()) 26 { 27 printf("Could not load Image...\n"); 28 return -1; 29 } 30 namedWindow("RgbImg"); 31 imshow("RgbImg", srcRgb); 32 Mat means,stdDevs; 33 meanStdDev(srcRgb, means, stdDevs); 34 printf("blue channel>>> mean:%.2f,stdDev:%.2f\n", means.at<double>(0, 0), stdDevs.at<double>(0, 0)); 35 printf("green channel>>>mean:%.2f,stdDev:%.2f\n", means.at<double>(1, 0), stdDevs.at<double>(1, 0)); 36 printf("red channel>>>mean:%.2f,stdDev:%.2f\n", means.at<double>(2, 0),stdDevs.at<double>(2,0)); 37 waitKey(0); 38 return 0; 39}
4 运行结果


5 扩展及注意事项
NULL