Remove unwanted lines in captcha text – opencv – python

Question:

I trying to get text from captcha image using opencv.
Problem is text are masked with noise and it is complex to process with those horizontal line/noise.

Original image

enter image description here

My processed image :

enter image description here

not sure how to remove those horizontal lines and get text

Code :

import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('captcha.jpg',0)

#display image in window
#cv2.imshow('image',img) #@param - windowname, image to be displayed

horizontal_inv = cv2.bitwise_not(img)
#perform bitwise_and to mask the lines with provided mask
masked_img = cv2.bitwise_and(img, img, mask=horizontal_inv)
#reverse the image back to normal
masked_img_inv = cv2.bitwise_not(masked_img)
cv2.imshow("masked img", masked_img_inv)
cv2.imwrite("result2.jpg", masked_img_inv)

cv2.waitKey(0) # time for window to show image in milliseconds - 0 is infinite wait
cv2.destroyAllWindows()

Edit : how to handle if text are in light color

enter image description here

enter image description here

Asked By: Jeya Kumar

||

Answers:

import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('captcha.jpg',0)

#display image in window
#cv2.imshow('image',img) #@param - windowname, image to be displayed

horizontal_inv = cv2.bitwise_not(img)
#perform bitwise_and to mask the lines with provided mask
masked_img = cv2.bitwise_and(img, img, mask=horizontal_inv)
#reverse the image back to normal
masked_img_inv = cv2.bitwise_not(masked_img)

kernel = np.ones((5,5),np.uint8)
dilation = cv2.dilate(masked_img_inv,kernel,iterations = 3) # to remove blackline noise
cv2.imwrite("result1.jpg", dilation)

enter image description here

ret,thresh2 = cv2.threshold(dilation,254,255,cv2.THRESH_BINARY_INV) 
thresh2=cv2.bitwise_not(thresh2)
# cv2.imshow("masked img", masked_img_inv)
cv2.imwrite("result2.jpg", thresh2)

enter image description here

cv2.waitKey(0) # time for window to show image in milliseconds - 0 is infinite wait
cv2.destroyAllWindows()

Let me know if you have future queries.

Answered By: spaceman