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:
I'm not really sure sure how to get rid of the last diagonal line and repair the S, my code so far is:
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')
Any help would be greatly appreciated, Here are more examples of the type of images that are produced from the captcha;
also heres the link to a folder containing the images