【问题标题】:Read a table of number from image (OCR) with different row colour从具有不同行颜色的图像(OCR)中读取数字表
【发布时间】:2020-02-05 00:51:32
【问题描述】:

我想从附加图像(png 文件)中读取数字表。

我的代码如下:

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

如我们所见,它错过了以蓝色突出显示的行。

我的问题是我们可以调整我们的图像,以便我们可以正确读取这个缺失的行吗?

【问题讨论】:

  • 为什么你在做阈值处理后会变得模糊?您的图像足够干净,根本不需要模糊,如果是这样,通常在阈值处理之前完成以清除嘈杂的图像。

标签: python opencv ocr image-recognition python-tesseract


【解决方案1】:

我只需将图像大小调整为 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')

【讨论】:

  • 又好又简单!
  • @Vorsprung durch Technik 不幸的是,这种调整图像大小的方法不适用于更大的表格图像(例如,60 行 x 5 列而不是示例中的 4 行 x 3 列)。有什么建议吗?
  • @Vorsprung durch Technik 对此有何想法?
  • @MollyBFL:我验证了使用我的新代码正确读取了所有数据。
  • @Vorsprung durch Technik 这很奇怪。当我运行您的代码时,我没有得到相同的结果。它无法正确读取。它给出了类似“'1,04','Loo','6.57','G81',,......等”的东西。您使用哪个版本的 python 和 PIL?我使用 python 3.7.3 和在 jupyter 实验室(窗口 10)上运行的 PIL 1.1.7 版
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-22
  • 2022-12-04
  • 2020-04-01
  • 1970-01-01
  • 2021-12-08
  • 2018-08-16
  • 2016-07-07
相关资源
最近更新 更多