溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于Qt的OpenGL可編程管線學(xué)習(xí)(11)- 高斯模糊

發(fā)布時(shí)間:2020-07-14 09:41:10 來源:網(wǎng)絡(luò) 閱讀:644 作者:Douzhq 欄目:編程語言

下圖是使用高斯模糊和未使用高斯模糊的效果圖對比

正常圖片

基于Qt的OpenGL可編程管線學(xué)習(xí)(11)- 高斯模糊


高斯模糊后

基于Qt的OpenGL可編程管線學(xué)習(xí)(11)- 高斯模糊


1、標(biāo)準(zhǔn)高斯模糊

原理:

每個(gè)像素周圍對應(yīng)的像素乘以對應(yīng)的算子,然后除以算子的綜合

算子為

1 2 1
2 4 2
1 2 1


fragment shader

varying vec2 M_coord;
varying vec3 M_normal;
varying vec3 M_WordPos;

uniform sampler2D U_MainTexture;
uniform sampler2D U_SubTexture;

void main()
{
    // 1 2 1
    // 2 4 2
    // 1 2 1
    int coreSize=3;
    int halfCoreSize=coreSize/2;
    float texelOffset=1/100.0;
    vec4 color = vec4(1.0);
    float nGaussionCore[9] = float[](1.0, 2.0, 1.0, 2.0, 4.0, 2.0, 6.0, 2.0, 1.0);

    int index = 0;
    for(int y=0;y<coreSize;y++)
    {
        for(int x=0;x<coreSize;x++)
        {
             vec4 currentColor=texture2D(U_MainTexture,
                                         M_coord+vec2((-halfCoreSize+x)*texelOffset,
                                                      (-halfCoreSize+y)*texelOffset));
             color += currentColor * nGaussionCore[index++];
         }
     }
     color /= 16.0;
     gl_FragColor=color ;
}


2、橫向模糊

與高斯模糊類似 不過只是模糊橫向分量

// 水平高斯模糊
varying vec2 M_coord;
varying vec3 M_normal;
varying vec3 M_WordPos;

uniform sampler2D U_MainTexture;
uniform sampler2D U_SubTexture;

void main()
{
    int coreSize=3;
    int halfCoreSize=coreSize/2;
    float texelOffset=1/100.0;
    vec4 color = vec4(0.0);
    float nGaussionCore[5] = float[](0.22, 0.19, 0.12, 0.08, 0.01);

    color = texture2D(U_MainTexture, M_coord) * nGaussionCore[0];
    for (int i=1; i<5; ++i)
    {
        color += texture2D(U_MainTexture, vec2(M_coord.x + i * texelOffset, M_coord.y))
                 * nGaussionCore[i];
        color += texture2D(U_MainTexture, vec2(M_coord.x + i * texelOffset, M_coord.y))
                 * nGaussionCore[i];
    }

    gl_FragColor=color ;
}


3、縱向模糊

與高斯模糊類似 不過只是模糊縱向分量

varying vec2 M_coord;
varying vec3 M_normal;
varying vec3 M_WordPos;

uniform sampler2D U_MainTexture;
uniform sampler2D U_SubTexture;

void main()
{
    int coreSize=3;
    int halfCoreSize=coreSize/2;
    float texelOffset=1/100.0;
    vec4 color = vec4(0.0);
    float nGaussionCore[5] = float[](0.22, 0.19, 0.12, 0.08, 0.01);

    color = texture2D(U_MainTexture, M_coord) * nGaussionCore[0];
    for (int i=1; i<5; ++i)
    {
        color += texture2D(U_MainTexture, vec2(M_coord.x, i * texelOffset + M_coord.y))
                 * nGaussionCore[i];
        color += texture2D(U_MainTexture, vec2(M_coord.x, i * texelOffset + M_coord.y))
                 * nGaussionCore[i];
    }

    gl_FragColor=color ;
}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI