I am attempting to repair broken text (the images below) so that I can perform OCR on the images. How do I go about repairing the text below? I have already tried dilation, erosion, morphology closing, and using the distance between contours. None of these seem to work. I would appreciate any help, thanks.
Broken Text:
Attempted Solutions (none work):
import cv2 import pytesseract import numpy as npimg = cv2.imread (“/Users/2020shatgiskessell/Desktop/OpenSlate/FN2.png”)
def OCR (img):
config = (‘-l eng --oem 1 --psm 3’)
text = pytesseract.image_to_string(img, config = config)
return textdef get_countour(img):
try:
output = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
output = output.copy()
except Exception:
output = img.copy()
#imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#ret, thresh = cv2.threshold(output, 127, 255, 0)
contours, hierarchy = cv2.findContours(output, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
c = max(contours, key = cv2.contourArea)
contours.remove©
cv2.drawContours(output, contours, -1, (0,255,0),-1)kernel = np.ones((2,1),np.uint8) #eroded = cv2.erode(output, kernel,1) output = cv2.dilate(output, kernel,1) return output
def strengthen(img):
try:
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
except Exception:
imgray = img
#ret, thresh = cv2.threshold(imgray,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
#blur1 = cv2.blur(imgray,(5,5))
blur2 = cv2.GaussianBlur(imgray,(5,5),0)
thresh2 = cv2.adaptiveThreshold(blur2, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)
kernel = np.ones((2,1),np.uint8)
#eroded = cv2.erode(thresh2, kernel,1)
#opening = cv2.morphologyEx(eroded, cv2.MORPH_CLOSE, kernel)
#closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)
return thresh2#MNIST(img)
strengthened= strengthen(img)contours = get_countour(strengthened)
print("from morphology transformation: "+ OCR(contours))
cv2.imshow(‘img’, img)
cv2.imshow(‘contour’, contours)cv2.waitKey(0)
cv2.destroyAllWindows()
The above images are recognized as:
Image 1: (CAN ajne oF
Image 2: > AMAR VRAIR
Image 3: STure
#python #opencv