中等和方差计算器 [英] Medium and variance calculator

查看:92
本文介绍了中等和方差计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,编译完源代码后我的方差为零。这个结果是真的还是没有?

提前谢谢



我尝试了什么:



Hello everyone, after compiling the source code I got zero variance. this result true or no?
thank you in advance

What I have tried:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include<iostream>
#include<math.h>
#include<time.h>
using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
system("clear");
Mat src=imread("/home/Adam/Bureau/2.jpg",0);
float mean,s,v,e;
int a=src.rows, b=src.cols;
time_t t;
srand((unsigned) time(&t));
for(int i=0;i<src.rows;i++){
for(int j=0;i<src.cols;j++){ s+=src.at<float>(i,j);
mean=s/(a*b);
e+=(src.at<float>(i,j)-mean)*(src.at<float>(i,j)-mean);
v=e/(a*b);
}
}
cout<<"the mean ="<<mean<<endl;
cout<<"la variance est ="<<v<<endl;
return 0;
}

推荐答案

无论如何你的方差计算是不正确的。

你必须首先计算平均值,然后使用计算的平均值来计算方差。那就是你需要两个循环。
Anyway your variance computation is incorrect.
You have to first compute the mean value and then use the computed mean value in order to compute the variance. That is you need two loops.


首先你应该将每个变量初始化为e到0的意思。访问src.at< float>(i,j)的值应该是存储在var。



你的程序逻辑看起来很奇怪。在每个i和j的交互中,你改变了平均值。我认为你应该遍历所有像素并总结以构建平均值。方差的循环。



阅读有关方差的基础知识,以证明您的计算方法是你想要的。
At first you should initialize every variable as mean to e with 0. The value from the access to src.at<float>(i,j) should be stored in a var.

Your program logic looks strange. On every i and j interation you change the mean. I think you should loop over all pixels and sum up to build the mean. Than loop for the variance.

Read the basics about variance to proof that your calculation is what you want.


你的代码在循环中用来计算方差的平均值不是最后一个,而是到目前为止处理项目的平均值。您还没有初始化和变量 s e 零。



你必须首先计算均值,然后使用另一个迭代来计算方差:

The mean value used by your code within the loops for calculating the variance is not the final one but the one for the processed items so far. You have also not initialised the sum variables s and e with zero.

You have to caluclate the mean first and then use another iteration to calculate the variance:
// Use double to reduce inaccuray and initialise start value
double mean = 0;
for (int i = 0; i < src.rows; i++)
{
    for (int j = 0; j < src.cols; j++)
        mean += src.at<uchar>(i, j);
}
mean /= a * b;

// Calculate variance now using the final mean
double v = 0;
for (int i = 0; i < src.rows; i++)
{
    for (int j = 0; j < src.cols; j++)
        v += (src.at<uchar>(i, j) - mean) * (src.at<uchar>(i, j) - mean);
}
// Variance
v /= a * b;
// Sample variance
//v /= (a * b) - 1;





还有另一个问题:

您使用 src.at< float>(i,j)从图像中获取像素值。但您已将 0 作为第二个参数传递给 imread ,这是 IMREAD_GRAYSCALE 。然后矩阵包含定义强度的每个像素的 uchar 值。访问像素时必须使用该类型。

[/ EDIT]



There is another problem:
You use src.at<float>(i, j) to get the pixel values from the image. But you have passed 0 as second parameter to imread which is IMREAD_GRAYSCALE. Then the matrix contains uchar values for each pixel defining the intensity. That type must be used when accessing pixels.
[/EDIT]


这篇关于中等和方差计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆