Calculation of the histogram of brightness in GPU-android opengl es 3.0

To calculate the Luminace histogram

I used the code from the graphic image of the iOS project from Brad Larson.

He used blending to calculate the histogram.

Attaching Vertex and Fragment Shaders

Vertex shader

#version 300 es 
in vec4 position;
out vec3 colorFactor;
const vec3 W = vec3(0.299, 0.587, 0.114);
void main() 
  {
      float luminance = dot(position.xyz, W);
      colorFactor = vec3(1.0, 1.0, 1.0);
      gl_Position = vec4(-1.0 + (luminance *  0.00784313725), 0.0, 0.0, 1.0);
      gl_PointSize = 1.0;
   } ;

Fragment shader

#version 300 es 
const lowp float scalingFactor = 1.0 / 256.0;
in lowp vec3 colorFactor;
out vec4 gl_FragColor;\n"+
void main() 
  {
     gl_FragColor = vec4(colorFactor * scalingFactor , 1.0);
  };

I used a 256x1 texture attached to the FBO and passed the pixel as input to the vertex shader. The texture is defined as follows

GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);

GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA,
                256, 1, 0, GLES30.GL_RGBA,
                GLES30.GL_UNSIGNED_BYTE, null);

And my onDrawFrame looks like this

GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
GLES30.glBlendEquation(GLES30.GL_FUNC_ADD);
GLES30.glBlendFunc(GLES30.GL_ONE, GLES30.GL_ONE);
GLES30.glEnable(GLES30.GL_BLEND);

filterPositionAttribute = mshader2.getHandle("position");


GLES30.glVertexAttribPointer(filterPositionAttribute, 4, GLES30.GL_UNSIGNED_BYTE,false,60, PixelBuffer);
GLES30.glEnableVertexAttribArray(filterPositionAttribute);


GLES30.glDrawArrays(GLES30.GL_POINTS, 0, mViewportWidth * mViewportHeight /16);

i will use 1 pixel out of 16 to calculate the histogram.

Now I get the graph and values. But when validated using matlab and other software like irfanview, it is rejected.

Attaching a graph created in excel value from my application

verified values ​​using matlab

. - ,

+4
1

C GL, :

Vertex shader

#version 330
layout (location = 0) in vec2 inPosition;
void main()
{
    int x = compute the bin (0 to 255) from inPosition;

    gl_Position = vec4(
        -1.0 + ((x + 0.5) / 128.0),
        0.5,
        0.0,
        1.0
    );
}

#version 330
out vec4 outputColor;
void main()
{
    outputColor = vec4(1.0, 1.0, 1.0, 1.0);
}

Init:

glGenTextures(1, &tex);
glGenFramebuffers(1, &fbo);

glActiveTexture(GL_TEXTURE0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32F, 256, 1);

glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex, 0);

:

/* Upload data */
glBufferData(GL_ARRAY_BUFFER, num_input_data * 2 * sizeof(float), input_data_ptr, GL_STREAM_DRAW);

/* Clear buffer */
const float zero[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
glClearBufferfv(GL_COLOR, 0, zero);

/* Init viewport */
glViewport(0, 0, 256, 1);

/* Draw */
glDrawArrays(GL_POINTS, 0, num_input_data);

brievety init , VBO/VAO init/binding

+2

All Articles