【问题标题】:Calculate mean of a row in the fragment shader (OpenGL ES 2.0)计算片段着色器中一行的平均值(OpenGL ES 2.0)
【发布时间】:2017-11-12 08:28:46
【问题描述】:

我目前正在编写一个图像处理应用程序。为了达到所需的性能,我必须使用 GPU 来计算相机输入,更具体地说,使用 OpenGL ES 2.0。 在这个项目 (https://github.com/yulu/ShaderCam) 的帮助下,我实现了将图像传递到管道并使用片段着色器进行简单操作(如反转颜色等)。

我对 GLSL、片段着色器和顶点着色器的了解相当有限,但我知道管道限制以及这两个着色器在管道中的作用。 所以 - 制定问题 - 我想计算我收到的图像中一行的平均颜色并将其(每行)返回给我的应用程序。 我在这里读到https://stackoverflow.com/a/13866636/8038866 这通常是可能的,但是我似乎无法找到以下内容:

1 (编辑:通过简单地将我的纹理的 w 和 h 传递给顶点和片段着色器来解决): 知道行结束的位置(并在片段着色器中拥有该信息)。为此,我假设我必须将图片的宽度传递给顶点着色器,然后从那里传递给片段着色器,对吧?

2.:如何计算片段着色器中每一行的颜色值的平均值,然后将它们传递给应用程序。如果我理解正确 - 片段着色器只执行每个像素的代码,所以我不知道如何实现这一点。

这是两个非常基本的着色器 顶点着色器:

uniform mat4 uTransformM;
uniform mat4 uOrientationM;
uniform vec2 ratios;
attribute vec2 aPosition;

varying vec2 vTextureCoord;

void main(){
    gl_Position = vec4(aPosition, 0.0, 1.0);
    vTextureCoord = (uTransformM * ((uOrientationM * gl_Position + 1.0)*0.5)).xy;
    gl_Position.xy *= ratios;
}

片段着色器:

#extension GL_OES_EGL_image_external : require

precision mediump float;

uniform samplerExternalOES sTexture;
varying vec2 vTextureCoord;

void main(){
    gl_FragColor = texture2D(sTexture, vTextureCoord);
    //calc mean per row and pass it back
}

我非常感谢您提供的每一个建议或帮助。

【问题讨论】:

    标签: android image-processing opengl-es glsl opengl-es-2.0


    【解决方案1】:

    我找到了一种对我有用的方法。这个想法是只计算一行像素的平均值,然后在应用程序中用

    得到这条线

    glReadPixels( GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid * data);

    这是我的片段着色器(注意表面的宽度也是必需的):

    #extension GL_OES_EGL_image_external : require
    
    precision mediump float;
    
    uniform samplerExternalOES sTexture;
    varying float width;
    varying vec2 vTextureCoord;
    
    void main(){
        vec4 accumulatedRGB = texture2D(sTexture, vec2(0,vTextureCoord.y));
        if(vTextureCoord.x < 0.50 && vTextureCoord.x > 0.499){ //small enough to only cover one line
            for(float i=1.0;i<=width;++i)
            {
                float xPosOnTexture = i/width;
                vec4 current =  texture2D(sTexture, vec2(xPosOnTexture,vTextureCoord.y));
                accumulatedRGB += current;
            }
            vec4 mean = accumulatedRGB/width;
    
            gl_FragColor = vec4(mean.rgb , mean.a);//avg color for one line
        }
        else{
            gl_FragColor = vec4(0.0,0.0,0.0,0.0);//rest of the screen
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多