I'm trying to do an Arabic OCR using Tesseract, but the OCR doesn't work unless the letters are filled with black color. How do I fill the gaps after Canny edge detection?
Here is a sample image and sample code:
import tesserocr
from PIL import Image
import pytesseract
import matplotlib as plt
import cv2
import imutils
import numpy as np
image = cv2.imread(r'c:\ahmed\test3.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray,30,40,40)
#gray = cv2.GaussianBlur(gray,(1,1), 0)
gray =cv2.fastNlMeansDenoising(gray ,None, 4, 7, 21)
image = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,11,2)
k = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
blur = cv2.medianBlur(image,3)
erode = cv2.erode(blur, k)
dilat = cv2.dilate(erode,k)
cv2.imshow("gray", dilat)
#cv2.imshow("dilation", img_dilation)
#thresh = cv2.Canny(thresh, 70, 200)
#crop_img = gray[215:215+315, 783:783+684]
#cv2.imshow("cropped", crop_img)
#resize = imutils.resize(blur, width = 460)
#cv2.imshow("resize", resize)
text = pytesseract.image_to_string(dilat, lang='ara')
print(text)
with open(r"c:\ahmed\file.txt", "w", encoding="utf-8") as myfile:
myfile.write(text)
cv2.waitKey(0)
Result:
This is a sample image that won't work with neither thresholding nor Canny.