屏幕后处理效果
高斯模糊
卷积的另一个常见应用就是高斯模糊。模糊的方法有很多,如均值模糊和中值模糊。均值模糊也是用了卷积操作,它使用的卷积核中的各个权重值相等,并且相加等于1,也就是卷积之后得到的像素值是其相邻像素值的平均值。中值模糊则是选择邻域内对所有像素排序后选择中间的值替换原来的颜色。而更高级的做法是高斯模糊。
一、高斯滤波
这篇文章讲的比较好,感谢作者的分享!
简单易懂的高斯滤波
高斯滤波有两个重要的步骤:
1、根据高斯分布算出高斯核
2、根据高斯核做卷积相加
高斯函数:
高斯核的计算步骤:
详见简单易懂的高斯滤波,作者讲的挺清楚的。
但是实际在图像处理中,直接使用NN的高斯核进行采样效率有点低,因为采样次数和N有关,NN的采样频率有点高。。。所以可以将一个二维的高斯函数拆分成两个一维的高斯函数,也就是使用两个高斯核,竖直水平方向各做一次卷积。同时去掉卷积核中重复的元素。
从卷积核中也可以看出离得近的点影响大,离得远的影响小,自身对自身的影响最大(正态分布)
二、实现
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GaussianBlur : PostEffectsBase
{
public Shader gaussianBlurShader;
private Material gaussianBlurMaterial = null;
public Material material
{
get
{
gaussianBlurMaterial = CheckShaderAndCreateMaterial(gaussianBlurShader, gaussianBlurMaterial);
return gaussianBlurMaterial;
}
}
[Range(0, 8)] public int downSample = 1;
[Range(0, 4)] public int iteration = 3;
[Range(0.2f, 3.0f)] public float blurSpread = 0.6f;
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
if (material != null)
{
//对原纹理做一次缩放
int rtW = src.width / downSample;
int rtH = src.height / downSample;
RenderTexture buffer0 = RenderTexture.GetTemporary(rtW, rtH, 0);
buffer0.filterMode = FilterMode.Bilinear;
Graphics.Blit(src,buffer0);
//每次迭代,水平竖直方向各做一次高斯模糊
for (int i = 0; i < iteration; i++)
{
material.SetFloat("_BlurSize", 1.0f + i * blurSpread);
RenderTexture buffer1 = RenderTexture.GetTemporary(rtW,rtH,0);
Graphics.Blit(buffer0,buffer1,material,0);
RenderTexture.ReleaseTemporary(buffer0);
buffer0 = buffer1;
buffer1 = RenderTexture.GetTemporary(rtW,rtH,0);
Graphics.Blit(buffer0,buffer1,material,1);
RenderTexture.ReleaseTemporary(buffer0);
buffer0 = buffer1;
}
Graphics.Blit(buffer0,dest);
RenderTexture.ReleaseTemporary(buffer0);
}
else
{
Graphics.Blit(src,dest);
}
}
}
Shader "Unlit/GaussionBlur"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_BlurSize("BlurSize",float) = 1.0
}
SubShader
{
CGINCLUDE
#include "UnityCG.cginc"
sampler2D _MainTex;
half4 _MainTex_TexelSize;
float _BlurSize;
struct v2f
{
float4 pos:SV_POSITION;
half2 uv[5]:TEXCOORD0;
};
v2f GuassionBlurVertical(appdata_img v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
half2 uv = v.texcoord;
o.uv[0] = uv;
o.uv[1] = uv + float2(0,_MainTex_TexelSize.y * 1) * _BlurSize;
o.uv[2] = uv - float2(0,_MainTex_TexelSize.y * 1) * _BlurSize;
o.uv[3] = uv + float2(0,_MainTex_TexelSize.y * 2) * _BlurSize;
o.uv[4] = uv - float2(0,_MainTex_TexelSize.y * 2) * _BlurSize;
return o;
}
v2f GuassionBlurHorizontal(appdata_img v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
half2 uv = v.texcoord;
o.uv[0] = uv;
o.uv[1] = uv + float2(_MainTex_TexelSize.x * 1,0) * _BlurSize;
o.uv[2] = uv - float2(_MainTex_TexelSize.x * 1,0) * _BlurSize;
o.uv[3] = uv + float2(_MainTex_TexelSize.x * 2,0) * _BlurSize;
o.uv[4] = uv - float2(_MainTex_TexelSize.x * 2,0) * _BlurSize;
return o;
}
fixed4 fragBlur(v2f i):SV_TARGET
{
//本来卷积核是{0.0545,0.2442,0.4026,0.2442,0.0545};
//去掉重复的
float weight[3] = {0.4026, 0.2442, 0.0545};
fixed3 sum = tex2D(_MainTex,i.uv[0]) * weight[0];
//根据权重做卷积操作
for(int it = 1; it < 3; it++)
{
sum += tex2D(_MainTex,i.uv[2*it-1]) * weight[it];
sum += tex2D(_MainTex,i.uv[2*it]) * weight[it];
}
return fixed4(sum,1.0);
}
ENDCG
ZTest Always Cull Off ZWrite Off
Pass
{
NAME "GAUSSION_BLUR_VERTICAL"
CGPROGRAM
#pragma vertex GuassionBlurVertical
#pragma fragment fragBlur
ENDCG
}
Pass
{
NAME "GAUSSION_BLUR_HORZONTAL"
CGPROGRAM
#pragma vertex GuassionBlurHorizontal
#pragma fragment fragBlur
ENDCG
}
}
}