图像均值:
方差:
?
标准差:
?
#include <opencv2/opencv.hpp>
#include "svmlicenceplate.h"
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat srcImage = imread("22-5.jpg");
if (srcImage.empty())return -1;
Mat gray;
cvtColor(srcImage, gray, COLOR_BGR2GRAY);
Mat m,s;
//opencv的函数计算均值和标准差
meanStdDev(gray, m, s);
cout << m << ' ' << s << endl;
//waitKey(0);
//一次遍历求均值和标准差
int height = srcImage.rows;
int width = srcImage.cols;
int num = height * width;
double a = 0, b = 0, me = 0;
for (int r = 0; r < height; r++) {
uchar* ptr = gray.ptr<uchar>(r);
for (int c = 0; c < width; c++) {
me += ptr[c];
a += (double)ptr[c] * ptr[c];
}
}
b = me;
me = me / num;
cout << me << endl;//均值
double sd = (a - 2 * b * me + num * me * me) / num;
cout << sd << endl;//方差
cout << sqrt(sd) << endl;
return 0;
}
|