拆分文本和背景作为OCR的预处理(Tesseract)

时间:2017-04-25 21:23:26

标签: c++ image-processing ocr tesseract

我正在对电视画面中的文字应用OCR。 (我正在使用Tesseact 3.x w / C++) 我试图将文本和背景部分拆分为OCR的预处理。

对于通常的镜头,文本和背景是高度对比的(例如白色与黑色),因此修改伽玛可以完成工作。 但是,这张附加图像(带有橙色/红色天空背景的黄色文字)让我很难进行预处理。

Yellow-text over orange sky

将这个黄色文字从背景中分割出来的好方法是什么?

1 个答案:

答案 0 :(得分:2)

以下是使用Python 2.7OpenCV 3.2.0Tesseract 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('./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输出(参见图像中的最后一行):

neo4j Python Driver

希望得到这个帮助。