Thursday, June 17, 2010

Binary filter Pixel Shader

Binary image is image composed only from 2 colors. Typical conversion algorithm to binary image is this -> If pixel's averaged intensity is greater than threshold - draw pixel in first color, otherwise- draw it in second color. But you can define other methods of conversion to binary as well.
For example another method can be - If pixel's color is near target color (within error bounds) - draw pixel in first color, otherwise - draw it in second. This conversion rule is implemented in following GLSL pixel shader code:

uniform sampler2D tex;

bool nearColor(in vec4 col, in vec4 tcol, in float error) {
return abs(col.r-tcol.r) < error &&
abs(col.g-tcol.g) < error &&
abs(col.b-tcol.b) < error ;

void main()
vec4 xcol = texture2D(tex, gl_TexCoord[0].xy);
vec4 scol = vec4(0.737,0.506,0.404,1.0);
if (nearColor(xcol,scol,0.085))
gl_FragColor = scol;
gl_FragColor = vec4(0.0,0.0,0.0,1.0);


converted to binary mode:

No comments:

Post a Comment

Comment will be posted after comment moderation.
Thank you for your appreciation.