Extract table structure from image containing Tables with borders

回眸只為那壹抹淺笑 提交于 2020-06-01 04:09:06

问题


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

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 ?


回答1:


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



来源:https://stackoverflow.com/questions/62092264/extract-table-structure-from-image-containing-tables-with-borders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!