[英]Tesseract OCR not splitting text into different result sets correctly
[英]Splitting text and background as preprocess of OCR (Tesseract)
以下是使用Python 2.7
, OpenCV 3.2.0
和Tesseract 4.0.0a
的简单解决方案。 对于OpenCV
将Python
转换为C++
应该不难,然后调用tesseract API
执行OCR。
import numpy as np
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
def show(title, img, color=True):
if color:
plt.imshow(img[:,:,::-1]), plt.title(title), plt.show()
else:
plt.imshow(img, cmap='gray'), plt.title(title), plt.show()
def ocr(img):
# I used a version of OpenCV with Tesseract binding. Modes set to:
# Page Segmentation mode (PSmode) = 11 (defualt = 3)
# OCR Enginer Mode (OEM) = 3 (defualt = 3)
tesser = cv2.text.OCRTesseract_create('C:/Program Files/Tesseract 4.0.0/tessdata/','eng', \
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',3,3)
retval = tesser.run(img, 0) # return text string type
print 'OCR Output: ' + retval
img = cv2.imread('./imagesStackoverflow/yellow_text.png')
show('original', img)
# apply GaussianBlur to smooth image, then threshholds yellow to white (255,255, 255)
# and sets the rest to black(0,0,0)
img = cv2.GaussianBlur(img,(5,5), 1) # smooth image
mask = cv2.inRange(img,(40,180,200),(70,220,240)) # filter out yellow color range, low and high range
show('mask', mask, False)
# invert the image to have text black-in-white
res = 255 - mask
show('result', res, False)
# pass to tesseract to perform OCR
ocr(res)
已处理的图像和OCR输出(请参阅图像的最后一行):
希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.