Skip to content

Commit b856f5b

Browse files
author
Robert Sachunsky
committed
resegment: join_polygons: make equidistant points relative to estimated scale
1 parent 56affe2 commit b856f5b

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

ocrd_cis/ocropy/resegment.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ def _process_segment(self, parent, parent_image, parent_coords, page_id, zoom, l
385385
# combine all assigned new lines to single outline polygon
386386
if len(new_lines) > 1:
387387
LOG.debug("joining %d new line polygons for '%s'", len(new_lines), line.id)
388-
new_polygon = join_polygons([intersections[(i, j)] for i in new_lines], loc=line.id)
388+
new_polygon = join_polygons([intersections[(i, j)] for i in new_lines],
389+
loc=line.id, scale=scale)
389390
line_polygons[j] = new_polygon
390391
# convert back to absolute (page) coordinates:
391392
line_polygon = coordinates_for_segment(new_polygon.exterior.coords[:-1],
@@ -460,7 +461,8 @@ def spread_dist(lines, old_labels, new_labels, binarized, components, coords,
460461
else:
461462
# get alpha shape
462463
poly = join_polygons([make_valid(Polygon(contour))
463-
for contour in contours], loc=line.id)
464+
for contour in contours],
465+
loc=line.id, scale=scale)
464466
poly = poly.exterior.coords[:-1]
465467
polygon = coordinates_for_segment(poly, None, coords)
466468
polygon = polygon_for_parent(polygon, line.parent_object_)
@@ -478,7 +480,7 @@ def diff_polygons(poly1, poly2):
478480
poly = make_valid(poly)
479481
return poly
480482

481-
def join_polygons(polygons, loc=''):
483+
def join_polygons(polygons, loc='', scale=20):
482484
"""construct concave hull (alpha shape) from input polygons"""
483485
# compoundp = unary_union(polygons)
484486
# jointp = compoundp.convex_hull
@@ -493,14 +495,14 @@ def join_polygons(polygons, loc=''):
493495
# (otherwise alphashape will jump across the interior)
494496
points = [poly.exterior.interpolate(dist).coords[0] # .xy
495497
for poly in polygons
496-
for dist in np.arange(0, poly.length, 5.0)]
498+
for dist in np.arange(0, poly.length, scale / 2)]
497499
#alpha = alphashape.optimizealpha(points) # too slow
498-
alpha = 0.05
500+
alpha = 0.03
499501
jointp = alphashape.alphashape(points, alpha)
500502
tries = 0
501503
# from descartes import PolygonPatch
502504
# import matplotlib.pyplot as plt
503-
while jointp.type in ['MultiPolygon', 'GeometryCollection']:
505+
while jointp.type in ['MultiPolygon', 'GeometryCollection'] or len(jointp.interiors):
504506
# plt.figure()
505507
# plt.gca().scatter(*zip(*points))
506508
# for geom in jointp.geoms:

0 commit comments

Comments
 (0)