title: How best to approach a localised thresholding opengl function
body: <p>I would like to take a photo of some text and make the text easier to read.   The tricky part is that the initial photo may have dark regions as well as light regions and I want the opengl function to enhance the text in all these regions.</p>
  
  <p>Here is an example.  On top is the original image.  On bottom is the processed images.</p>
  
  <p><img src="https://i.stack.imgur.com/yiU6c.png" alt="enter image description here"></p>
  
  <p><em>[edited]
  I have added in a better example picture of what is happening. I am able to enhance the text, but in areas where I have no text, this simple thresholding is creating speckled noise (image bottom left).
  If I wind back the threshold, then I lose the text in the darker region (bottom right).</em></p>
  
  <p>At the moment, the processed image only picks up some of the text, not all the text.  The original algorithm I used was pretty simple:
  - sample 8 pixels around the current pixel (pixels about 4-5 distant away seem to work best)
  - figure out the lightest and darkest pixels from this sample
  - if the current pixel is closer to the darkest threshold, then make black, and vice versa</p>
  
  <p>This seemed to work very well for around text, but when it came to non-text, then it provided a very noisy image (even when I provided an initial rejection threshold)</p>
  
  <p>I modified this algorithm to assume that text was always close to black.  This provided the bottom image above, but once again I am not able to pull out all the text features I want.</p>