拆分文本和背景作为 OCR (Tesseract) 的预处理
Splitting text and background as preprocess of OCR (Tesseract)
我正在对电视镜头中的文本应用 OCR。 (我正在使用 Tesseact 3.x
w/ C++
)
我正在尝试拆分文本和背景部分作为 OCR 的预处理。
对于通常的镜头,文本和背景的对比度很高(例如白色与黑色对比),因此修改伽马就可以完成这项工作。
但是,这张附加图片(背景为 orange/red 天空的黄色文本)让我很难进行预处理。
从背景中分离黄色文本的好方法是什么?
下面是使用 Python 2.7
、OpenCV 3.2.0
和 Tesseract 4.0.0a
的简单解决方案。将Python
转换为C++
对于OpenCV
应该不难,然后调用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('./imagesWhosebug/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 输出(参见图像中的最后一行):
希望对您有所帮助。
我正在对电视镜头中的文本应用 OCR。 (我正在使用 Tesseact 3.x
w/ C++
)
我正在尝试拆分文本和背景部分作为 OCR 的预处理。
对于通常的镜头,文本和背景的对比度很高(例如白色与黑色对比),因此修改伽马就可以完成这项工作。 但是,这张附加图片(背景为 orange/red 天空的黄色文本)让我很难进行预处理。
从背景中分离黄色文本的好方法是什么?
下面是使用 Python 2.7
、OpenCV 3.2.0
和 Tesseract 4.0.0a
的简单解决方案。将Python
转换为C++
对于OpenCV
应该不难,然后调用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('./imagesWhosebug/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 输出(参见图像中的最后一行):
希望对您有所帮助。