使用Pytesseract OCR识别表格图像中的特定数字 [英] Recognize specific numbers from table image with Pytesseract OCR

查看:600
本文介绍了使用Pytesseract OCR识别表格图像中的特定数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从附加的图像(png文件)中读取一列数字.

I want to read a column of number from an attached image (png file).

我的代码是

import cv2
import pytesseract
import os

img = cv2.imread(os.path.join(image_path, image_name), 0)
config= "-c 
        tessedit_char_whitelist=01234567890.:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

pytesseract.image_to_string(img, config=config)

这段代码为我提供了输出字符串:"n113 \ nun \ n1.08".如我们所见,存在两个问题:

This code gives me the output string: 'n113\nun\n1.08'. As we can see, there are two problems:

  1. 无法识别1.13中的小数点(请参见图片).
  2. 它完全无法读取1.11(请参阅附图).它只是返回"nun".

这些问题的解决方案是什么?

What is a solution to these problems?

最佳

推荐答案

您需要预处理图像.一种简单的方法是使用Otsu的阈值调整图像大小,转换为灰度并获得二进制图像.在这里,我们可以应用轻微的高斯模糊,然后反转图像,以便提取的所需文本为白色,背景为黑色.这是准备好用于OCR的已处理图像

You need to preprocess the image. A simple approach is to resize the image, convert to grayscale, and obtain a binary image using Otsu's threshold. From here we can apply a slight gaussian blur then invert the image so the desired text to extract is in white with the background in black. Here's the processed image ready for OCR

OCR的结果

1.13
1.11
1.08

代码

import cv2
import pytesseract
import imutils

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

# Resize, grayscale, Otsu's threshold
image = cv2.imread('1.png')
image = imutils.resize(image, width=400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Blur and perform text extraction
thresh = 255 - cv2.GaussianBlur(thresh, (5,5), 0)
data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
print(data)

cv2.imshow('thresh', thresh)
cv2.waitKey()

这篇关于使用Pytesseract OCR识别表格图像中的特定数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆