各位好!今天写这个是只是用opencv的形态学来处理表格,并获取表格中的文本。
一、表格检测与矫正
以下 Extract_Rotate.py 的函数详解:
__Duplicate_elements__
主要用来统计重复元素。
__four_point_transform__
是重写了imutils.perspective.four_point_transform方法。主要用来图片仿射的。关于仿射校正【详情点击】。
__Getting_Affine_line__
获取横线竖线线条
__Intersection__
用于计算线条的交点。关于交点计算【详情点击】。
import cv2
import math
import numpy as np
from collections import defaultdict
def __Duplicate_elements__(array,axis=0):
dd = defaultdict(list)
temp_index = []
temp_values = []
temp_length = []
Duplicates = []
for i, v in enumerate(array):
temp_index.append(i)
temp_values.append(v)
for k, val in zip(temp_values, temp_index):
dd[k[axis]].append(val)
for l in sorted(list(set([V[axis] for V in array]))):
temp_length.append((l,len(dd[l])))
temp_length = sorted(temp_length, key=lambda s: s[1], reverse=True)
for m in array:
if m[axis] in temp_length[0]:
Duplicates.append(m)
return Duplicates
def __four_point_transform__(img,locals):
docCnt = locals
widthA = np.sqrt(((locals[0][0] - locals[1][0]) ** 2) + ((locals[0][1] - locals[1][1]) ** 2))
widthB = np.sqrt(((locals[2][0] - locals[3][0]) ** 2) + ((locals[2][1] - locals[3][1]) ** 2))
maxWidth = min(int(widthA), int(widthB))+0.8*(max(int(widthA), int(widthB)) - min(int(widthA), int(widthB)))
heightA = np.sqrt(((locals[1][0] - locals[2][0]) ** 2) + ((locals[1][1] - locals[2][1]) ** 2))
heightB = np.sqrt(((locals[3][0] - locals[0][0]) ** 2) + ((locals[3][1] - locals[0][1]) ** 2))
maxHeight = int(max(int(heightA), int(heightB))*1.1)
print('docCnt:',docCnt)
pts1 = np.float32([docCnt[0], docCnt[1], docCnt[2], docCnt[3]])
pts2 = np.float32([[0, 0],[maxWidth,0],[maxWidth,maxHeight],[0,maxHeight]])
M = cv2.getPerspectiveTransform(pts1, pts2)
dst = cv2.warpPerspective(img, M, (int(maxWidth),maxHeight))
return dst
def __Getting_Affine_line__(src_img,LineSetShu, LineSet):
H_row, W_cols = src_img.shape[:2]
LineSet = sorted(LineSet, key=lambda s: s[1], reverse=False)
LineSetShu = sorted(LineSetShu, key=lambda s: s[0], reverse=False)
line_A = [A1 for A1 in LineSet if
1 < A1[0] < W_cols
and 1 < A1[2]