title: Pre-processing before digit recognition with KNN classifier
body: <p>Right now I'm trying to create digit recognition system using OpenCV. There are many articles and examples in WEB (and even on <a href="https://stackoverflow.com/questions/9413216/simple-digit-recognition-ocr-in-opencv-python">StackOverflow</a>). I decided to use <a href="http://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm" rel="noreferrer">KNN classifier</a> because this solution is the most popular in WEB. I found a <a href="http://yann.lecun.com/exdb/mnist/" rel="noreferrer">database of handwritten digits</a> with a training set of 60k examples and with error rate less than 5%.</p>
  
  <p>I used <a href="http://www.aishack.in/2010/10/k-nearest-neighbors-in-opencv/" rel="noreferrer">this tutorial</a> as an example of how to work with this database using OpenCV. I'm using exactly same technique and on test data (<code>t10k-images.idx3-ubyte</code>) I've got 4% error rate. But when I try to classify my own digits I've got much bigger error. For example:</p>
  
  <ul>
  <li><img src="https://i.stack.imgur.com/3qEFo.jpg" alt="enter image description here"> is recognized as <strong>7</strong></li>
  <li><img src="https://i.stack.imgur.com/OOfkt.jpg" alt="enter image description here"> and <img src="https://i.stack.imgur.com/rKGee.jpg" alt="enter image description here"> are recognized as <strong>5</strong></li>
  <li><img src="https://i.stack.imgur.com/lDmHP.jpg" alt="enter image description here"> and <img src="https://i.stack.imgur.com/MpfXY.jpg" alt="enter image description here"> are recognized as <strong>1</strong></li>
  <li><img src="https://i.stack.imgur.com/rvKAF.jpg" alt="enter image description here"> is recognized as <strong>8</strong></li>
  </ul>
  
  <p>And so on (I can upload all images if it's needed).</p>
  
  <p>As you can see all digits have good quality and are easily-recognizable for human.</p>
  
  <p>So I decided to do some pre-processing before classifying. From the table on <a href="http://yann.lecun.com/exdb/mnist/" rel="noreferrer">MNIST database site</a> I found that people are using <em>deskewing</em>, <em>noise removal</em>, <em>blurring</em> and <em>pixel shift</em> techniques. Unfortunately almost all links to the articles are broken. So I decided to do such pre-processing by myself, because I already know how to do that.</p>
  
  <p>Right now, my algorithm is the following:</p>
  
  <ol>
  <li>Erode image (I think that my original digits are too<br>
  rough).</li>
  <li>Remove small contours.</li>
  <li>Threshold and blur image.</li>
  <li>Center digit (instead of shifting).</li>
  </ol>
  
  <p>I think that deskewing is not needed in my situation because all digits are normally rotated. And also I have no idea how to find a right rotation angle.
  So after this I've got these images:</p>
  
  <ul>
  <li><img src="https://i.stack.imgur.com/C9DB7.jpg" alt="enter image description here"> is also <strong>1</strong></li>
  <li><img src="https://i.stack.imgur.com/UPNch.jpg" alt="enter image description here"> is <strong>3</strong> (not <strong>5</strong> as it used to be)</li>
  <li><img src="https://i.stack.imgur.com/wJ0g3.jpg" alt="enter image description here"> is <strong>5</strong> (not <strong>8</strong>)</li>
  <li><img src="https://i.stack.imgur.com/ryRqs.jpg" alt="List item"> is <strong>7</strong> (profit!)</li>
  </ul>
  
  <p>So, such pre-processing helped me a bit, but I need better results, because in my opinion such digits should be recognized without problems.</p>
  
  <p>Can anyone give me any advice with pre-processing? Thanks for any help.</p>
  
  <p>P.S. I can upload my source (c++) code.</p>