Merge remote-tracking branch 'origin/fig-detection-scanned-pdfs' into fig-detection-scanned-pdfs
This commit is contained in:
commit
98ed9a4220
@ -16,8 +16,8 @@ from cv_analysis.utils.visual_logging import vizlogger
|
|||||||
from cv_analysis.layout_parsing import parse_layout
|
from cv_analysis.layout_parsing import parse_layout
|
||||||
|
|
||||||
|
|
||||||
def add_external_contours(image, img):
|
def add_external_contours(image, contour_source_image):
|
||||||
contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
|
contours, _ = cv2.findContours(contour_source_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
|
||||||
contours = filter(partial(is_large_enough, min_area=5000), contours)
|
contours = filter(partial(is_large_enough, min_area=5000), contours)
|
||||||
|
|
||||||
for cnt in contours:
|
for cnt in contours:
|
||||||
@ -27,6 +27,16 @@ def add_external_contours(image, img):
|
|||||||
return image
|
return image
|
||||||
|
|
||||||
|
|
||||||
|
def extend_lines():
|
||||||
|
#TODO
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def make_table_block_mask():
|
||||||
|
#TODO
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def apply_motion_blur(image: np.array, angle, size=80):
|
def apply_motion_blur(image: np.array, angle, size=80):
|
||||||
"""Solidifies and slightly extends detected lines.
|
"""Solidifies and slightly extends detected lines.
|
||||||
|
|
||||||
@ -68,7 +78,8 @@ def isolate_vertical_and_horizontal_components(img_bin):
|
|||||||
img_bin_h = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_h)
|
img_bin_h = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_h)
|
||||||
vizlogger.debug(img_bin_h, "tables01_isolate01_img_bin_h.png")
|
vizlogger.debug(img_bin_h, "tables01_isolate01_img_bin_h.png")
|
||||||
img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_v)
|
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_h = np.ones((1, 30), np.uint8)
|
||||||
kernel_v = np.ones((30, 1), np.uint8)
|
kernel_v = np.ones((30, 1), np.uint8)
|
||||||
@ -90,16 +101,13 @@ def isolate_vertical_and_horizontal_components(img_bin):
|
|||||||
img_bin_final = cv2.dilate(img_bin_final, np.ones((1, 1), np.uint8), iterations=1)
|
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")
|
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
|
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):
|
def has_table_shape(rects):
|
||||||
assert isinstance(rects, list)
|
assert isinstance(rects, list)
|
||||||
|
|
||||||
@ -158,10 +166,8 @@ def parse_table(image: np.array, show=False):
|
|||||||
table_layout_boxes = find_table_layout_boxes(image)
|
table_layout_boxes = find_table_layout_boxes(image)
|
||||||
|
|
||||||
image = isolate_vertical_and_horizontal_components(image)
|
image = isolate_vertical_and_horizontal_components(image)
|
||||||
image = disconnect_non_existing_cells(image, table_layout_boxes)
|
#image = add_external_contours(image, image)
|
||||||
vizlogger.debug(image, "tables12_isolate14_disconnect.png")
|
#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)
|
_, _, stats, _ = cv2.connectedComponentsWithStats(~image, connectivity=8, ltype=cv2.CV_32S)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user