python - 从图像(OCR)中读取具有不同行颜色的数字表

标签 python opencv ocr image-recognition python-tesseract

我想从一个附加的图像(PNG文件)中读取数的表。

enter image description here

我的代码如下:

import cv2
import imutils
import pytesseract
import os

image = cv2.imread(os.path.join(image_path, image_name))
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

thresh = 255 - cv2.GaussianBlur(thresh, (5,5), 0)
data = pytesseract.image_to_string(thresh, lang='eng', config='--psm 6')

print(data)

结果是:
0.74 0.73 0.72
0.72 0.71 0.71
0.71 0.70 0.70

我们可以看到,它错过,突出以蓝色的行。

我的问题是,我们可以调整我们的形象,这样我们就可以正确读取这个失踪行?

最佳答案

我能得到的只是通过调整图像2X要读取的数据。

from PIL import Image
import pytesseract

img = Image.open('PBdvo.png')
img_larger = img.resize((img.width*2,img.height*2)) 
data = pytesseract.image_to_string(img_larger)
print(data)

结果:
0.74 0.73 0.72
0.73 0.72 0.72
0.72 0.71 0.71
0.71 0.70 0.70

编辑:根据你新的,更大的图像上,我想出了以下调整:
from PIL import ImageFilter

img = Image.open('doc0m.png') 
img_larger = img.resize((round(img.width*2.5),round(img.height*2.5))) 
img_enhanced_more = img_larger.filter(ImageFilter.EDGE_ENHANCE_MORE)
data = pytesseract.image_to_string(img_enhanced_more)

然后,我注意到,数据在列返回,所以如果你想回来的,你可以做行:
for i in zip(*[column.split('\n') for column in data.split('\n\n')]): 
    print(i) 

这使:
('1.04', '1.03', '1.03', '1.02', '1.01')
('1.02', '1.01', '1.00', '1.00', '0.99')
('1.00', '0.99', '0.98', '0.97', '0.97')
('0.97', '0.97', '0.96', '0.95', '0.95')
('0.95', '0.95', '0.94', '0.93', '0.92')
('0.93', '0.92', '0.92', '0.91', '0.30')
('0.91', '0.90', '0.90', '0.89', '0.88')
('0.89', '0.88', '0.88', '0.87', '0.86')
('0.87', '0.86', '0.86', '0.85', '0.84')
('0.85', '0.84', '0.84', '0.83', '0.82')
('0.83', '0.82', '0.82', '0.81', '0.80')
('0.81', '0.80', '0.80', '0.79', '0.78')
('0.79', '0.78', '0.78', '0.77', '0.76')
('0.77', '0.76', '0.76', '0.75', '0.74')
('0.76', '0.75', '0.75', '0.74', '0.73')
('0.75', '0.74', '0.74', '0.73', '0.72')
('0.74', '0.74', '0.73', '0.72', '0.72')
('0.73', '0.73', '0.72', '0.71', '0.71')
('0.72', '0.72', '0.71', '0.70', '0.70')
('0.71', '0.71', '0.70', '0.69', '0.69')
('0.71', '0.70', '0.69', '0.69', '0.68')
('0.70', '0.69', '0.68', '0.68', '0.67')
('0.69', '0.68', '0.67', '0.67', '0.66')
('0.68', '0.67', '0.67', '0.66', '0.65')
('0.67', '0.66', '0.66', '0.65', '0.64')
('0.66', '0.65', '0.65', '0.64', '0.63')
('0.65', '0.65', '0.64', '0.63', '0.63')
('0.64', '0.64', '0.63', '0.62', '0.62')
('0.63', '0.63', '0.62', '0.62', '0.61')
('0.63', '0.62', '0.61', '0.61', '0.60')
('0.62', '0.61', '0.60', '0.60', '0.59')

关于python - 从图像(OCR)中读取具有不同行颜色的数字表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60067593/

相关文章:

python - 使用 PyArray_NewFromDescr 存储数据

c++ - 以 float 组形式访问图像像素

python - Python 装饰函数中的参数如何工作

python - try except 和编程礼仪

python - 在python opencv中捕获双眼

image - 哪里可以找到车牌图片库?

python - 我自己的 Python OCR 程序

python - read_mrz 错误 : Could not find a format to read the specified file in mode 'i'

python - Panda 的写入 CSV - 追加与写入

image - 在图像中用白色替换黑色,用白色替换黑色