Opengl es 使用着色器的颜色检测
我正在将YUV转换为RGB,并且工作正常。现在,我想检测指定颜色特定范围内的所有点,例如所有红点。并相应地显示所有红点,并将其他所有内容渲染为黑白 检测红点的最佳方法是什么 这是我的片段着色器- 代码Opengl es 使用着色器的颜色检测,opengl-es,glsl,shader,Opengl Es,Glsl,Shader,我正在将YUV转换为RGB,并且工作正常。现在,我想检测指定颜色特定范围内的所有点,例如所有红点。并相应地显示所有红点,并将其他所有内容渲染为黑白 检测红点的最佳方法是什么 这是我的片段着色器- 代码 const char *Shaders::yuvtorgb = MULTI_LINE_STRING( varying mediump vec2 v_yTexCoord; varying mediump vec4 v_effectTexCoord;
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通常最容易计算RGB颜色之间的欧几里德距离(或其平方,以避免
sqrt()
)。加权欧氏距离,如√(2Δ²R+4Δ²G+Δ²B)
也被使用。我认为这完全取决于您所说的“在指定颜色的特定范围内”的意思。您可以按照Walter的建议在RGB空间中执行此操作,也可以在YUV空间中执行此操作(特别是因为您已经拥有了它)。这个问题()中的着色器做了一些类似的事情(色度键控),实际上它将RGB转换为YRCB空间只是为了进行比较。如果你已经有YUV数据,你甚至可能想直接使用UV数据,因为这将使颜色检测更独立于亮度(但好的,这实际上取决于“特定范围”是什么)指在您的情况下,如果深红色与浅红色“同等红”)。如果使用RGB空间,如果需要,还可以使用颜色向量之间的“角度”来获得亮度独立性。