当前位置: 首页 > news >正文

opencv提取结构化文本总结

扫描文件表格识别

1.识别结构

situation1 有明确表格结构

1.纠正表格偏移角度(获取最大轮廓,计算最小的矩形,变换坐标截取矩形)

获取面积最大轮廓

_, contours, HIERARCHY = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
candidate_table = sorted(candidate_table, key=cv2.contourArea, reverse=True) 

计算包含该轮廓的最小的矩形

rect = cv2.minAreaRect(candidate_table[0])
box = cv2.boxPoints(rect)  # box是四个点的坐标
box = np.int0(box)  # 取整
  • 去除面积不符合要求的表格
candidate_table = [cnt for cnt in contours if cv2.contourArea(cnt) > min_table_area] 
  • 去除长宽比不符合要求的表格
box_width, box_height = cv2.minAreaRect(cnt)[1][0], cv2.minAreaRect(cnt)[1][1]  # 长宽if cv2.minAreaRect(cnt)[2] > 45:box_width, box_height = box_height, box_width

坐标截取矩形

def get_sorted_rect(rect):'''获取矩阵排序的四个坐标,方便透视变换使用@param rect:@return:按照左上 右上 右下 左下排列返回'''mid_x = (max([x[1] for x in rect]) - min([x[1] for x in rect])) * 0.5 + min([x[1] for x in rect])  # 中间点坐标left_rect = [x for x in rect if x[1] < mid_x]left_rect.sort(key=lambda x: (x[0], x[1]))right_rect = [x for x in rect if x[1] > mid_x]right_rect.sort(key=lambda x: (x[0], x[1]))sorted_rect = left_rect[0], left_rect[1], right_rect[1], right_rect[0]  # 左上 右上 右下 左下return sorted_rectdef perTran(image, rect):'''做透视变换image 图像rect  四个顶点位置:左上 右上 右下 左下'''tl, tr, br, bl = rect  # 左下 右下  左上 右上 || topleft topright 左上 右上 右下 左下# 计算宽度widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))maxWidth = max(int(widthA), int(widthB))# 计算高度heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))maxHeight = max(int(heightA), int(heightB))# 定义变换后新图像的尺寸dst = np.array([[0, 0], [maxWidth - 1, 0], [maxWidth - 1, maxHeight - 1],[0, maxHeight - 1]], dtype='float32')# 变换矩阵rect = np.array(rect, dtype=np.float32)dst = np.array(dst, dtype=np.float32)M = cv2.getPerspectiveTransform(rect, dst)# 透视变换warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))return warped

2. 计算每一条横线或总线的横坐标

def drop_duplicated_row_points(pos, max_span):'''获取去重后的 坐标点Args:sort_point:max_span: Returns: ''' sort_point = np.sort(list(set(pos)))point_arr = [sort_point[0]]  # 每种类型数据max_span行都不一样for i in range(1, len(sort_point) - 1):if sort_point[i] - point_arr[-1] > max_span:point_arr.append(sort_point[i])return point_arrdef dilate_line(binary, type='vertical', x_scale=10, y_scale=5):'''获取竖线/横线腐蚀后的二值图@param binary:@param type:@return:'''rows_z, cols_z = binary.shapeif type == 'horizontal':size = (cols_z // x_scale, 1)else:size = (1, rows_z // y_scale)kernel = cv2.getStructuringElement(cv2.MORPH_RECT, size) eroded = cv2.erode(binary, kernel, iterations=1)  # 腐蚀dilated = cv2.dilate(eroded, kernel, iterations=1)  # 膨胀 return dilateddef get_page_rows_y_array(binary, row_height, x_scale=20):'''获取该图像行数 cv2.imwrite('dilated_col_z.jpg', dilated_col_z):param binary::return:''' dilated_col_z = dilate_line(binary, type='horizontal', x_scale=x_scale)ys, xs = np.where(dilated_col_z > 0)point_arr = drop_duplicated_row_points(ys, max_span=row_height)return point_arr

situation2 无明确表格结构

只有横线/纵线

1.纠正表格偏移角度( 消除除线条外的其他信息,计算横线或者纵向的起始点和坐标点,计算角度,纠正角度后,再通过坐标截取矩形 )
2. 回到situation1 第二步

计算坐标

lsd = cv2.createLineSegmentDetector(0, 1)
def get_pos_by_horizontal_line(lsd,binary ):'''获取横线的开始点和结束点'''dlines = lsd.detect(binary)[0]pos = [[x[0][0], x[0][1]] for x in dlines] + [[x[0][2], x[0][3]] for x in dlines]top_pos_list = [x for x in pos if x[0] == min([x[0] for x in pos])]  # 最小的xbottom_pos_list = [x for x in pos if x[0] == max([x[0] for x in pos])]  # 最大的xtop_pos = [x for x in top_pos_list if x[1] == min([x[1] for x in top_pos_list])][0]  # 最小的ybottom_pos = [x for x in bottom_pos_list if x[1] == max([x[1] for x in bottom_pos_list])][0]  # 最大的yx1, y1, x2, y2 = top_pos + bottom_posreturn  int(x1), int(y1), int(x2), int(y2)

计算角度


def cal_angle(x1, y1, x2, y2, is_vertical=True):if x2 - x1 == 0:result = 90elif y2 - y1 == 0:result = 0else:# 计算斜率k = -(y2 - y1) / (x2 - x1)# 求反正切,再将得到的弧度转换为度result = np.arctan(k) * 57.29577  # 逆时针if is_vertical:if result < 0:result += 90elif result == 90:result = 0else:result -= 90print("通过竖线计算得到直线倾斜角度为:{} 度".format(result))else:print("通过横线计算得到直线倾斜角度为:{} 度".format(result))result = round(result, 3)return result

纠正角度

def rotate_image( image, angle):# dividing height and width by 2 to get the center of the imageheight, width = image.shape[:2]# get the center coordinates of the image to create the 2D rotation matrixcenter = (width / 2, height / 2)# using cv2.getRotationMatrix2D() to get the rotation matrixrotate_matrix = cv2.getRotationMatrix2D(center=center, angle=angle, scale=1)# rotate the image using cv2.warpAffinerotated_image = cv2.warpAffine(src=image, M=rotate_matrix, dsize=(width, height))return rotated_image

无横线/纵线

通过扫描时设置白边处理,图片转灰度图后,非255的像素值转成0,然后回到situation1注意扫描时如果有黑边会影响计算,需要考察扫描仪选项

binary[binary <= 254] = 1
binary[binary == 255] = 0
binary[binary == 1] = 255

tips

图片预处理,通过中值滤波和色差

def clean_gray(gray,ksize=3,difference=50):gray_copy = gray.copy()gray_copy[(gray_copy >= (255-difference))] = 255gray_copy = cv2.medianBlur(gray_copy, ksize)return gray_copy

如果线条有断裂的地方,可以先膨胀再腐蚀,把断裂线条补齐

 opening = cv2.morphologyEx(binary_dilate, cv2.MORPH_OPEN, kernel, 1)

2.paddle ocr 识别文字

排序

ocr_result = ocr.ocr(padding_cell_child )
# 排序ocr_result = sorted(ocr_result, key=lambda x: (x[0][0][0]))# 横坐标排序
ocr_result = sorted(cell_result, key=lambda x: (x[0][3][1], x[0][3][0]))  # 按照y 再按照x 排序

数字识别不到

检测到数字轮廓 ,截取数字,paddle det设置为False

长文本识别不到

描述:paddle超出25个字识别可能会识别不出来

  • 改变图片比例为1:10
ratio = cell.shape[1] // cell.shape[0]
cell = cv2.resize(cell, (0, 0), fx=round(1 / (ratio / 10), 2), fy=1) 
  • 图片加padding (此处上下加5),少部分概率下会导致识别错误
cell = np.pad(cell, ((5, 5), (0, 0)), 'constant', constant_values=(255))
http://www.lryc.cn/news/20657.html

相关文章:

  • JVM知识体系学习八:OOM的案例(承接上篇博文,可以作为面试中的案例)
  • Redis的持久化方式
  • 【unity游戏制作-mango的冒险】-4.场景二的镜头和法球特效跟随
  • handwrite-1
  • 【一天一门编程语言】Pascal 语言程序设计极简教程
  • 【基础篇0】Linux下ANACONDA与TF-LITE环境配置
  • TCP协议原理二
  • 电子科技大学网络协议(TCP/IP作业答案)--网工(五次作业汇总)
  • Kubernetes集群声明式文件YAML
  • 为赋能,创共赢~ 〖TFS_CLUB社区〗-〖星荐官计划〗来袭~ 期待各位小伙伴的加入~
  • 【华为OD机试模拟题】用 C++ 实现 - 水仙花数(2023.Q1)
  • Windows作为操作系统的典型特征和主要功能
  • 【Linux】-- 多线程安全
  • Unity Avatar Camera Controller 第一、第三人称相机控制
  • SRE中 的SLO,SLI等知识 归纳
  • MS9123是一款单芯片USB投屏器,内部集成了USB2 0控制器和数据收发模块、视频DAC和音视频处理模块,MS9123可以通过USB接口显示或者扩展PC、
  • 针孔成像模型零基础入门(三)
  • 你真的了解环形队列吗?(学习数据结构必须掌握的模型)
  • 《痞子衡嵌入式半月刊》 第 72 期
  • 对redis之键值型数据库的理解
  • Linux内核中的软中断、tasklet和工作队列
  • 【Java】Spring Boot 2 集成 nacos
  • JavaSE学习笔记day14
  • LLVM高级架构介绍
  • 全网最经典函数题型【详解】——C语言
  • emqx桥接配置+常见问题解决+jmeter压测emqx
  • improve-1
  • 华为OD机试用Python实现 -【云短信平台优惠活动】(2023-Q1 新题)
  • Facebook广告投放运营中的关键成功因素是什么?
  • 2023年1月综合预订类APP用户洞察——旅游市场复苏明显,三年需求春节集中释放