问题描述
我正在尝试识别下图底部左上角代表时间的数字。
具体地说,这是我需要识别的图像:
数字采用七个小节的格式。
我正在使用opencv和tesseract,但是我使用的任何过滤器都无法获得良好的结果。
这是开发的代码(我假设已经确定了数字所在的部分,所以这是我的起点):
image = cv2.imread("C:\example2.jpg")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray,(5,5),0)
edged = cv2.Canny(blurred,50,200,255)
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
text = pytesseract.image_to_string(edged)
尝试使用另一组滤镜,但没有一个滤镜能提供接近图像的结果。
谢谢。
解决方法
Tesseract喜欢白色背景上的黑色文本。另外,它喜欢characters to be a minimum height。
我通过了cv2.THRESH_OTSU
来进行Otsu thresholding和cv2.THRESH_BINARY_INV
来反转图像(将黑变白,白变黑)。以原始大小,“ 9”被识别为“ q”。我将尺寸调整为2倍,识别度更高。
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
img = cv2.imread('example2.jpg',cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1]
thresh = cv2.resize(thresh,(0,0),fx=2,fy=2) # scale image 2X
detected_text = pytesseract.image_to_string(thresh,config = '--psm 6')
print(detected_text)
给出
9:47