高斯曲线计算(C ++)

时间:2019-02-20 16:12:27

标签: c++ image-processing computer-vision histogram gaussian

给出直方图,执行双均值高斯拟合。 该项目将两条高斯曲线拟合到具有初始阈值的直方图,并使用这些曲线确定理想阈值。每次迭代时,直方图都分为两部分,直方图的每个部分都称为高斯。

考虑到我的合成曲线毫无意义,我的计算肯定是错误的。有人可以看到这里发生的事情吗?我已经包括了用于计算biMeans高斯曲线,fitGauss(计算高斯曲线),均值,方差和高斯值的函数。

int biMeanGauss (int thrVal){
    double sum1, sum2, total = 0.0;
    int bestThr = thrVal;
    double minDiff = 99999999.0;

    for (int i = thrVal; i < (maxVal-offSet); i++) {
        set1DZero(GaussAry);

        sum1 = fitGauss(0, i);
        cout << "Sum1: " <<sum1 << endl;
        sum2 = fitGauss(i, maxVal);
        cout << "Sum2: " << sum2 << endl;
        total = sum1 + sum2;
        cout << "Total: " << total << endl;

        if (total < minDiff) {
            minDiff = total;
            bestThr = i; 
            cout << "Total < MinDiff, so BestThr: " <<bestThr << endl;
        }
        else;
    }
    return bestThr;
}

double fitGauss(int leftIndex, int rightIndex){
    double mean, var, sum = 0.0;
    double Gval;

    mean = computeMean(leftIndex, rightIndex, maxCount);
    var = computeVar (leftIndex, rightIndex, mean);

    for (int index = leftIndex; index <= rightIndex; index ++){
        Gval = computeGaussian(histAry[index], mean, var);
        sum += abs(Gval - histAry[index]);
        GaussAry[index] = (int)Gval;
        GaussImg[index][(int)Gval]= 1;
    }

    return sum;
};

double computeMean(int leftIndex, int rightIndex, int maxCount) {
    int mean, n, sum = 0;
    for (int i = leftIndex; i <= rightIndex; i++){
        sum += (i*histAry[i]);
        n++;
        if (histAry[i] > maxCount){
            maxCount = histAry[i];
        }
    }
    mean = (sum / (double) n);
    return mean;
}

double computeVar(int leftIndex, int rightIndex, int mean) {
    double var = 0;
    double sum = 0;
    double n = 0;

    for (int i = leftIndex; i <= rightIndex; i++) {
        sum += histAry[i]*(i - mean) * (i - mean);
        n++;
    }

    var = (sum / n);
    return var;
}

double computeGaussian(int index, double mean, double var) {
    double Gval = 0;
    Gval = maxCount * exp(-(((index - mean)*(index - mean)) / (2 * var)));
    return Gval;
}

0 个答案:

没有答案