title: Auto crop black borders from a scanned image by making stats about gray values (Java)
body: <p>I'm writing a piece of code to automatically detect black noisy borders on scanned images and crop them off.
  My algorithm is based on 2 variables: gray mean value (of the pixels in a rows/columns) and position (of a row/column in the image).</p>
  <p>GRAY MEAN VALUE<br>
  Images are in gray scale: this means that any pixel has a gray value in the range 0 (black), 255 (white).<br>
  For each row/column of pixels, I estimate the mean gray value for all the pixels in that row/column.<br>
  If the result is dark, then the current row/column is part of the border to cut off.</p>
  <p>POSITION<br>
  The position is the distance in pixel of a row/column from the top left corner of the image.</p>
  <p>Take a look at the following images for a better idea.<br>
  Thumbnail of a scanned image:<br>
  <img src="https://i.stack.imgur.com/Y7SeE.png" alt="Thumbnail of a scanned image" /><br>
  Resulting chart:<br>
  <img src="https://i.stack.imgur.com/8Ng8P.png" alt="Resulting char" /></p>
  <p>It is very easy, by looking at the chart, to estimate where the cropping points are because of the following rule: the most of the samples are in a white narrow range (150-200) which is the actual paper, then in the tails there is a quick change to dark values.<br>
  Those quick changes are the cropping points (Notice also that in the really end of tails there can still be white for a few pixelx, but this seldom happens).</p>
  <p>I want to do it automatically, is there any statistics which can help me out?<br>
  PS: I'm a computer engineer, I've studied some statistics but... too many years ago!!</p>
  <p>In the best case scenario the code should work with any scanned image affected by the black border problem, but, getting real, I'll be satisfied to make it work with these samples:<br>
  <a href="https://docs.google.com/folder/d/0B8ubCWBwsuOON3d1VVo4Z1AxWDA/edit" rel="nofollow noreferrer">https://docs.google.com/folder/d/0B8ubCWBwsuOON3d1VVo4Z1AxWDA/edit</a></p>