用python在图像中的表上创建边框
我有一个图像,其中有一个表和一些其他数据。我需要为表格绘制边框,以分隔每个单元格 我的图像是这样的 我正在尝试的是: 1) 放大图像以创建连续的斑点,如下所示 2) 寻找等高线和绘图 问题:我无法正确地绘制,因为我的表格单元格看起来太近了,在膨胀时,它们变成了一个连续的点 **我从互联网上获取了这段代码,并试图修改它,但对于这张图片效果不佳 代码:用python在图像中的表上创建边框,python,image,opencv,Python,Image,Opencv,我有一个图像,其中有一个表和一些其他数据。我需要为表格绘制边框,以分隔每个单元格 我的图像是这样的 我正在尝试的是: 1) 放大图像以创建连续的斑点,如下所示 2) 寻找等高线和绘图 问题:我无法正确地绘制,因为我的表格单元格看起来太近了,在膨胀时,它们变成了一个连续的点 **我从互联网上获取了这段代码,并试图修改它,但对于这张图片效果不佳 代码: import os import cv2 import imutils # This only works if
import os
import cv2
import imutils
# This only works if there's only one table on a page
# Important parameters:
# - morph_size
# - min_text_height_limit
# - max_text_height_limit
# - cell_threshold
# - min_columns
def pre_process_image(img, save_in_file, morph_size=(7, 7)):
# get rid of the color
pre = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Otsu threshold
pre = cv2.threshold(pre,250, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# dilate the text to make it solid spot
cpy = pre.copy()
struct = cv2.getStructuringElement(cv2.MORPH_RECT, morph_size)
cpy = cv2.dilate(~cpy, struct, anchor=(-1, -1), iterations=1)
# cpy = cv2.dilate(img,kernel,iterations = 1)
pre = ~cpy
# pre=cpy
if save_in_file is not None:
cv2.imwrite(save_in_file, pre)
return pre
def find_text_boxes(pre, min_text_height_limit=3, max_text_height_limit=30):
# Looking for the text spots contours
contours = cv2.findContours(pre, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# contours = contours[0] if imutils.is_cv2() else contours[1]
contours = contours[0]
# Getting the texts bounding boxes based on the text size assumptions
boxes = []
for contour in contours:
box = cv2.boundingRect(contour)
h = box[3]
if min_text_height_limit < h < max_text_height_limit:
boxes.append(box)
return boxes
def find_table_in_boxes(boxes, cell_threshold=10, min_columns=2):
rows = {}
cols = {}
# Clustering the bounding boxes by their positions
for box in boxes:
(x, y, w, h) = box
col_key = x // cell_threshold
row_key = y // cell_threshold
cols[row_key] = [box] if col_key not in cols else cols[col_key] + [box]
rows[row_key] = [box] if row_key not in rows else rows[row_key] + [box]
# Filtering out the clusters having less than 2 cols
table_cells = list(filter(lambda r: len(r) >= min_columns, rows.values()))
# Sorting the row cells by x coord
table_cells = [list(sorted(tb)) for tb in table_cells]
# Sorting rows by the y coord
table_cells = list(sorted(table_cells, key=lambda r: r[0][1]))
return table_cells
def build_lines(table_cells):
if table_cells is None or len(table_cells) <= 0:
return [], []
max_last_col_width_row = max(table_cells, key=lambda b: b[-1][2])
max_x = max_last_col_width_row[-1][0] + max_last_col_width_row[-1][2]
max_last_row_height_box = max(table_cells[-1], key=lambda b: b[3])
max_y = max_last_row_height_box[1] + max_last_row_height_box[3]
hor_lines = []
ver_lines = []
for box in table_cells:
x = box[0][0]
y = box[0][1]
hor_lines.append((x, y, max_x, y))
for box in table_cells[0]:
x = box[0]
y = box[1]
ver_lines.append((x, y, x, max_y))
(x, y, w, h) = table_cells[0][-1]
ver_lines.append((max_x, y, max_x, max_y))
(x, y, w, h) = table_cells[0][0]
hor_lines.append((x, max_y, max_x, max_y))
return hor_lines, ver_lines
if __name__ == "__main__":
in_file = os.path.join("data", "page1.jpg")
pre_file = os.path.join("data", "pre.png")
out_file = os.path.join("data", "out.png")
img = cv2.imread(os.path.join(in_file))
pre_processed = pre_process_image(img, pre_file)
text_boxes = find_text_boxes(pre_processed)
cells = find_table_in_boxes(text_boxes)
hor_lines, ver_lines = build_lines(cells)
# Visualize the result
vis = img.copy()
# for box in text_boxes:
# (x, y, w, h) = box
# cv2.rectangle(vis, (x, y), (x + w - 2, y + h - 2), (0, 255, 0), 1)
for line in hor_lines:
[x1, y1, x2, y2] = line
cv2.line(vis, (x1, y1), (x2, y2), (0, 0, 255), 1)
for line in ver_lines:
[x1, y1, x2, y2] = line
cv2.line(vis, (x1, y1), (x2, y2), (0, 0, 255), 1)
cv2.imwrite(out_file, vis)
导入操作系统
进口cv2
导入imutils
#这仅在页面上只有一个表时有效
#重要参数:
#-变形大小
#-最小文本高度限制
#-最大文字高度限制
#-细胞单位阈值
#-min_列
def pre_process_图像(img,将_保存在_文件中,变形_大小=(7,7)):
#去掉颜色
pre=cv2.CVT颜色(img,cv2.COLOR\U BGR2GRAY)
#大津阈值
pre=cv2.阈值(pre,250255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
#放大文本使其成为实心点
cpy=pre.copy()
struct=cv2.getStructuringElement(cv2.morp\u RECT,morp\u size)
cpy=cv2.deflate(~cpy,struct,anchor=(-1,-1),迭代次数=1)
#cpy=cv2.deflate(img,内核,迭代次数=1)
pre=~cpy
#pre=cpy
如果在_文件中保存_不是无:
cv2.imwrite(将\u保存在\u文件中,预处理)
返回前
def查找文本框(前置,最小文本高度限制=3,最大文本高度限制=30):
#寻找文本点轮廓
轮廓=cv2.找到的轮廓(前、cv2.RETR\u列表、cv2.链近似\u简单)
#等高线=等高线[0],如果imutils.is_cv2()或等高线[1]
等高线=等高线[0]
#基于文本大小假设获取文本边界框
框=[]
对于等高线中的等高线:
box=cv2.boundingRect(轮廓)
h=框[3]
如果最小文字高度限制=min_列,rows.values())
#按x坐标对行单元格进行排序
表_单元格=[表_单元格中tb的列表(已排序(tb)]]
#按y坐标排序行
table_cells=列表(已排序(table_cells,key=lambda r:r[0][1]))
返回表单元
def生成行(表格单元格):
如果table_cells是None或len(table_cells)非常有趣的应用程序
原始拨号可能不是最好的方法
我确实建议使用OCR路由。如下
输出是这样的
因此,只要有两排彼此更靠近。例如,第1-2行
在我的样本中,如果| 292-335 |<50。然后在(292+27+335)/2之间画一条线
表示位于资产线和建筑红线之间
对于OCR包,如果坚持使用python,可以尝试使用tesseract
https://pypi.org/project/pytesseract/
有关python文本坐标,请参见此处
rect包含所需的零件x y高度宽度
我在这里展示的演示实际上使用了类似于windows OCR示例的东西
https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/OCR
您可以尝试任何方法来获得所需的表格行 非常有趣的应用
原始拨号可能不是最好的方法
我确实建议使用OCR路由。如下
输出是这样的
因此,只要有两排彼此更靠近。例如,第1-2行
在我的样本中,如果| 292-335 |<50。然后在(292+27+335)/2之间画一条线
表示位于资产线和建筑红线之间
对于OCR包,如果坚持使用python,可以尝试使用tesseract
https://pypi.org/project/pytesseract/
有关python文本坐标,请参见此处
rect包含所需的零件x y高度宽度
我在这里展示的演示实际上使用了类似于windows OCR示例的东西
https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/OCR
您可以尝试任何方法来获得所需的表格行 @Yuan谢谢,看起来很棒。我会试试看,然后回来。@yuan,有没有用python实现的源代码你可以试试。然后在中间添加了我决定的样本。是的,但是它也给了我文本的坐标吗?跟着它给出了文本的YouS位置。我在我的网站上更新它post@Yuan谢谢,看起来很棒。我会试试看,然后回来。@yuan,有没有用python实现的源代码你可以试试。然后在中间添加了我决定的样本。是的,但是它也给了我文本的坐标吗?跟着它给出了文本的YouS位置。我在我的帖子里更新了它