Python 用pytesseract读取低分辨率图像

Python 用pytesseract读取低分辨率图像,python,image-processing,ocr,python-tesseract,Python,Image Processing,Ocr,Python Tesseract,我正在尝试读取pdf文件中表格裁剪(手动)部分的一些统计数据 我得到的当前结果包含了大部分数字,但不是全部文本,如下所示: 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 在调整大小的步骤中,我尝试过使用插值而不是inter-cubic,并尝试过改变内核大小,但1x1似乎效果最好 以下是当前代码: # import t

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

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

 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
在调整大小的步骤中,我尝试过使用插值而不是inter-cubic,并尝试过改变内核大小,但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)

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

我应用了
自适应阈值
+
按位非
操作,结果是:

现在,当我读到:

txt=pytesseract.image_to_字符串(bnt,config=“--psm 6”)
打印(txt)
结果:

Hewrogenedty:Chit«0.09,模具1(P=0.78);如果0.0%
总热膨胀系数的Teal:Z=1.41(P=0.16)
测试子组ditlrenote:不适用
不完美,但至少数字是正确的(如果我没弄错的话)

代码:


导入cv2
导入pytesseract
img=cv2.imread(“Q8iIo.png”)
img=cv2.调整大小(img,无,fx=2.5,fy=2.5,
插值=cv2.INTER_立方)
gry=cv2.CVT颜色(img,cv2.COLOR\u BGR2GRAY)
thr=cv2.自适应阈值(gry,255,cv2.自适应阈值平均值,
cv2.THRESH_二进制_INV,25,28)
bnt=cv2。按位_非(thr)
txt=pytesseract.image_to_字符串(bnt,config=“--psm 6”)
打印(txt)