diff --git a/vidocp/table_parsing.py b/vidocp/table_parsing.py index c43a457..e05d78e 100644 --- a/vidocp/table_parsing.py +++ b/vidocp/table_parsing.py @@ -5,6 +5,7 @@ from operator import attrgetter import cv2 import numpy as np from pdf2image import pdf2image +from scipy import ndimage from vidocp.utils.display import show_mpl from vidocp.utils.draw import draw_rectangles @@ -23,6 +24,69 @@ def add_external_contours(image, img): return image +# def rotate_line_components(img_bin_h, img_bin_v): +# def get_avg_angle(img_bin): +# edges = cv2.Canny(img_bin, 50, 150, apertureSize=3) +# lines = cv2.HoughLines(edges, 1, np.pi / 180, 200) +# angles_deg = [] +# +# for line in lines: +# line = line[0] +# rho, theta = line +# angles_deg.append(180/np.pi * theta) +# +# avg_ang_deg = sum(angles_deg) / len(angles_deg) +# return average_angle_in_deg +# avg_ang_h = get_avg_angle(img_bin_h) +# avg_ang_v = get_avg_angle(img_bin_v) +# print(average_angle_in_deg, "angle before angle correction") +# # average_angle_in_deg = 90 - average_angle_in_deg +# # print(average_angle_in_deg) +# if average_angle_in_deg == 0.0 or average_angle_in_deg == 90.0: +# print("angle is 0 or 90") +# return img_bin +# elif average_angle_in_deg > 0: #linksrtoation im originalbild +# print("leftrot angle is not 0 or 90") +# average_angle_in_deg = 360 - average_angle_in_deg +# else: +# print("rightrot", average_angle_in_deg) +# print(average_angle_in_deg, "angle for Rotationsmatrix") +# img_bin_rotated = ndimage.rotate(img_bin, average_angle_in_deg, reshape=False) +# show_mpl(img_bin_rotated) +# +# return img_bin + +# def rotate_line_components(img_bin): +# height, width = img_bin.shape[:2] +# center = (width/2, height/2) +# edges = cv2.Canny(img_bin, 50, 150, apertureSize=3) +# lines = cv2.HoughLines(edges, 1, np.pi / 180, 200) +# angles_deg = [] +# +# for line in lines: +# line = line[0] +# rho, theta = line +# angles_deg.append(180/np.pi * theta) +# #linksdrehung < 90 rechtsdrehung > 90 +# average_angle_in_deg = sum(angles_deg) / len(angles_deg) +# print(average_angle_in_deg, "angle before angle correction") +# average_angle_in_deg = 90 - average_angle_in_deg +# print(average_angle_in_deg) +# if average_angle_in_deg == 0.0 or average_angle_in_deg == 90.0: +# print("angle is 0 or 90") +# return img_bin +# elif average_angle_in_deg > 0: #linksrtoation im originalbild +# print("leftrot angle is not 0 or 90") +# average_angle_in_deg = 360 - average_angle_in_deg +# else: +# print("rightrot", average_angle_in_deg) +# print(average_angle_in_deg, "angle for Rotationsmatrix") +# img_bin_rotated = ndimage.rotate(img_bin, average_angle_in_deg, reshape=False) +# show_mpl(img_bin_rotated) +# +# return img_bin + + def isolate_vertical_and_horizontal_components(img_bin, bounding_rects): line_min_width = 48 @@ -33,25 +97,31 @@ def isolate_vertical_and_horizontal_components(img_bin, bounding_rects): img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_v) show_mpl(img_bin_h | img_bin_v) + # img_bin_h = rotate_line_components(img_bin_h) + # img_bin_v = rotate_line_components(img_bin_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=2) img_bin_v = cv2.dilate(img_bin_v, kernel_v, iterations=2) - show_mpl(img_bin_h | img_bin_v) + #show_mpl(img_bin_h | img_bin_v) #reduced filtersize from 100 to 80 to minimize splitting narrow cells - img_bin_h = apply_motion_blur(img_bin_h, 80, 0) - img_bin_v = apply_motion_blur(img_bin_v, 80, 90) + img_bin_h = apply_motion_blur(img_bin_h, 90, 0) + img_bin_v = apply_motion_blur(img_bin_v, 90, 90) img_bin_final = img_bin_h | img_bin_v - show_mpl(img_bin_final) + #show_mpl(img_bin_final) + #changed threshold from 110 to 120 to minimize cell splitting th1, img_bin_final = cv2.threshold(img_bin_final, 120, 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) + #show_mpl(img_bin_final) + # problem if layout parser detects too big of a layout box as in VV-748542.pdf p.22 img_bin_final = disconnect_non_existing_cells(img_bin_final, bounding_rects) - show_mpl(img_bin_final) + #show_mpl(img_bin_final) return img_bin_final diff --git a/vidocp/utils/post_processing.py b/vidocp/utils/post_processing.py index a3a04b1..bf10793 100644 --- a/vidocp/utils/post_processing.py +++ b/vidocp/utils/post_processing.py @@ -43,7 +43,7 @@ def adjacent(a, b): """ def adjacent2d(g, h, i, j, k, l): - #print(adjacent1d(g, h) and any(k <= p <= l for p in [i, j])) + #print(adjacent1d(g, h), any(k <= p <= l for p in [i, j])) return adjacent1d(g, h) and any(k <= p <= l for p in [i, j]) if any(x is None for x in (a, b)): @@ -79,7 +79,7 @@ def __remove_isolated_unsorted(rectangles): def __remove_isolated_sorted(rectangles): def is_connected(left, center, right): - # print(left,center,right, list(starmap(adjacent, [(left, center), (center, right)]))) + #print(left,center,right) return any(starmap(adjacent, [(left, center), (center, right)])) rectangles = list(map(xywh_to_vec_rect, rectangles))