Before applying OCR onto the image, you need to preprocess the image. A simple preprocessing approach is to enlarge the image, obtain a binary image using Otsu's threshold, perform morphological operations, then OCR the image.
Enlarge, Gaussian blur, and Otsu's threshold
Morph open
Morph close
Invert, apply slight blur, and OCR
Result from Pytesseract OCR image_to_string
using the --psm 6
configuration option to treat the image as a single block of text.
xc2kc2
Code
import cv2
import pytesseract
import imutils
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
# Resize, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread('1.jpg')
image = imutils.resize(image, width=400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Perform morphological operations
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
close = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=3)
# Invert, Blur, and perform text extraction
invert = 255 - cv2.GaussianBlur(close, (3,3), 0)
data = pytesseract.image_to_string(invert, lang='eng',config='--psm 6')
print(data)
cv2.imshow('thresh', thresh)
cv2.imshow('opening', opening)
cv2.imshow('close', close)
cv2.imshow('invert', invert)
cv2.waitKey()