高斯滤波

 http://blog.csdn.net/zxpddfg/article/details/45912561(二维高斯滤波可分离计算实现)

 http://blog.csdn.net/sulanqing/article/details/42744291(OpenCv高斯滤波源码分析)

 

计算高速滤波核

void CreatGauss1D(double sigma, double *pdKernel, int &pnWidowSize)
{
    LONG i;

    int nCenter;//数组中心点
    double dDis;//数组中一点到中心点距离

    //中间变量
    double dValue;
    double dSum;
    dSum = 0;

    pnWidowSize = 1 + 2 * ceil(3 * sigma);// [-3*sigma,3*sigma] 以内数据,会覆盖绝大部分滤波系数
    //double temp0 = -0.5 / (sigma*sigma);
    //double temp1 = 1.0 / (sqrt(2 * 3.1415926)*sigma);
    nCenter = pnWidowSize / 2;
    //生成高斯数据
    for (i = 0; i < pnWidowSize; i++)
    {
        dDis = (double)(i - nCenter);
        //dValue = exp(dDis*dDis * temp0) * temp1 ;
        dValue = exp(-(1.0 / 2)*dDis*dDis / (sigma*sigma)) / (sqrt(2 * 3.1415926)*sigma);
        pdKernel[i] = dValue;
        dSum += dValue;
    }
    //归一化
    for (i = 0; i < pnWidowSize; i++)
    {
        pdKernel[i] /= dSum;
        pdKernel[i] *= 255;
    }
}

void CreatGauss2D(double sigma, double **pdKernel, int pnWidowSize)
{
    LONG i;

    int nCenter;//数组中心点
    double dDisx, dDisy;//数组中一点到中心点距离

    //中间变量
    double dValue;
    double dSum;
    dSum = 0;

    nCenter = pnWidowSize / 2;
    //生成2D高斯数据
    for (i = 0; i < pnWidowSize; i++)
    {
        dDisy = (double)(i - nCenter);
        for (int j = 0; j < pnWidowSize; ++j)
        {
            dDisx = (double)(j - nCenter);
            dValue = exp(-0.5*(dDisx*dDisx + dDisy*dDisy) / (sigma*sigma)) / (2 * 3.1415926*sigma*sigma);
            pdKernel[i][j] = dValue;
            dSum += dValue;
        }
    }

    //归一化
    for (i = 0; i < pnWidowSize; i++)
    {
        for (int j = 0; j < pnWidowSize; ++j)
        {
            pdKernel[i][j] /= dSum;
            pdKernel[i][j] *= 255;
        }
    }
}

 

posted on 2016-08-18 15:08  Maddock  阅读(427)  评论(0编辑  收藏  举报

导航