c++ 卷积运算后卷积输出是否翻转?

6yjfywim  于 2022-12-01  发布在  其他
关注(0)|答案(1)|浏览(146)

我正在尝试在SparkAR中实现一个浮雕滤镜。它可以工作,但最终输出是翻转的。下面的代码是我从https://www.shadertoy.com/view/ld3XW4修改的,所以它可以在软件中工作,但我不知道我可以做什么,这可能会导致翻转的最终图像。我不太精通编码图像处理的东西,所以任何指针将是非常感谢。
该语言是META中的着色语言:SparkSL帮助保存我剩下的头发。非常感谢。
编辑:哦,我的上帝,代码的语法是如此混乱。我试图编辑它,请容忍我,如果你遇到下面的混乱之前,我可以。

// Source:
// http://coding-experiments.blogspot.com/2010/07/convolution.html

#define EMBOSS_WIDTH    0.0015
#define EMBOSS_HEIGHT 0.0050

// samples a pixel centerd at "uv" and with offset of dx|dy

const vec4 sample_pixel(std:: Texture2d myTex, in vec2 uv, in float dx, in float dy)
{
    return myTex.sample( uv + vec2(dx, dy));
}

// convolves a SINGLE channel of input color_matrix
const float convolve(in float[9] kernel, in vec4[9] color_matrix) 
{
   float res = 0.0;
   for (int i=0; i<9; i++)
   {
      res += kernel[i] * color_matrix[i].a;
   }
   return clamp(res + 0.5, 0.0 ,1.0);
}

// builds a 3x3 color matrix centerd at "uv"

const void build_color_matrix(std:: Texture2d myTex, in vec2 uv, out vec4[9] color_matrix)
{
    float dxtex = EMBOSS_WIDTH;
    float dytex = EMBOSS_HEIGHT;
 
    color_matrix[0].rgb = sample_pixel(myTex, uv, -dxtex, -dytex)   .rgb;
    color_matrix[1].rgb = sample_pixel(myTex, uv, -dxtex,   0.0)    .rgb;
    color_matrix[2].rgb = sample_pixel(myTex, uv, -dxtex,   dytex)  .rgb;
    color_matrix[3].rgb = sample_pixel(myTex, uv, 0.0,  -dytex) .rgb;
    color_matrix[4].rgb = sample_pixel(myTex, uv, 0.0,  0.0)    .rgb;
    color_matrix[5].rgb = sample_pixel(myTex, uv, 0.0,  dytex)  .rgb;
    color_matrix[6].rgb = sample_pixel(myTex, uv, dxtex,    -dytex) .rgb;
    color_matrix[7].rgb = sample_pixel(myTex, uv, dxtex,    0.0)    .rgb;
    color_matrix[8].rgb = sample_pixel(myTex, uv, dxtex,    dytex)  .rgb;
}

// builds a mean color matrix (off of .rgb of input).
// NOTE: stores the output in alpha channel
 void build_mean_matrix(inout vec4[9] color_matrix)
{
   for (int i=0; i<9; i++)
   {
      color_matrix[i].a = (color_matrix[i].r + color_matrix[i].g + color_matrix[i].b) / 3.;
   }
}
 
vec4 mainImage( in std:: Texture2d myTex, out vec4 )
{
    /*2.    0.  0.
        0.  -1  0.
        0.  0.  -1  */
    
    float kerEmboss[9];
    kerEmboss[0] = 2.;
    kerEmboss[1] = 0.0;
    kerEmboss[2] = 0.0;
    kerEmboss[3] = 0.0;
    kerEmboss[4] = -1.;
    kerEmboss[5] = 0.0;
    kerEmboss[6] = 0.0;
    kerEmboss[7] = 0.0;
    kerEmboss[8] = -1.;
    
    vec4 pixel_matrix[9];
  vec2 iResolution = std::getRenderTargetSize();
  vec4 fragCoord = std:: getFragmentCoord();
    vec2 uv = fragCoord.xy / iResolution.xy;
    
    build_color_matrix(myTex, uv, pixel_matrix);
    build_mean_matrix(pixel_matrix);
    
   float convolved = convolve(kerEmboss, pixel_matrix);
    vec4 fragColor = vec4(vec3(convolved), 1.0);
   return fragColor;
}

卷积“着色器”应该产生一个浮雕图像,但垂直翻转它(或旋转)。我试着否定尽可能多的数字,我可以找到,但nada。

omqzjyyz

omqzjyyz1#

尝试在以下位置之后添加此行:

...
    vec2 uv = fragCoord.xy / iResolution.xy;
    uv.y = 1.0 - uv.y; // flipping vertically

   ...

相关问题