【发布时间】:2016-06-26 07:03:38
【问题描述】:
这是我要处理的原始图像的链接:-
使用 opencv2 处理图像后,我得到以下结果:-
但即使使用上面的图像,Tesseract 也无法识别图像中的字符。这发生在许多与上述示例具有相同样式的图像中。
欢迎任何关于如何提高图像质量或使用其他 Tesseract 模式的建议。
此外,如果上述技术不起作用,请提出替代方案,例如训练 Tesseract 或使用其他 OCR 或方法?
谢谢
编辑:我也包含代码
# Read the image
im = cv2.imread("image.jpg")
# Convert to grayscale and apply Gaussian filtering
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
im_gray = cv2.GaussianBlur(im_gray, (5, 5), 0)
# Threshold the image
ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)
# Find contours in the image
ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Get rectangles contains each contour
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
for rect in rects:
# Only consider rects which are bigger than a certain area
if rect[3]*rect[2] > 300:
# Draw the rectangles
cv2.rectangle(im, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3)
# Make the rectangular region around the digit
leng = int(rect[3] * 1.6)
pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
pt2 = int(rect[0] + rect[2] // 2 - leng // 2)
if pt2 < 0:
pt2 = rect[0] + rect[2]
roi = im_th[pt1:pt1+leng, pt2:pt2+leng]
# Invert the image such that the text is black and background is white
roi = (255-roi)
# roi is the final processed image
try:
cv2.imwrite("test.jpg", roi)
# call the terminal command: tesseract test.jpg out -psm 10
call(["tesseract", "test.jpg", "out", "-psm", "10"])
file = open('out.txt', 'rb+')
text = file.read()
file.close()
if text:
print text
except:
pass
【问题讨论】:
-
我已经包含了代码..