diff --git a/vidocp/table_parsing.py b/vidocp/table_parsing.py index 2301ac1..dd65cd2 100644 --- a/vidocp/table_parsing.py +++ b/vidocp/table_parsing.py @@ -9,6 +9,7 @@ from pdf2image import pdf2image from vidocp.utils.display import show_mpl from vidocp.utils.draw import draw_rectangles from vidocp.utils.post_processing import xywh_to_vecs, xywh_to_vec_rect, adjacent1d, remove_isolated +from vidocp.layout_parsing import parse_layout def add_external_contours(image, img): @@ -23,50 +24,8 @@ def add_external_contours(image, img): return image -def process_lines(img_line_component): - def draw_lines(detected_lines, img_bin): - for line in detected_lines: - for x1, y1, x2, y2 in line: - cv2.line(img_bin, (x1, y1), (x2, y2), (255, 255, 255), 6) - return img_bin - - lines = cv2.HoughLines(img_line_component, 1, np.pi / 180, 500) - draw_lines(lines, lines) - - return img_line_component - -# def isolate_vertical_and_horizontal_components(img_bin): -# line_min_width = 50 -# kernel_h = np.ones((1, line_min_width), np.uint8) -# kernel_v = np.ones((line_min_width, 1), np.uint8) -# -# img_bin_h = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_h) -# img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_v) -# show_mpl(img_bin_h | img_bin_v) -# -# img_bin_h = apply_motion_blur(img_bin_h, 140, 0) -# img_bin_v = apply_motion_blur(img_bin_v, 140, 90) -# show_mpl(img_bin_h | img_bin_v) -# -# th1, img_bin_h = cv2.threshold(img_bin_h, 95, 255, cv2.THRESH_BINARY) -# th1, img_bin_v = cv2.threshold(img_bin_v, 95, 255, cv2.THRESH_BINARY) -# show_mpl(img_bin_h | img_bin_v) -# -# kernel_h = np.ones((1, 8), np.uint8) -# kernel_v = np.ones((8, 1), np.uint8) -# img_bin_h = cv2.dilate(img_bin_h, kernel_h, iterations=4) -# img_bin_v = cv2.dilate(img_bin_v, kernel_v, iterations=4) -# -# img_bin_final = img_bin_h | img_bin_v -# show_mpl(img_bin_final) -# # th 130 -# #th1, img_bin_final = cv2.threshold(img_bin_final, 90, 255, cv2.THRESH_BINARY) -# #show_mpl(img_bin_final) -# return img_bin_final - - -def isolate_vertical_and_horizontal_components(img_bin): - line_min_width = 30 +def isolate_vertical_and_horizontal_components(img_bin, bounding_rects): + line_min_width = 47 kernel_h = np.ones((1, line_min_width), np.uint8) kernel_v = np.ones((line_min_width, 1), np.uint8) @@ -74,51 +33,32 @@ def isolate_vertical_and_horizontal_components(img_bin): img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_v) show_mpl(img_bin_h | img_bin_v) - img_bin_h = apply_motion_blur(img_bin_h, 150, 0) - img_bin_v = apply_motion_blur(img_bin_v, 150, 90) + kernel_h = np.ones((1, 30), np.uint8) + kernel_v = np.ones((30, 1), np.uint8) + img_bin_h = cv2.dilate(img_bin_h, kernel_h, iterations=2) + img_bin_v = cv2.dilate(img_bin_v, kernel_v, iterations=2) show_mpl(img_bin_h | img_bin_v) - th1, img_bin_h = cv2.threshold(img_bin_h, 70, 255, cv2.THRESH_BINARY) - th1, img_bin_v = cv2.threshold(img_bin_v, 70, 255, cv2.THRESH_BINARY) - show_mpl(img_bin_h | img_bin_v) - - kernel_h = np.ones((1, 10), np.uint8) - kernel_v = np.ones((10, 1), np.uint8) - img_bin_h = cv2.erode(img_bin_h, kernel_h, iterations=1) - img_bin_v = cv2.erode(img_bin_v, kernel_v, iterations=1) + img_bin_h = apply_motion_blur(img_bin_h, 100, 0) + img_bin_v = apply_motion_blur(img_bin_v, 100, 90) img_bin_final = img_bin_h | img_bin_v show_mpl(img_bin_final) - # th 130 - # th1, img_bin_final = cv2.threshold(img_bin_final, 150, 255, cv2.THRESH_BINARY) - # show_mpl(img_bin_final) + + th1, img_bin_final = cv2.threshold(img_bin_final, 110, 255, cv2.THRESH_BINARY) + img_bin_final = cv2.dilate(img_bin_final, np.ones((1, 1), np.uint8), iterations=1) + show_mpl(img_bin_final) + img_bin_final = disconnect_non_existing_cells(img_bin_final, bounding_rects) + show_mpl(img_bin_final) + return img_bin_final -# def isolate_vertical_and_horizontal_components(img_bin): -# line_min_width = 30 -# kernel_h = np.ones((1, line_min_width), np.uint8) -# kernel_v = np.ones((line_min_width, 1), np.uint8) -# -# img_bin_h = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_h) -# img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_v) -# show_mpl(img_bin_h | img_bin_v) -# -# kernel_h = np.ones((1, 30), np.uint8) -# kernel_v = np.ones((30, 1), np.uint8) -# img_bin_h = cv2.dilate(img_bin_h, kernel_h, iterations=1) -# img_bin_v = cv2.dilate(img_bin_v, kernel_v, iterations=1) -# show_mpl(img_bin_h | img_bin_v) -# -# img_bin_h = apply_motion_blur(img_bin_h, 100, 0) -# img_bin_v = apply_motion_blur(img_bin_v, 100, 90) -# -# img_bin_final = img_bin_h | img_bin_v -# show_mpl(img_bin_final) -# # th 130 -# th1, img_bin_final = cv2.threshold(img_bin_final, 125, 255, cv2.THRESH_BINARY) -# show_mpl(img_bin_final) -# -# return img_bin_final + +def disconnect_non_existing_cells(img_bin, bounding_rects): + for rect in bounding_rects: + x, y, w, h = rect + img_bin = cv2.rectangle(img_bin, (x, y), (x + w, y + h), (0, 0, 0), 5) + return img_bin # FIXME: does not work yet @@ -163,18 +103,28 @@ def apply_motion_blur(image, size, angle): return cv2.filter2D(image, -1, k) +def find_table_layout_boxes(image: np.array): + layout_boxes = parse_layout(image) + table_boxes = [] + for box in layout_boxes: + (x, y, w, h) = box + if w * h >= 300000: + table_boxes.append(box) + return table_boxes + + def parse_table(image: np.array): def is_large_enough(stat): x1, y1, w, h, area = stat - return area > 500 and w > 35 and h > 15 + return area > 2000 and w > 35 and h > 25 gray_scale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) - # blur_gray_scale = cv2.GaussianBlur(gray_scale, (5, 5), 1, borderType=cv2.BORDER_REPLICATE) th1, img_bin = cv2.threshold(gray_scale, 195, 255, cv2.THRESH_BINARY) img_bin = ~img_bin show_mpl(img_bin) - img_bin = isolate_vertical_and_horizontal_components(img_bin) + table_layout_boxes = find_table_layout_boxes(image) + img_bin = isolate_vertical_and_horizontal_components(img_bin, table_layout_boxes) img_bin_final = add_external_contours(img_bin, img_bin) _, _, stats, _ = cv2.connectedComponentsWithStats(~img_bin_final, connectivity=8, ltype=cv2.CV_32S) diff --git a/vidocp/utils/post_processing.py b/vidocp/utils/post_processing.py index 6cc9452..02018c3 100644 --- a/vidocp/utils/post_processing.py +++ b/vidocp/utils/post_processing.py @@ -30,7 +30,7 @@ def remove_included(rectangles): return rectangles #tolerance was set too low (1) most lines are 2px wide -def adjacent1d(n, m, tolerance=2): +def adjacent1d(n, m, tolerance=4): return abs(n - m) <= tolerance