我建议将每行文本作为单独的图像传递 tesseract。
由于某种原因,它似乎解决了小数点问题......
- 使用
cv2.threshold将图像从灰度转换为黑白。
- 使用具有超长水平内核的
cv2.dilate 形态学运算(跨水平方向合并块)。
- 使用查找轮廓 - 每个合并的行都将位于单独的轮廓中。
- 找到轮廓的边界框。
- 根据 y 坐标对边界框进行排序。
- 迭代边界框,并将切片传递给
pytesseract。
代码如下:
import numpy as np
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # I am using Windows
path_to_image = 'image.png'
img = cv2.imread(path_to_image, cv2.IMREAD_GRAYSCALE) # Read input image as Grayscale
# Convert to binary using automatic threshold (use cv2.THRESH_OTSU)
ret, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# Dilate thresh for uniting text areas into blocks of rows.
dilated_thresh = cv2.dilate(thresh, np.ones((3,100)))
# Find contours on dilated_thresh
cnts = cv2.findContours(dilated_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2] # Use index [-2] to be compatible to OpenCV 3 and 4
# Build a list of bounding boxes
bounding_boxes = [cv2.boundingRect(c) for c in cnts]
# Sort bounding boxes from "top to bottom"
bounding_boxes = sorted(bounding_boxes, key=lambda b: b[1])
# Iterate bounding boxes
for b in bounding_boxes:
x, y, w, h = b
if (h > 10) and (w > 10):
# Crop a slice, and inverse black and white (tesseract prefers black text).
slice = 255 - thresh[max(y-10, 0):min(y+h+10, thresh.shape[0]), max(x-10, 0):min(x+w+10, thresh.shape[1])]
text = pytesseract.image_to_string(slice, config="-c tessedit"
"_char_whitelist=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-:."
" --psm 3"
" ")
print(text)
我知道这不是最通用的解决方案,但它可以解决您发布的示例。
请将答案视为概念性解决方案 - 找到可靠的解决方案可能非常具有挑战性。
结果:
扩张后的阈值图像:
第一个切片:
第二个切片:
第三片:
输出文本:
7.3-8.2
Primo:50