表格识别效果图
获取表格
- 之前我想获取表格是通过canny+HoughLines处理的。
- 但是这里是通过腐蚀(erode)和膨胀(dilate)得到边界线条的图片。
- 然后合并在一起形成一个表格
- 根据轮廓检测函数(findContours)提取表格
- ocr文字识别
文字提取
上面用过的tesseract对于中文的识别效果不明显;
我这里改为了使用PaddleOCR
代码
import cv2
import numpy as np
from paddleocr import PaddleOCR
image = cv2.imread(r'D:/BaiduNetdiskDownload/kearsImg/bank_/test222.webp', 1)
image_copy = image.copy()
#二值化
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
binary = cv2.adaptiveThreshold(~gray, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, -10)
# cv2.imshow("cell", binary)
# cv2.waitKey(0)
rows,cols=binary.shape
scale = 20
#识别横线
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(cols//scale,1))
eroded = cv2.erode(binary,kernel,iterations = 1)
#cv2.imshow("Eroded Image",eroded)
dilatedcol = cv2.dilate(eroded,kernel,iterations = 1)
#cv2.imshow("Dilated Image",dilatedcol)
#cv2.waitKey(0)
#识别竖线
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(1,rows//scale))
eroded = cv2.erode(binary,kernel,iterations = 1)
dilatedrow = cv2.dilate(eroded,kernel,iterations = 1)
#cv2.imshow("Dilated Image",dilatedrow)
#cv2.waitKey(0)
#标识交点
bitwiseAnd = cv2.bitwise_and(dilatedcol,dilatedrow)
#cv2.imshow("bitwiseAnd Image",bitwiseAnd)
#cv2.waitKey(0)
#标识表格
merge = cv2.add(dilatedcol,dilatedrow)
#cv2.imshow("add Image",merge)
#cv2.waitKey(0)
contours, hierarchy = cv2.findContours(merge, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image, contours, -1, (0, 0, 255), 3)
cv2.imshow("findContours",image)
cv2.waitKey(0)
ocr=PaddleOCR(use_angle_cls = True,use_gpu= True) #使用CPU预加载,不用GPU
contours.reverse()
msg = []
for i in range(0,len(contours)-1):
#if cv2.contourArea(contours[i])>10:
x,y,w,h=cv2.boundingRect(contours[i])
im = image_copy[y:y + h, x:x + w]
#cv2.imshow("im", im)
#cv2.waitKey(0)
text = ocr.ocr(im, cls=True)
if(len(text)==0):
continue
text2 = ""
for t in text:
if t[1][0] == "":
continue
text2 += t[1][0]
#print(t[1][0])
msg.append(text2.replace("\n", ""))
for j in range(0,len(msg)):
print("---",msg[j])
cv2.waitKey(0)
提取效