Glsl shader for adaptive thresholding?

hello friends,

i’m working with black-on-white images (shapie on paper) and need to turn them into clean white-on-black video; i’d like to do it with a GLSL shader, but would like a smarter-then-the-most-basic thresholding algorithm (i.e. smoothing, adaptive, …).

has anyone here work with one?

ali

Does something like http://www.codingadventures.com/2008/06/threshold-filter-in-glsl/ help at all? It’s not adaptive but I think you could start with something like that perhaps.

1 Like

I was also wondering about this. Did you ever find any adaptive threshold shader?

@joshuajnoble It looks like codingadventures.com is no more, do you remember what was in that link?

Edit: nvm! I found it on the wayback machine
https://web.archive.org/web/20120620142147/http://www.codingadventures.com/2008/06/threshold-filter-in-glsl

kernel vec4 threshold(sampler image, float midPoint, float range )

{

  vec4 pixel=unpremultiply( sample(image, samplerCoord(image)) );



  float high = midPoint + range * 0.5;

  float low = midPoint – range * 0.5;

  

  high = min(1.0, high);

  low = max(0.0, low);

  

  float brightness = 0.3 * pixel.x + 0.59 * pixel.y+ 0.11 *pixel.z;

    

  brightness = step( low, brightness ) * brightness;

  

  brightness = brightness + step(  high, brightness );

  brightness = min( 1.0, brightness );

    

  pixel.x = pixel.y =pixel.z = brightness;

  

  return premultiply(pixel);

}