title: Binarization methods, middle-threshold binarisation
body: <p>I'm trying to binarise a picture, firstly of course having it prepared(grayscaling)
  My method is to find the maximum and minimum values of grayscale, then find the middle value(which is my threshold) and then, iterating over all the pixels I compare the current one with a threshold and if the grayscale is larger than the threshold, I put 0 in a matrix, or for the others I put 1.
  But now I'm facing the problem. In common I'm binarising images with white background, so my algorithm is further based on this feature. But when I meet an image with black background everything collapses, but I still can see the number clearly(now 0's and 1's switch places)
  How can i solve this problem, make my program more common?
  Maybe I'd better look for another ways of binarization/</p>
  
  <p>P.S. I looked for an understandable explanation of Otsu threshold method, but it seems either I'm not prepared for this way of difficulty or I find very complicated explanations every time, but I can't write it in C. If anyone could hrlp here, it'd be wonderful.</p>
  
  <p>Sorry for not answering the questions, just didn't see them
  Firstly - the code
  </p>
  
  <pre><code>for (int y=1;y&lt;Source-&gt;Picture-&gt;Height;y++)
      for (int x=1;x&lt;Source-&gt;Picture-&gt;Width;x++)
      {
          unsigned green = GetGValue(Source-&gt;Canvas-&gt;Pixels[x][y]);
          unsigned red = GetRValue(Source-&gt;Canvas-&gt;Pixels[x][y]);
          unsigned blue = GetBValue(Source-&gt;Canvas-&gt;Pixels[x][y]);
          threshold = (0.2125*red+0.7154*green+0.0721*blue);
          if (min&gt;threshold)
              min=threshold;
          if (max&lt;threshold)
              max = threshold;
      }
      middle = (max+min)/2;
  </code></pre>
  
  <p>Then iterating through the image </p>
  
  <pre><code>        if (threshold&lt;middle)
          {
              picture[x][y]=1;
              fprintf( fo,"1");
          } else {
              picture[x][y]=0;
              fprintf( fo,"0");
          }
      }
      fprintf( fo,"\n");
  }
  fclose(fo);  
  </code></pre>
  
  <p>So I get a file, something like this</p>
  
  <blockquote>
    <p>000000000<br>
    000001000<br>
    000001000<br>
    000011000<br>
    000101000<br>
    000001000<br>
    000001000<br>
    000001000<br>
    000000000</p>
  </blockquote>
  
  <p>Here you can see an example of one. <img src="https://i.stack.imgur.com/Jtv8o.png" alt="enter image description here"></p>
  
  <p>Then I can interpolate it, or do something else (recognize), depending on zero's and one's.
  But if I switch the colors, the numbers won't be the same. So the recognition will not work. I wonder if there's an algoritm that can help me out.</p>