使用着色器进行颜色检测

时间:2013-07-01 09:59:29

标签: opengl-es glsl shader

我正在将YUV转换为RGB并且工作正常。现在,我想要检测指定颜色的特定范围内的所有点,例如所有红点。并相应地显示所有红点并将其他所有内容呈现为黑色和白色。

哪种方法可以检测到红点?

这是我的片段着色器 -

CODE

const char *Shaders::yuvtorgb = MULTI_LINE_STRING(
        varying mediump vec2 v_yTexCoord;
        varying mediump vec4 v_effectTexCoord;

        uniform sampler2D yTexture;
        uniform sampler2D uvTexture;

        // YUV to RGB decoder working fine.
        mediump vec3 yuvDecode(mediump vec2 texCoord)
        {
            // Get y
            mediump float y = texture2D(yTexture, texCoord).r;
            y -= 0.0627;
            y *= 1.164;

            // Get uv
            mediump vec2 uv = texture2D(uvTexture, texCoord).ra;
            uv -= 0.5;

            // Convert to rgb
            mediump vec3 rgb;
            rgb = vec3(y);
            rgb += vec3( 1.596 * uv.x, - 0.813 * uv.x - 0.391 * uv.y, 2.018 * uv.y);

            return rgb;
        }

   //Detects red color and outputs B&W image but with red points 
    mediump vec3 detectColor(mediump vec3 rgbColor)
    {
        mediump vec3 redTexture = vec3(rgbColor);

        mediump float r = redTexture.r;
        mediump float g = redTexture.g;
        mediump float b = redTexture.b;

        if (// Detect red color) {
            //if primary color is red
            //leave as it is
        } else {
            //primary color is not red
            //apply B&W image.
            mediump float y = texture2D(yTexture, v_yTexCoord.xy).r;
            y -= 0.0627;
            y *= 1.164;
            redTexture = vec3(y);
        }
        return redTexture;
    }

        void main()
        {
            mediump vec3 rgb = yuvDecode(v_yTexCoord.xy);
            rgb = detectColor(rgb);

            gl_FragColor = vec4(rgb, 1.0);
        }

);

更新

使用OpenGLES 2.0

0 个答案:

没有答案