用pytesseract读取低分辨率图像

时间:2018-11-23 23:39:40

标签: python image-processing ocr python-tesseract

我正在尝试从pdf文件中表格的裁剪部分(手动)中读取一些统计信息。

Here is the image I'm trying to process

我得到的当前结果包含大多数数字,但不是全部文本,如下所示:

 Hmuwinu'fg. cm’: -009,d1-I (F -o.761.l= .om, 
 Tamar wuall ma: 2 1.41(F-o.167
 Tao! hr aubgrwp dimes: Nol wvwe

在调整大小的步骤中,我尝试使用除三次插值之外的插值,并尝试更改内核大小,但是1x1似乎效果最好。

这是当前代码:

# import the packages
from PIL import Image
import pytesseract
import numpy as np
import argparse
import cv2
import os

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,help="path to input image to OCR'd")
ap.add_argument("-p","--preprocess",type=str,default="thresh",help="type of preprocessing to be done")
args = vars(ap.parse_args())

#load the example image 
image = cv2.imread(args["image"])

# Rescale image 
image = cv2.resize(image,None,fx=1.5,fy=1.5,interpolation=cv2.INTER_CUBIC)

#Apply dilation and erosion to remove some noise
kernel = np.ones((1,1),np.uint8)
image = cv2.dilate(image,kernel,iterations=1)
image = cv2.erode(image,kernel,iterations=1)

#Convert it to grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

# check to see if we should apply thresholding to process image
if args["preprocess"] == "thresh":
    gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

# make a check to see if median blurring should be applied
elif args["preprocess"] == "blur":
    gray = cv2.medianBlur(gray,3)

#write the gray scale image to a disk as a temp file so we can OCR it
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename,gray)

#load the image as a PIL/pillow image, apploy OCR, then delete temp file
text = pytesseract.image_to_string(Image.open(filename))
os.remove(filename)
print(text)

# show the output images
cv2.imshow("Image",image)
cv2.imshow("Output",gray)
cv2.waitKey(0)

任何建议或方法都非常感谢。

1 个答案:

答案 0 :(得分:0)

我应用了adaptive-threshold + bitwise-not个操作,结果是:

enter image description here

现在,当我阅读:

txt = pytesseract.image_to_string(bnt, config="--psm 6")
print(txt)

结果:

Hewrogenedty: Chit «0.09, die 1 (P = 0,78); If 0.0%
Teal for overall ettect: Z = 1.41 (P = 0.16)
Test tor subgroup ditlrenote: Not appliaalle

不是首府,但至少数字是正确的(如果我没记错的话)

代码:


import cv2
import pytesseract

img = cv2.imread("Q8iIo.png")
img = cv2.resize(img, None, fx=2.5, fy=2.5,
                 interpolation=cv2.INTER_CUBIC)
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                            cv2.THRESH_BINARY_INV, 25, 28)
bnt = cv2.bitwise_not(thr)
txt = pytesseract.image_to_string(bnt, config="--psm 6")
print(txt)