adjusted isolation of vertical and horizontal components to be more robust to scanned pages; work in progress
This commit is contained in:
parent
885fc22f9d
commit
c2faf7d00b
@ -10,6 +10,7 @@ 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
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def add_external_contours(image, img):
|
||||
|
||||
@ -27,12 +28,12 @@ def process_lines(img_bin_h, img_bin_v):
|
||||
def draw_lines(lines, img_bin):
|
||||
for line in lines:
|
||||
for x1, y1, x2, y2 in line:
|
||||
cv2.line(img_bin, (x1, y1), (x2, y2), (255, 255, 255), 3)
|
||||
cv2.line(img_bin, (x1, y1), (x2, y2), (255, 255, 255), 6)
|
||||
return img_bin
|
||||
lines_h = cv2.HoughLinesP(img_bin_h, 1, np.pi/180, 500, 700, 0)
|
||||
lines_h = cv2.HoughLinesP(img_bin_h, 1, np.pi / 180, 500, 500, 250)
|
||||
draw_lines(lines_h, img_bin_h)
|
||||
|
||||
lines_v = cv2.HoughLinesP(img_bin_v, 0.7, np.pi / 180, 500, 700, 0)
|
||||
lines_v = cv2.HoughLinesP(img_bin_v, 0.7, np.pi / 180, 500, 500, 250)
|
||||
draw_lines(lines_v,img_bin_v)
|
||||
|
||||
return img_bin_h, img_bin_v
|
||||
@ -46,20 +47,17 @@ def isolate_vertical_and_horizontal_components(img_bin):
|
||||
img_bin_h = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_h)
|
||||
img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_v)
|
||||
|
||||
#img_bin_h, img_bin_v = process_lines(img_bin_h,img_bin_v)
|
||||
|
||||
# lines_h = cv2.HoughLinesP(img_bin_h, 1, np.pi/180, 500)
|
||||
# for line in lines_h:
|
||||
# for x1, y1, x2, y2 in line:
|
||||
# cv2.line(img_bin_h, (x1, y1), (x2, y2), (255, 255, 255), 3)
|
||||
# lines_v = cv2.HoughLinesP(img_bin_v, 0.7, np.pi / 180, 500, 600, 0)
|
||||
# for line in lines_v:
|
||||
# for x1, y1, x2, y2 in line:
|
||||
# cv2.line(img_bin_v, (x1, y1), (x2, y2), (255, 255, 255), 3)
|
||||
|
||||
img_bin_h = cv2.dilate(img_bin_h, kernel_h, 1)
|
||||
img_bin_v = cv2.dilate(img_bin_v, kernel_v, 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_h, img_bin_v = process_lines(img_bin_h,img_bin_v)
|
||||
img_bin_final = img_bin_h | img_bin_v
|
||||
|
||||
kernel = np.ones((5, 5), np.uint8)
|
||||
# img_bin_final = cv2.dilate(img_bin_final, kernel, 2)
|
||||
th1, img_bin_final = cv2.threshold(img_bin_final, 10, 255, cv2.THRESH_BINARY)
|
||||
show_mpl(img_bin_final)
|
||||
return img_bin_final
|
||||
|
||||
|
||||
@ -99,21 +97,27 @@ def has_table_shape(rects):
|
||||
|
||||
|
||||
|
||||
def apply_motion_blur(image, size, angle):
|
||||
k = np.zeros((size, size), dtype=np.float32)
|
||||
k[ (size-1)// 2 , :] = np.ones(size, dtype=np.float32)
|
||||
k = cv2.warpAffine(k, cv2.getRotationMatrix2D( (size / 2 -0.5 , size / 2 -0.5 ) , angle, 1.0), (size, size) )
|
||||
k = k * ( 1.0 / np.sum(k) )
|
||||
return cv2.filter2D(image, -1, k)
|
||||
|
||||
|
||||
def parse_table(image: np.array):
|
||||
def is_large_enough(stat):
|
||||
x1, y1, w, h, area = stat
|
||||
# was set too high (3000): Boxes in a Table can be smaller. example: a column titled "No." This cell has approximatly an area of 500 px based on 11pt letters
|
||||
# with extra condition for the length of height and width, weirdly narrow rectangles can be filtered
|
||||
# with extra condition for the length of height and width weirdly narrow rectangles can be filtered
|
||||
return area > 500 and w > 35 and h > 15
|
||||
|
||||
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(blur_gray_scale, 195, 255, cv2.THRESH_BINARY)
|
||||
|
||||
blur_gray_scale = cv2.GaussianBlur(gray_scale, (5, 5), 1, borderType=cv2.BORDER_REPLICATE)
|
||||
th1, img_bin = cv2.threshold(blur_gray_scale, 195, 255, cv2.THRESH_BINARY)
|
||||
show_mpl(img_bin)
|
||||
# changed threshold value from 150 to 195 because of a shaded edgecase table
|
||||
th1, img_bin = cv2.threshold(gray_scale, 195, 255, cv2.THRESH_BINARY)
|
||||
# th1, img_bin = cv2.threshold(gray_scale, 195, 255, cv2.THRESH_BINARY)
|
||||
img_bin = ~img_bin
|
||||
|
||||
img_bin = isolate_vertical_and_horizontal_components(img_bin)
|
||||
@ -127,6 +131,10 @@ def parse_table(image: np.array):
|
||||
# FIXME: produces false negatives for `data0/043d551b4c4c768b899eaece4466c836.pdf 1 --type table`
|
||||
rects = list(remove_isolated(rects, input_sorted=True))
|
||||
|
||||
# if not has_table_shape(rects):
|
||||
# return False
|
||||
|
||||
|
||||
|
||||
return rects
|
||||
|
||||
@ -138,5 +146,7 @@ def annotate_tables_in_pdf(pdf_path, page_index=1):
|
||||
|
||||
stats = parse_table(page)
|
||||
page = draw_rectangles(page, stats, annotate=True)
|
||||
# if stats:
|
||||
# page = draw_rectangles(page, stats, annotate=True)
|
||||
|
||||
show_mpl(page)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user