将文本和背景分割为OCR(Tesseract)的预处理
我正在对电视镜头中的文本应用OCR。 (我正在使用Tesseact 3.x
w/C++
) 我想分割文本和背景部分作为OCR的预处理。将文本和背景分割为OCR(Tesseract)的预处理
与通常的素材相比,文本和背景高度对比(如白色和黑色),以便修改gamma可以完成这项工作。 然而,这个附加的图像(带有橙色/红色天空背景的黄色文字)给我很难做预处理。
什么是分裂的背景这个黄色文字的好办法?
下面是使用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('./images*/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输出(见图像最后一行):
希望这有助于。
@Aki24x对上述答案有何评论? – thewaywewere
非常感谢您的回答!结果看起来不错!只是一个跟进问题,你选择“(40,180,200),(70,220,240)”作为过滤范围。你是如何决定范围的?我也尝试用编程方式过滤掉颜色范围,但它失败了。可能是因为我的颜色范围太宽。 – Aki24x
@ Aki24x很高兴收到您的好评。我通过[颜色选择器](http://colorcop.net/)手动选择颜色范围。只做了很少的尝试,然后正确地得到了结果。 – thewaywewere
您是否尝试过使用'opencv'来限制图像以提取黄色文本? – thewaywewere