corrected function for detecting external edges

This commit is contained in:
llocarnini 2022-02-02 16:35:35 +01:00
parent 5abc80aced
commit cf29204a9e

View File

@ -18,7 +18,7 @@ def parse(image: np.array):
img_bin_h = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_h)
img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernel_v)
# print([cv2.countNonZero(row) for row in img_bin_v])
#print(np.nonzero(img_bin_v))
img_bin_final = img_bin_h | img_bin_v
@ -79,43 +79,70 @@ def annotate_image(image, stats):
return image
# def find_and_close_edges(img_bin_final):
# contours, hierarchy = cv2.findContours(img_bin_final, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#
# for cnt in contours:
# missing_external_edges = True
# left = tuple(cnt[cnt[:, :, 0].argmin()][0])
# right = tuple(cnt[cnt[:, :, 0].argmax()][0])
# top = tuple(cnt[cnt[:, :, 1].argmin()][0])
# bottom = tuple(cnt[cnt[:, :, 1].argmax()][0])
# topleft = [left[0] + 1, top[1]]
# # print(cnt, left, top, topleft)
# bottomright = [right[0] - 1, bottom[1]]
# for arr in cnt:
# if np.array_equal(arr, np.array([bottomright])) or np.array_equal(arr, np.array([topleft])):
# missing_external_edges = False
# break
#
# if missing_external_edges and (bottomright[0]-topleft[0])*(bottomright[1]-topleft[1]) >= 50000:
# topleft[0] -= 1
# bottomright[0] += 1
# cv2.rectangle(img_bin_final, tuple(topleft), tuple(bottomright), (255,255,255) , 2)
# #print("missing cell detectet rectangle drawn")
#
# return img_bin_final
def find_and_close_edges(img_bin_final):
contours, hierarchy = cv2.findContours(img_bin_final, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours, hierarchy = cv2.findContours(img_bin_final, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
missing_external_edges = True
left = tuple(cnt[cnt[:, :, 0].argmin()][0])
right = tuple(cnt[cnt[:, :, 0].argmax()][0])
top = tuple(cnt[cnt[:, :, 1].argmin()][0])
bottom = tuple(cnt[cnt[:, :, 1].argmax()][0])
topleft = [left[0] + 1, top[1]]
topleft = [left[0], top[1]]
# print(cnt, left, top, topleft)
bottomright = [right[0] - 1, bottom[1]]
bottomright = [right[0], bottom[1]]
for arr in cnt:
if np.array_equal(arr, np.array([bottomright])) or np.array_equal(arr, np.array([topleft])):
missing_external_edges = False
break
if missing_external_edges and (bottomright[0]-topleft[0])*(bottomright[1]-topleft[1])>= 50000:
topleft[0] -= 1
bottomright[0] += 1
if missing_external_edges and (bottomright[0]-topleft[0])*(bottomright[1]-topleft[1]) >= 50000:
cv2.rectangle(img_bin_final, tuple(topleft), tuple(bottomright), (255,255,255) , 2)
print("missing cell detectet rectangle drawn")
#print("missing cell detectet rectangle drawn")
return img_bin_final
def find_and_close_internal_gaps(img_bin_final):
contours, hierarchy = cv2.findContours(img_bin_final, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
def parse_tables_in_pdf(pages):
return zip(map(parse, pages), count())
def annotate_tables_in_pdf(pdf_path, page_index=1):
timeit()
#timeit()
page = pdf2image.convert_from_path(pdf_path, first_page=page_index + 1, last_page=page_index + 1)[0]
page = np.array(page)
_, stats = parse(page)
page = annotate_image(page, stats)
print(timeit())
#print(timeit())
fig, ax = plt.subplots(1, 1)
fig.set_size_inches(20, 20)
ax.imshow(page)