title: Cleaning up captcha image
body: <p><a href="https://i.stack.imgur.com/A1nO4.png" rel="noreferrer"><img src="https://i.stack.imgur.com/A1nO4.png" alt="captcha image"></a></p>
  
  <p>I'm trying to clean up the image above I've tried several different methods using open cv, I either erode the original image too much to the point where parts of the letters become missing such as below:</p>
  
  <p><a href="https://i.stack.imgur.com/AjyHV.png" rel="noreferrer"><img src="https://i.stack.imgur.com/AjyHV.png" alt="result of erosion via python opencv 3"></a></p>
  
  <p>I'm not really sure sure how to get rid of the last diagonal line and repair the S, my code so far is:</p>
  
  <pre><code>import cv2 
  import matplotlib.pylab as plt
  img = cv2.imread('/captcha_3blHDdS.png')
  
  #make image gray 
  gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  
  #Blur
  blur = cv2.GaussianBlur(gray,(5,5),0)
  bilateral = cv2.bilateralFilter(gray,5,75,75)
  
  #Thresholding
  ret, thresh = cv2.threshold(bilateral,25,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
  
  #Kernal
  kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
  
  #other things
  erosion = cv2.erode(thresh,kernel,iterations = 1)
  closing = cv2.morphologyEx(erosion, cv2.MORPH_CLOSE, kernel, iterations = 1)
  
  #Transform image
  dist_transform = cv2.distanceTransform(closing,cv2.DIST_L2,5)
  ret, sure_fg = cv2.threshold(dist_transform,0.02*dist_transform.max(),255,cv2.THRESH_BINARY)#,255,0)
  
  #kernel_1
  kernel_1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1, 2))
  
  dilation_1 = cv2.dilate(sure_fg,kernel_1,iterations = 2)
  erosion_1 = cv2.erode(dilation_1,kernel_1,iterations = 3)
  
  plt.imshow(erosion_1, 'gray')
  </code></pre>
  
  <p>Any help would be greatly appreciated, Here are more examples of the type of images that are produced from the captcha;
  <a href="https://i.stack.imgur.com/QA7GH.png" rel="noreferrer"><img src="https://i.stack.imgur.com/QA7GH.png" alt="example of captcha images"></a></p>
  
  <p>also heres the link to a folder containing the <a href="https://www.dropbox.com/sh/c2h4yq76ly85z65/AADJ6Hfn_KjuynjxfnTXgzjqa?dl=0" rel="noreferrer">images</a></p>