avoid creating invalid polygons via rounding

This commit is contained in:
Robert Sachunsky 2025-08-21 01:03:46 +02:00
parent 277d00579e
commit 3d53070b90
2 changed files with 10 additions and 4 deletions

View file

@ -3670,16 +3670,17 @@ class Eynollah:
return x_differential_new
def dilate_textregions_contours_textline_version(self, all_found_textline_polygons):
return [[np.array(make_valid(Polygon(poly[:, 0]).buffer(5)).exterior.coords,
return [[np.array(make_valid(Polygon(poly[:, 0]).buffer(5)).exterior.coords[:-1],
dtype=int)[:, np.newaxis]
for poly in region]
for region in all_found_textline_polygons]
def dilate_textregions_contours(self, all_found_textline_polygons):
return [np.array(make_valid(Polygon(poly[:, 0]).buffer(5)).exterior.coords,
return [np.array(make_valid(Polygon(poly[:, 0]).buffer(5)).exterior.coords[:-1],
dtype=int)[:, np.newaxis]
for poly in all_found_textline_polygons]
def dilate_textline_contours(self, all_found_textline_polygons):
for j in range(len(all_found_textline_polygons)):
for ij in range(len(all_found_textline_polygons[j])):

View file

@ -49,7 +49,7 @@ def filter_contours_area_of_image(image, contours, hierarchy, max_area, min_area
area <= max_area * np.prod(image.shape[:2]) and
hierarchy[0][jv][3] == -1):
found_polygons_early.append(np.array([[point]
for point in polygon.exterior.coords], dtype=np.uint))
for point in polygon.exterior.coords[:-1]], dtype=np.uint))
return found_polygons_early
def filter_contours_area_of_image_tables(image, contours, hierarchy, max_area, min_area):
@ -70,7 +70,7 @@ def filter_contours_area_of_image_tables(image, contours, hierarchy, max_area, m
True):
# print(c[0][0][1])
found_polygons_early.append(np.array([[point]
for point in polygon.exterior.coords], dtype=np.int32))
for point in polygon.exterior.coords[:-1]], dtype=np.int32))
return found_polygons_early
def find_new_features_of_contours(contours_main):
@ -327,6 +327,11 @@ def return_contours_of_interested_region_by_size(region_pre_p, pixel, min_area,
def make_valid(polygon: Polygon) -> Polygon:
"""Ensures shapely.geometry.Polygon object is valid by repeated rearrangement/simplification/enlargement."""
def isint(x):
return isinstance(x, int) or int(x) == x
# make sure rounding does not invalidate
if not all(map(isint, np.array(polygon.exterior.coords).flat)) and polygon.minimum_clearance < 1.0:
polygon = Polygon(np.round(polygon.exterior.coords))
points = list(polygon.exterior.coords)
# try by re-arranging points
for split in range(1, len(points)):