【问题标题】:PyTesseract - Text broken by horizontal white linesPyTesseract - 被水平白线打断的文本
【发布时间】:2020-07-09 04:06:31
【问题描述】:

这是一个经典的 PyTesseract 噪声图像扫描问题。但是,在这种情况下,点阵打印机会在文本中打印一些水平白线。附上一些样品。我不确定什么样的预处理会改善文本的扫描。

使用下面的命令下面的输出来自下面的示例:

tesseract test.png stdout  --psm 6 --dpi 120

输出:(预期为“RVC 64.80%”)

PRVG
64.5056"


对于上图 pytesseract 给出了

152.00 KILOGRAW
817.51 USO

预期为 - 152.00 公斤 617.51 美元

我知道图像很嘈杂,所以请不要发布明显的答案,因为图像很嘈杂,所以输出很差。因为我总是从打印机获得相同的文本,所以我可以应用相同类型的预处理。

【问题讨论】:

  • 只能帮你去掉白线,还是不能正确扫描。

标签: python opencv python-imaging-library python-tesseract


【解决方案1】:

第一张图,句柄代码:

from PIL import Image
import numpy as np
import pytesseract
import time
from collections import Counter

img = Image.open('OCR.png').convert('L')
pixelArray = img.load()
threshold = 240
table = []
for y in range(img.size[1]): # binaryzation
    List = []
    for x in range(img.size[0]):
        if pixelArray[x,y] < threshold:
            List.append(0)
        else:
            List.append(256)
    table.append(List)

img = Image.fromarray(np.array(table))
img.show()

def operation(image):
    resultList = []
    pixelList = image.load()
    flag = False
    for y in range(image.size[1]):
        temp = []
        linePixel = 0
        for x in range(image.size[0]):
            if not pixelList[x,y]:
                linePixel += 1
            temp.append(pixelList[x,y])
        if linePixel >= 35: # judge the black dot in one line
            flag = True
            resultList.append(temp)
        elif flag:
            # resultList.append([0]*image.size[0]) # to check the handling lines
            flag = False
        else:
            resultList.append([256] * image.size[0])
    return Image.fromarray(np.array(resultList))

for i in range(6):
    img = operation(img)

img.show()
print(pytesseract.image_to_string(img,config='--psm 6'))

处理的第一个措施(二进制化):

第二个措施是去除白线(判断一行中的黑色像素):

最后,结果是:

"RVC
64.80%"

【讨论】:

  • 谢谢..但是对我来说,第一张图片的结果是 RVG 而不是 RVC.. 让我看看在哪里调整此代码,因为那里有改进.. 我可以从高层次上知道你做了什么
  • @SandeepBhutani 第一个for循环是做一些二进制处理。(去除一些嘈杂的)。函数operation()是去除白线。原理:判断一行中的黑色像素。如果它小于某些值。它不会将这些像素添加到resultList
  • 您也可以取消注释# resultList.append([0]*image.size[0]) # to check the handling lines 并添加img.show() 以查看如何处理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 2013-04-07
  • 1970-01-01
  • 2013-11-24
  • 1970-01-01
相关资源
最近更新 更多