Is there a way to remove complex lines from an image with OpenCV?

Rinilorri

New member
I have this 3 CAPTCHA images listed below and I want to make the text as clean as possible using opencv. Is there a pipeline that can achieve what I want?

T3YM6.png


vPEZw.png


AsJBn.png


Here is an example of what I want:

JEOdL.png


The biggest problem here is the color difference in letters. Any help would be appreciated!

Here are some parts from my code with all the image preprocessing

Code:
raw_img = cv2.imread(image_file) #load image

img = cv2.cvtColor(raw_img, cv2.COLOR_RGB2GRAY) #grayscale



_,thresh = cv2.threshold(img,240,255, cv2.THRESH_BINARY_INV + cv2.THRESH_TRUNC) #thresholding



thresh = cv2.bitwise_not(thresh) #invert black and white



#resizing image

resized = cv2.resize(img, (140, 60), interpolation=cv2.INTER_AREA)



img_gray = cv2.copyMakeBorder(resized, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=[255, 255, 255])



#blur

blur = cv2.GaussianBlur(img_gray, (3, 3), 0)



# threshold again

_, img = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY or cv2.THRESH_OTSU)



cv2.imshow("Output", img)

cv2.waitKey()

However, if I apply this same code to the second iamge, for example, here is what I get:
5pZKA.png


I'm relatively new to OpenCV. I´m not sure if I am using the right approach here, if someone can help me showing other methods to clean this images I´d be greatful!