Implementing Gaussian Blur - How to calculate convolution matrix (kernel)

后端 未结 7 2039
予麋鹿
予麋鹿 2020-12-22 21:14

My question is very close to this question: How do I gaussian blur an image without using any in-built gaussian functions?

The answer to this question is very good,

7条回答
  •  感情败类
    2020-12-22 21:39

    It's as simple as it sounds:

    double sigma = 1;
    int W = 5;
    double kernel[W][W];
    double mean = W/2;
    double sum = 0.0; // For accumulating the kernel values
    for (int x = 0; x < W; ++x) 
        for (int y = 0; y < W; ++y) {
            kernel[x][y] = exp( -0.5 * (pow((x-mean)/sigma, 2.0) + pow((y-mean)/sigma,2.0)) )
                             / (2 * M_PI * sigma * sigma);
    
            // Accumulate the kernel values
            sum += kernel[x][y];
        }
    
    // Normalize the kernel
    for (int x = 0; x < W; ++x) 
        for (int y = 0; y < W; ++y)
            kernel[x][y] /= sum;
    

提交回复
热议问题