简体   繁体   English

着色器上具有卷积矩阵的高斯模糊

[英]Gaussian Blur with convolution matrix on shader

I am trying to implement a gaussian blur with convolution matrix on my shader. 我正在尝试在我的着色器上使用卷积矩阵实现高斯模糊。

This is the code i have: 这是我的代码:

float4 ppPS(float2 uv : TEXCOORD0, uniform sampler2D t1) : COLOR { 
  //kernel matrix
  float3x3 kernel={1*(1/16),2*(1/16),1*(1/16),
  2*(1/16),4*(1/16),2*(1/16),
  1*(1/16),2*(1/16),1*(1/16)
  };

  int x,y;
  float2 sum = 0;
  for (x = -1; x <= 1; x++)
  {
    for (y = -1; y <= 1; y++)
    {
      float2 fl;
      fl.x = uv.x+x;
      fl.y = uv.y+y; 
      sum += (fl)*(kernel[x+1][y+1]);
    }
  }
  return tex2D(t1, sum);
 }

but for some reason, i get a picture all in one solid color. 但是由于某种原因,我只能用一种纯色获得一张照片。

Here is the image without the blur: 这是没有模糊的图像:

不模糊

Here is the image with the so called blur: 这是带有所谓模糊的图像:

带有模糊

any idea of what am i doing wrong over here? 我在这做错什么的任何想法吗?

Try to change the float3x3 initialize values into floating point format (.0f) otherwise all the values will end up as 0. 尝试将float3x3初始化值更改为浮点格式(.0f),否则所有值最终都将变为0。

 //kernel matrix
 static const float3x3 kernel={1*(1.0f/16.0f),2*(1.0f/16.0f),1*(1.0f/16.0f),
 2*(1.0f/16.0f),4*(1.0f/16.0f),2*(1.0f/16.0f),
 1*(1.0f/16.0f),2*(1.0f/16.0f),1*(1.0f/16.0f)
};

After this change you wouldn't see the blank output image !!! 更改之后,您将看不到空白的输出图像!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM