Removing black background/black stray straight lines from a captcha in python
Question:
I am trying read text from this image] using Python with OpenCV.
However, black background in corners if this pic is messing with the text output and is giving wrong text.
I tried to used Adaptive Gaussian Thresholding in OpenCV using code:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img=cv.imread(file_path,0)
img = cv.medianBlur(img,5)
ret,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
th2 =cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,
cv.THRESH_BINARY,11,2)
**th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,
cv.THRESH_BINARY,11,2)**
titles = ['Original Image', 'Global Thresholding (v = 127)',
'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in range(4):
plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
The output of this code as AGT_result
How to extract the words only?
Answers:
Here is one way to do that in Python/OpenCV.
- Read the input
- Map the corner black to white with a threshold on black that is slightly darker than your text to mitigate the antialiased white-black transition
- Use morphology close to clean up dotted border
- Save the result
Input:
import cv2
import numpy as np
img = cv2.imread('text_black_corners.png')
# map the black corners to white
img2 = img.copy()
img2[np.where((img2 <= [150,150,150]).all(axis=2))] = [255,255,255]
# apply morphology close
kernel = cv2.getStructuringElement(cv2.MORPH_RECT , (3,3))
result = cv2.morphologyEx(img2, cv2.MORPH_CLOSE, kernel)
# write result to disk
cv2.imwrite("text_black_corners_removed.png", result)
# display it
cv2.imshow("img2", img2)
cv2.imshow("result", result)
cv2.waitKey(0)
Result:
As an ad-hoc solution, we may use cv2.floodFill
4 times – one at each corner:
img = cv.imread(file_path, 0)
rows, cols = img.shape
cv.floodFill(img, None, seedPoint=(0, 0), newVal=255, loDiff=1, upDiff=1) # Fill the top left corner.
cv.floodFill(img, None, seedPoint=(cols-1, 0), newVal=255, loDiff=1, upDiff=1) # Fill the top right corner.
cv.floodFill(img, None, seedPoint=(0, rows-1), newVal=255, loDiff=1, upDiff=1) # Fill the bottop left corner.
cv.floodFill(img, None, seedPoint=(cols-1, rows-1), newVal=255, loDiff=1, upDiff=1) # Fill the bottom right corner.
I am trying read text from this image] using Python with OpenCV.
However, black background in corners if this pic is messing with the text output and is giving wrong text.
I tried to used Adaptive Gaussian Thresholding in OpenCV using code:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img=cv.imread(file_path,0)
img = cv.medianBlur(img,5)
ret,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
th2 =cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,
cv.THRESH_BINARY,11,2)
**th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,
cv.THRESH_BINARY,11,2)**
titles = ['Original Image', 'Global Thresholding (v = 127)',
'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in range(4):
plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
The output of this code as AGT_result
How to extract the words only?
Here is one way to do that in Python/OpenCV.
- Read the input
- Map the corner black to white with a threshold on black that is slightly darker than your text to mitigate the antialiased white-black transition
- Use morphology close to clean up dotted border
- Save the result
Input:
import cv2
import numpy as np
img = cv2.imread('text_black_corners.png')
# map the black corners to white
img2 = img.copy()
img2[np.where((img2 <= [150,150,150]).all(axis=2))] = [255,255,255]
# apply morphology close
kernel = cv2.getStructuringElement(cv2.MORPH_RECT , (3,3))
result = cv2.morphologyEx(img2, cv2.MORPH_CLOSE, kernel)
# write result to disk
cv2.imwrite("text_black_corners_removed.png", result)
# display it
cv2.imshow("img2", img2)
cv2.imshow("result", result)
cv2.waitKey(0)
Result:
As an ad-hoc solution, we may use cv2.floodFill
4 times – one at each corner:
img = cv.imread(file_path, 0)
rows, cols = img.shape
cv.floodFill(img, None, seedPoint=(0, 0), newVal=255, loDiff=1, upDiff=1) # Fill the top left corner.
cv.floodFill(img, None, seedPoint=(cols-1, 0), newVal=255, loDiff=1, upDiff=1) # Fill the top right corner.
cv.floodFill(img, None, seedPoint=(0, rows-1), newVal=255, loDiff=1, upDiff=1) # Fill the bottop left corner.
cv.floodFill(img, None, seedPoint=(cols-1, rows-1), newVal=255, loDiff=1, upDiff=1) # Fill the bottom right corner.