title: Remove horizontal lines with Open CV
body: <p>I am trying to remove horizontal lines from my daughter's drawings, but can't get it quite right.</p>
  <p>The approach I am following is creating a mask with horizontal lines (<a href="https://stackoverflow.com/a/57410471/1873521">https://stackoverflow.com/a/57410471/1873521</a>) and then removing that mask from the original (<a href="https://docs.opencv.org/3.3.1/df/d3d/tutorial_py_inpainting.html" rel="noreferrer">https://docs.opencv.org/3.3.1/df/d3d/tutorial_py_inpainting.html</a>).</p>
  <p>As you can see in the pics below, this only partially removes the horizontal lines, and also creates a few distortions, as some of the original drawing horizontal-ish lines also end up in the mask.</p>
  <p>Any help improving this approach would be greatly appreciated!</p>
  <h2>Create mask with horizontal lines</h2>
  <p>From <a href="https://stackoverflow.com/a/57410471/1873521">https://stackoverflow.com/a/57410471/1873521</a></p>
  <pre><code>import cv2
  import numpy as np
  
  img = cv2.imread(&quot;input.png&quot;, 0)
  
  if len(img.shape) != 2:
      gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  else:
      gray = img
  
  gray = cv2.bitwise_not(gray)
  bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, 
  cv2.THRESH_BINARY, 15, -2)
  
  horizontal = np.copy(bw)
  
  cols = horizontal.shape[1]
  horizontal_size = cols // 30
  
  horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (horizontal_size, 1))
  
  horizontal = cv2.erode(horizontal, horizontalStructure)
  horizontal = cv2.dilate(horizontal, horizontalStructure)
  
  cv2.imwrite(&quot;horizontal_lines_extracted.png&quot;, horizontal)
  
    
  </code></pre>
  <h2>Remove horizontal lines using mask</h2>
  <p>From <a href="https://docs.opencv.org/3.3.1/df/d3d/tutorial_py_inpainting.html" rel="noreferrer">https://docs.opencv.org/3.3.1/df/d3d/tutorial_py_inpainting.html</a></p>
  <pre><code>import numpy as np
  import cv2
  mask = cv2.imread('horizontal_lines_extracted.png',0)
  dst = cv2.inpaint(img,mask,3,cv2.INPAINT_TELEA)
  cv2.imwrite(&quot;original_unmasked.png&quot;, dst)
  </code></pre>
  <h1>Pics</h1>
  <h3>Original picture</h3>
  <p><a href="https://i.stack.imgur.com/i0RDA.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/i0RDA.jpg" alt="Original picture" /></a></p>
  <h3>Mask</h3>
  <p><a href="https://i.stack.imgur.com/hV2qK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/hV2qK.png" alt="enter image description here" /></a></p>
  <h3>Partially cleaned:</h3>
  <p><a href="https://i.stack.imgur.com/WomdV.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/WomdV.jpg" alt="Partially cleaned" /></a></p>