簡體   English   中英

拆分文本和背景作為OCR的預處理(Tesseract)

[英]Splitting text and background as preprocess of OCR (Tesseract)

我正在對電視畫面中的文本應用OCR。 (我正在使用帶C++ Tesseact 3.x )我試圖將文本和背景部分拆分為OCR的預處理。

與通常的素材相比,文本和背景形成了鮮明的對比(例如白色對黑色),因此修改gamma即可勝任。 但是,此附加圖像(帶有橙色/紅色天空背景的黃色文本)使我很難進行預處理。

黃色文本在橙色的天空

將黃色文本與背景分開的好方法是什么?

以下是使用Python 2.7OpenCV 3.2.0Tesseract 4.0.0a的簡單解決方案。 對於OpenCVPython轉換為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM