title: Removing line artifacts from an image
body: <p>I'm creating an OCR application. It extracts handwritten characters from a boxed section in a scanned or photographed printed form, and reads it using a CNN.</p>
  
  <p>It successfully extracts characters using contours, but there are cases where there are lines that are read too as contours. These lines seem to be the result of either mere noise, or leftover pixels when the boxed section is cropped. The boxed section is cropped using contours.</p>
  
  <p>Basically, it works when the form is scanned with a good scanner, saved in PNG format. Otherwise, it won't work as well. I need it to account for JPEG files too and crap camera/scanners.</p>
  
  <p>This is then more of a question of what possible techniques I can use theoretically.</p>
  
  <p>I'd like to either remove lines, or make the code ignore it.</p>
  
  <p>I've tried:</p>
  
  <ul>
  <li>"padding" the cropped boxed section by a negative number <em>n</em>. So it instead removes <em>n</em> pixels from each side. This can't be used too much though, as it also eats up the pixels of the character.</li>
  <li>use morphological operation "close". Modifying the kernel size does almost nothing significant, though. </li>
  <li>implementing a boxed section area:character area ratio. If the retrieved contour area ratio to the boxed section area is not in the range, it's ignored.</li>
  </ul>
  
  <p>Here's what it looks like: </p>
  
  <p><img src="https://imgur.com/wLERYk1.jpg" alt="1"> <img src="https://imgur.com/F3IXbxn.jpg" alt="3"> </p>
  
  <p><img src="https://imgur.com/7QoTMuz.jpg" alt="4"> <img src="https://imgur.com/8GS7vur.jpg" alt="5"></p>
  
  <p>The grey parts outline the detected contours. The numbers indicate the index of the contour, ordered by the order they are detected. Notice there are strips of lines detected too. I want to get rid of this.</p>
  
  <p>Beside the lines interfering with the model and making it spout nonsense trying to interpret these, there are some cases where it also seems to cause this error:</p>
  
  <pre><code>ValueError: cannot reshape array of size 339 into shape (1,28,28,1)
  </code></pre>
  
  <p>Maybe I'll start with investigating this in the meantime.</p>