2

我有必要计算两遍模糊的内核。假设有一个 5x5 的模糊核,生成如下:

public static float[][] getKernelf(int size, float sigma) {
    float[][] kernel = new float[size][size];
    int mean = size / 2;
    float sum = 0; // For accumulating the kernel values
    for (int x = 0; x < size; x++)  {
        for (int y = 0; y < size; y++) {
            kernel[x][y] = (float) (Math.exp(-0.5 * (Math.pow((x - mean) / sigma, 2.0) + 
                    Math.pow((y - mean) / sigma, 2.0))) / (2 * Geometry.PI * sigma * sigma));
            // Accumulate the kernel values
            sum += kernel[x][y];
        }
    }

    // Normalize the kernel
    for (int x = 0; x < size; x++) 
        for (int y = 0; y < size; y++)
            kernel[x][y] /= sum;

    return kernel;
}

当 sigma = 1 和 size = 5 时,我们有以下内核:

0.0029690173  0.013306212  0.021938235  0.013306212  0.0029690173
0.013306212   0.059634306  0.09832035   0.059634306  0.013306212
0.021938235   0.09832035   0.16210285   0.09832035   0.021938235
0.013306212   0.059634306  0.09832035   0.059634306  0.013306212
0.0029690173  0.013306212  0.021938235  0.013306212  0.0029690173

问题是如何将此内核带入适合两次渲染的视图(水平和垂直,OpenGL中的实时渲染)

编辑:

书中给出的内核:0.227027 0.1945946 0.1216216 0.054054 0.016216

我完整的fragment_blur_shader.glsl:

#version 330

out vec4 fragColor;
in vec2 texCoords;

uniform sampler2D image;
uniform bool isHorizontal;
uniform float weight[5] = float[] (0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216);

void main() {
    vec2 texOffset = 1.0 / textureSize(image, 0); // gets size of single texel
    vec3 result = texture(image, texCoords).rgb * weight[0]; // current fragment’s contribution
    if(isHorizontal) {
        for(int i = 1; i < 5; i++) {
            result += texture(image, texCoords + vec2(texOffset.x * i, 0.0)).rgb * weight[i];
            result += texture(image, texCoords - vec2(texOffset.x * i, 0.0)).rgb * weight[i];
        }
    } else {
        for(int i = 1; i < 5; ++i) {
            result += texture(image, texCoords + vec2(0.0, texOffset.y * i)).rgb * weight[i];
            result += texture(image, texCoords - vec2(0.0, texOffset.y * i)).rgb * weight[i];
        }
    }
    fragColor = vec4(result, 1.0);
}

我还发现下图展示了从 1D 内核接收 2D 内核以进行两阶段渲染:

使用两通可分离滤波器方法完成的 7x7 卷积核示例

但我不知道如何获得这个一维核心。我希望能得到你的帮助。

编辑:

我明白了如何获取我需要的内核,但我仍然不明白为什么书上会以这种形式给出这个内核

4

2 回答 2

4

要从您拥有的 2D 高斯内核开始:

float[][] kernel = new float[size][size];
int mean = size / 2;
float sum = 0; // For accumulating the kernel values
for (int x = 0; x < size; x++)  {
    for (int y = 0; y < size; y++) {
        kernel[x][y] = (float) (Math.exp(-0.5 * (Math.pow((x - mean) / sigma, 2.0) + 
                Math.pow((y - mean) / sigma, 2.0))) / (2 * Geometry.PI * sigma * sigma));
        // Accumulate the kernel values
        sum += kernel[x][y];
    }
}

// Normalize the kernel
for (int x = 0; x < size; x++) 
    for (int y = 0; y < size; y++)
        kernel[x][y] /= sum;

到一维高斯内核,只需删除循环y(以及对 的所有进一步引用y):

float[] kernel = new float[size];
int mean = size / 2;
float sum = 0; // For accumulating the kernel values
for (int x = 0; x < size; x++)  {
    kernel[x] = (float) Math.exp(-0.5 * Math.pow((x - mean) / sigma, 2.0));
    // Accumulate the kernel values
    sum += kernel[x];
}

// Normalize the kernel
for (int x = 0; x < size; x++) 
    kernel[x] /= sum;

更多提示:

  • 正如您已经注意到的,您的着色器只使用了该内核的一半(用于 的部分x-mean >= 0)。由于它是对称的,另一半是多余的。

  • 无需用 缩放kernel2 * Geometry.PI * sigma * sigma,因为稍后您对内核进行标准化,这种缩放是无关紧要的。

  • 将此内核与其转置相乘产生第一位代码产生的二维内核(如您在问题中包含的图中所示)。

于 2018-06-19T19:07:07.457 回答
1

所以,我得出了最后的答案。感谢我找到的图片和您的建议,我终于编写了获得一维二次内核的最终函数:

public static float[] getTwoPassKernelf(int size, float sigma) {
    float[] kernel = new float[size];
    float[][] fkernel = getKernelf(size, sigma);

    for (int i = 0; i < size; i++) {
        kernel[i] = (float) Math.sqrt(fkernel[i][i]);
    }

    return kernel;
}

public static float[][] getKernelf(int size, float sigma) {
    float[][] kernel = new float[size][size];
    int mean = size / 2;
    float sum = 0; // For accumulating the kernel values
    for (int x = 0; x < size; x++)  {
        for (int y = 0; y < size; y++) {
            kernel[x][y] = (float) (Math.exp(-0.5 * (Math.pow((x - mean) / sigma, 2.0) + 
                    Math.pow((y - mean) / sigma, 2.0))) / (2 * Geometry.PI * sigma * sigma));
            // Accumulate the kernel values
            sum += kernel[x][y];
        }
    }

    // Normalize the kernel
    for (int x = 0; x < size; x++) 
        for (int y = 0; y < size; y++)
            kernel[x][y] /= sum;

    return kernel;
}

总之,着色器需要传输的不是所有内核,而是它的一部分,包括中心(在我的例子中)。因此,我在问题本身中引用的核心不是 5x5 而是 10x10。

例如:getTwoPassKernelf(10, 2f)退货

0.008890575, 0.027384898, 0.065692954, 0.1227306, 0.17857197, 0.20234855, 0.17857197, 0.1227306, 0.065692954, 0.027384898

但就我而言,我必须只接受0.20234855, 0.17857197, 0.1227306, 0.065692954, 0.027384898- 这正是这本书所提供的

一张图解释了两遍渲染获取内核的本质

一张图解释了两遍渲染获取内核的本质

Selected - 内核元素的平方

我希望答案对某人有用

于 2018-06-19T18:18:37.290 回答