结合opencv求取图像的局部均值、中值、方差;
//src 为待处理图像
//indexrows 为图像遍历的行数
//indexcols 为图像遍历的列数
//meanv 保存均值
//ker 窗口大小
//均值求取
void Meanvalue(Mat& src, int indexrows, int indexcols, float* meanv, int ker)
{
int lo = (ker - 1) / 2;
float total = 0;
for (int i = indexrows - lo; i <= indexrows + lo; i++)
{
for (int j = indexcols - lo; j <= indexcols + lo; j++)
{
total += src.at<uchar>(i, j);
}
}
*meanv = total / (ker * ker);
return;
}
//src 为待处理图像
//indexrows 为图像遍历的行数
//indexcols 为图像遍历的列数
//meanv 保存中值
//ker 窗口大小
//中值求取
void Media(Mat& src, int indexrows, int indexcols, int* meanv, int ker)
{
int lo = (ker - 1) / 2;
vector<int>moreo;
for (int i = indexrows - lo; i <= indexrows + lo; i++)
{
for (int j = indexcols - lo; j <= indexcols + lo; j++)
{
moreo.push_back(src.at<uchar>(i, j));
}
}
sort(moreo.begin(), moreo.end());
*meanv = moreo.at(ker * ker / 2);
return;
}
src 为待处理图像
//indexrows 为图像遍历的行数
//indexcols 为图像遍历的列数
//vall 保存方差值
//ker 窗口大小
//mean 为均值
//局部方差求取
void Vvalue(Mat& src, int indexrows, int indexcols, int* vall, int ker, float mean)
{
int lo = (ker - 1) / 2;
float total = 0;
for (int i = indexrows - lo; i <= indexrows + lo; i++)
{
for (int j = indexcols - lo; j <= indexcols + lo; j++)
{
total += pow((src.at<uchar>(i, j) - mean), 2);
}
}
*vall = static_cast<int>(total);
return;
}