GPU-Android OpenGL ES 3.0中的亮度直方图计算 [英] Luminance histogram calculation in GPU-android opengl es 3.0

查看:404
本文介绍了GPU-Android OpenGL ES 3.0中的亮度直方图计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于计算亮度直方图

我使用了Brad Larson的项目GPU图像ios中的代码.

I have used the code from the project GPU image ios by Brad Larson.

他已将混合用于直方图计算.

He has used blending for Histogram calculation.

附加顶点和片段"着色器

Attaching the Vertex and Fragment 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;
   } ;

片段着色器

#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);
  };

我已经将256x1纹理附加到FBO,并将像素中的Pass作为顶点着色器的输入. 纹理定义如下

i have used a 256x1 texture attached to an FBO and Pass in the pixel as input to vertex shader. 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);

我的onDrawFrame像这样

And my onDrawFrame goes 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);

我将使用16个像素中的1个进行直方图计算.

i will be using 1 pixel out of 16 for histogram calculation.

现在我正在获取图形和值. 但是,当使用matlab和irfanview等其他软件进行验证时,它似乎出现了偏差.

Now i am getting the graph and values. But when verified using matlab and other software like irfanview ,it appears deviated.

附加在excel中创建的图形 我的应用程序中的值

Attaching the graph created in excel values from my application

使用matlab验证的值

这种变化是预期的,还是我犯了一些错误. 任何人都可以帮助,请提前感谢

is this variation expected or i am making some mistakes. Could anybody help ,thanks in advance

推荐答案

我正在使用C和桌面GL,但这是要点:

I'm using C and desktop GL, but here's the gist of it :

顶点着色器

#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);
}

初始化:

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);

为了简洁起见,我只将初始化代码放在结果缓冲区中,所有的VBO/VAO初始化/绑定都被跳过了

For brievety I only put the init code for the resulting buffer, all the VBO/VAO init/binding has been skipped

这篇关于GPU-Android OpenGL ES 3.0中的亮度直方图计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆