从包含带有边框的表格的图像中提取表格结构 [英] Extract table structure from image containing Tables with borders

查看:106
本文介绍了从包含带有边框的表格的图像中提取表格结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取下表中的单元格位置。

I am trying to extract the cell locations in the table below.

我能够找到轮廓应用自适应阈值后,单元格位置和HoughLines会获得垂直和水平结构元素。
这是我的代码:

I was able to get the contours around the cell positions after applying adaptive thresholding and HoughLines get vertical and horizontal structuring elements. Here's my code :

img = cv2.imread(os.path.join(img_path, file))
img1 = img.copy()


gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 17, 1)
bw = cv2.bitwise_not(bw)


#detect horizontal lines
horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 1))

horizontal = cv2.erode(bw, horizontalStructure)
horizontal = cv2.dilate(horizontal, horizontalStructure)

horizontal = cv2.dilate(horizontal, (1,1), iterations=5)
horizontal = cv2.erode(horizontal, (1,1), iterations=5)


hlines = cv2.HoughLinesP(horizontal, 1, np.pi/180, 20, np.array([]), 20, 2)


for line in hlines :
    for x1,y1,x2,y2 in line:
        if abs(x1 - x2) > img.shape[1]/4:    
            cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)





#detect vertical lines
verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 15))

vertical = cv2.erode(bw, verticalStructure)
vertical = cv2.dilate(vertical, verticalStructure)

vertical = cv2.dilate(vertical, (1,1), iterations=5)
#vertical = cv2.erode(vertical, (1,1), iterations=5)


vlines = cv2.HoughLinesP(vertical, 1, np.pi/180, 20, np.array([]), 20, 2)


for line in vlines :
    for x1,y1,x2,y2 in line:
        #if abs(y1 - y2) > img.shape[0]/2:
        cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)





# red color boundaries [B, G, R]
lower = [0, 240, 0]
upper = [20, 255, 20]

# create NumPy arrays from the boundaries
lower = np.array(lower, dtype="uint8")
upper = np.array(upper, dtype="uint8")

# find the colors within the specified boundaries and apply
# the mask
mask = cv2.inRange(img, lower, upper)
output = cv2.bitwise_and(img1, img, mask=mask)



ret,thresh = cv2.threshold(mask, 40, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

img_area  = img.shape[0] * img.shape[1]

for c in contours:
    x, y, w, h = cv2.boundingRect(c)
    if w * h > 0.005 * img_area:
        cv2.rectangle(img1, (x, y), (x+w, y+h), (0, 0, 255), 2)

我该如何改进这个解决方案?为了更好,更可靠地提取表格单元格信息,我还可以实现哪些其他方法?

How can I improve this solution? What other approaches can I implement in order to extract the table cells information better and in a more robust manner ?

推荐答案

被检测到,需要更大的面积来适应任意误差阈值(以n像素宽度,例如5像素为单位),您应该能够检测到每个文本内容

for each box detected , take a wider area to get along with an arbitrary error treshold (in n pixel width, like 5 pixel), you should be able to detect every text content

这篇关于从包含带有边框的表格的图像中提取表格结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆