opencv 如何从包含表格数据的图像中提取数据?

wljmcqd8  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(112)

我正在使用pytesseract,pillow,cv 2来OCR图像并获取图像中的文本。由于我的输入是扫描的PDF文档,我首先将其转换为图像(JPEG)格式,然后尝试提取文本。我只完成了一半。输入是一个表格,标题没有显示,因为标题有一个黑色的背景。我也试过getstructuringelement,但无法找出一个方法。这是我所做的,直到现在-

import cv2
import os  
import numpy as np 
import pytesseract
#import pillow 

#Since scanned PDF can't be handled by pdf2image, convert the scanned PDF into a JPEG format using the below code- 
filename = path   
from pdf2image import convert_from_path 
pages = convert_from_path(filename, 500) for page in pages:
page.save("dest", 'JPEG')

imgname = "path" 
oriimg = cv2.imread(imgname,cv2.IMREAD_COLOR) 
cv2.imshow("original image", oriimg)
cv2.waitKey(0)

#img = cv2.resize(oriimg,None,fx=0.5,fy=0.5,interpolation=cv2.INTER_CUBIC) 
img = cv2.resize(oriimg,(700,1500),interpolation=cv2.INTER_AREA) 
#here length height  
cv2.imshow("lol", img) 
cv2.waitKey(0) 
cv2.imwrite("changed_dimensionsimgpath", img)

import PIL.Image  
image = cv2.imread(imgname,cv2.IMREAD_COLOR) 
grayedimg = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayedimg = 
cv2.threshold(grayedimg, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] 
cv2.imwrite("H://newim.jpg", grayedimg)

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

text = pytesseract.image_to_string(PIL.Image.open("path"))
print(text)

我的输入表如下所示。黑色背景的区域没有被OCR识别,也没有被提取为文本。任何帮助都将非常感谢。x1c 0d1x
输出此代码的图像样本-

Sun by Select .

F'I‘L‘Mlm":[ [Juir SHIIEF'. ”fillfit Fadll'fi


Brand Type Fragranm Unit: Ithange Dollm 'LChanga Men
Eleanit' Sprayl Grange J.?IEBflI-Efl' 11% '5H'1Elfi9flflfl 35% I E
Eleanlt! kfimnsul' Grange IEEEESWI 39% I521LESM1MH 1113553 ‘ E
Dehuxe F‘mmr [emu 525.940 461% '51:EE?,GED,00 433.6% 5
Datum: Anus»! firing?) 4,3341%} 29% 513573300119 215% E
Dem Spray ‘Drangr: £432,100 09% 515.223.:53000 154%

Min Blaster Aemgul: Dramge ”2114033111 59% :SHSiMMfl H94:

DiFlEIESIEf Sprawl Drama “NEW. 50% ‘5E1D1_E-BDM 141% I
Incredlme Spray Lem 1.513.410" 483% a HELENE] $11143 I E

t“ In

1'"
kknvjkwl

kknvjkwl1#

在cv2.imwrite(temp_filename,gray_img)之后使用cv2是好的

import PIL.Image  
Use config='-psm 6'
page_str = image_to_string(Image.open(temp_filename), lang="eng", config='-psm 6')

这将从表图像返回良好的数据

相关问题