如何获取OpenTK中纹理的“平均强度”?

时间:2019-02-01 11:13:01

标签: c# opengl opentk

我有一个使用openGL显示图像的应用程序。现在,我想找到该纹理的平均强度,并需要将其设置为强度。下面的代码用于设置图像的强度。此代码会将RGB颜色转换为HSV(色相,饱和度,值)。更改饱和度并将其转换回RGB值。如何将强度设置为该纹理的平均强度?

 private void CreateShaders()
{
    /***********Vert Shader********************/
    vertShader = GL.CreateShader(ShaderType.VertexShader);
    GL.ShaderSource(vertShader, @"attribute vec3 a_position;
    varying vec2 vTexCoord;                                            
    void main() {
    vTexCoord = (a_position.xy + 1) / 2;
    gl_Position = vec4(a_position,1);

   }");
    GL.CompileShader(vertShader);

    /***********Frag Shader ****************/
    fragShader = GL.CreateShader(ShaderType.FragmentShader);
    GL.ShaderSource(fragShader, @"precision highp float;
    uniform sampler2D sTexture;
    varying vec2 vTexCoord;
    vec3 RGBtoHSV(in vec3 RGB)
    {
        vec4 P = (RGB.g < RGB.b) ? vec4(RGB.bg, -1.0, 2.0 / 3.0) : vec4(RGB.gb, 0.0, -1.0 / 3.0);
        vec4 Q = (RGB.r < P.x) ? vec4(P.xyw, RGB.r) : vec4(RGB.r, P.yzx);
        float C = Q.x - min(Q.w, Q.y);
        float H = abs((Q.w - Q.y) / (6.0 * C + Epsilon) + Q.z);
        vec3 HCV = vec3(H, C, Q.x);
        float S = HCV.y / (HCV.z + Epsilon);
        return vec3(HCV.x, S, HCV.z);
    }
    vec3 HSVtoRGB(in vec3 HSV)
    {
        float H = HSV.x;
        float R = abs(H * 6.0 - 3.0) - 1.0;
        float G = 2.0 - abs(H * 6.0 - 2.0);
        float B = 2.0 - abs(H * 6.0 - 4.0);
        vec3 RGB = clamp(vec3(R, G, B), 0.0, 1.0);
        return ((RGB - 1.0) * HSV.y + 1.0) * HSV.z;
    }

    void main ()
    {
vec4  color = texture2D (sTexture, vTexCoord);
float u_saturate=0.9;
vec3 col_hsv = RGBtoHSV(color.rgb);
col_hsv.y *= (u_saturate * 2.0);
vec3 col_rgb = HSVtoRGB(col_hsv.rgb);   
gl_FragColor = vec4(col_rgb.rgb, color.a);                
    }");
    GL.CompileShader(fragShader);
}

0 个答案:

没有答案