diff --git a/cv_analysis/table_parsing.py b/cv_analysis/table_parsing.py index d2bd1c3..0a6ceed 100644 --- a/cv_analysis/table_parsing.py +++ b/cv_analysis/table_parsing.py @@ -16,8 +16,8 @@ from cv_analysis.utils.visual_logging import vizlogger from cv_analysis.layout_parsing import parse_layout -def add_external_contours(image, img): - contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) +def add_external_contours(image, contour_source_image): + contours, _ = cv2.findContours(contour_source_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) contours = filter(partial(is_large_enough, min_area=5000), contours) for cnt in contours: @@ -27,6 +27,16 @@ def add_external_contours(image, img): return image +def extend_lines(): + #TODO + pass + + +def make_table_block_mask(): + #TODO + pass + + def apply_motion_blur(image: np.array, angle, size=80): """Solidifies and slightly extends detected lines. @@ -68,8 +78,9 @@ def isolate_vertical_and_horizontal_components(img_bin): img_bin_h = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_h) vizlogger.debug(img_bin_h, "tables01_isolate01_img_bin_h.png") img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_v) - vizlogger.debug(img_bin_v | img_bin_h, "tables02_isolate02_img_bin_v.png") - + img_lines_raw = img_bin_v | img_bin_h + vizlogger.debug(img_lines_raw, "tables02_isolate02_img_bin_v.png") + 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) @@ -89,17 +100,14 @@ def isolate_vertical_and_horizontal_components(img_bin): vizlogger.debug(img_bin_final, "tables10_isolate12_threshold.png") img_bin_final = cv2.dilate(img_bin_final, np.ones((1, 1), np.uint8), iterations=1) vizlogger.debug(img_bin_final, "tables11_isolate13_dilate.png") + + # add contours before lines are extended by blurring + img_bin_final = add_external_contours(img_bin_final, img_lines_raw) + vizlogger.debug(img_bin_final, "tables11_isolate14_contours_added.png") 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 - - def has_table_shape(rects): assert isinstance(rects, list) @@ -158,10 +166,8 @@ def parse_table(image: np.array, show=False): table_layout_boxes = find_table_layout_boxes(image) image = isolate_vertical_and_horizontal_components(image) - image = disconnect_non_existing_cells(image, table_layout_boxes) - vizlogger.debug(image, "tables12_isolate14_disconnect.png") - image = add_external_contours(image, image) - vizlogger.debug(image, "external_contours_added.png") + #image = add_external_contours(image, image) + #vizlogger.debug(image, "external_contours_added.png") _, _, stats, _ = cv2.connectedComponentsWithStats(~image, connectivity=8, ltype=cv2.CV_32S)