### 2. 二维高斯函数

A是幅值，x。y。是中心点坐标，σx σy是方差，图示如下，A = 1, xo = 0, yo = 0, σx = σy = 1

### 高斯函数在图像设计中应用

namespace mycv {    const double pi = 3.1415926;    void createGaussianNoise(cv::Mat& src, cv::Mat& dst)    {        dst = src.clone();        //1、灰阶范围[0， G - 1], 取sigma > 0; sigma越小噪声越小        const int G = 256;        double sigma = 20;            for(int i = 0; i < src.rows; ++i)            for (int j = 0; j < src.cols - 1; ++j)            {                //2、产生位于[0, 1]独立随机数gamma、phi                std::random_device rd;                std::mt19937 gen(rd());                double gamma = std::generate_canonical(gen);                double phi = std::generate_canonical(gen);                //3、计算z1、z2                double z1 = sigma * std::cos(2 * pi*phi)*std::sqrt(-2 * std::log(gamma));                double z2 = sigma * std::sin(2 * pi*phi)*std::sqrt(-2 * std::log(gamma));                //4、                double tmpxy = src.at(i, j) + z1;                double tmpxy1 = src.at(i, j + 1) + z2;                //5                if (tmpxy < 0)                    dst.at(i, j) = 0;                else if (tmpxy > G - 1)                    dst.at(i, j) = G - 1;                else                    dst.at(i, j) = static_cast(tmpxy);                                if (tmpxy1 < 0)                    dst.at(i, j + 1) = 0;                else if (tmpxy > G - 1)                    dst.at(i, j + 1) = G - 1;                else                    dst.at(i, j + 1) = static_cast(tmpxy1);            }    }}//mycv
// gaussian filtercv::Mat gaussian_filter(cv::Mat img, double sigma) {    int height = img.rows;    int width = img.cols;    int channel = img.channels();    // prepare output    cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3);    // prepare kernel    int pad = floor(kernel_size / 2);    int _x = 0, _y = 0;    double kernel_sum = 0;    // get gaussian kernel    float kernel[kernel_size][kernel_size];    for (int y = 0; y < kernel_size; y++) {        for (int x = 0; x < kernel_size; x++) {            _y = y - pad;            _x = x - pad;            kernel[y][x] = 1 / (2 * M_PI * sigma * sigma) * exp(-(_x * _x + _y * _y) / (2 * sigma * sigma));            kernel_sum += kernel[y][x];        }    }    for (int y = 0; y < kernel_size; y++) {        for (int x = 0; x < kernel_size; x++) {            kernel[y][x] /= kernel_sum;        }    }    // filtering    double v = 0;    for (int y = 0; y < height; y++) {        for (int x = 0; x < width; x++) {            for (int c = 0; c < channel; c++)            {                v = 0;                for (int dy = -pad; dy < pad + 1; dy++)                {                    for (int dx = -pad; dx < pad + 1; dx++)                    {                        if (((x - pad) > 0) && ((y - pad) > 0)&&((x + pad) < width) &&((y + pad) < height))                         {                            v += (double)img.at(y + dy, x + dx)[c] * kernel[dy + pad][dx + pad];                        }                    }                }                out.at(y, x)[c] = v;            }        }    }    return out;}