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 np
img = 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 text
def 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(c)
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
You can train a GAN (Generative Adversarial Network) to do that though Image Completion:
Image Completion using Deep Convolutional Generative Adversarial Nets
https://github.com/saikatbsk/ImageCompletion-DCGAN
Example:
For more info about GANs:
GANs Presentation
your images are just results not the source right ? did you play with the blur parameters like 21, 21
blur2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur2 = cv2.GaussianBlur(blur2 , (21, 21), 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With