From 8cab7e0722457556ae19e047e07b7e66f635eab5 Mon Sep 17 00:00:00 2001 From: Benjamin Kiessling Date: Fri, 11 Feb 2022 16:15:12 +0100 Subject: [PATCH] ugly bounding polygon hack for openiti expands bounding polygons to offset from baseline at maximum seam distance. Should solve dots getting cut off in Arabic print. --- kraken/lib/segmentation.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/kraken/lib/segmentation.py b/kraken/lib/segmentation.py index 0eb7f0bdc..888beb09b 100644 --- a/kraken/lib/segmentation.py +++ b/kraken/lib/segmentation.py @@ -599,16 +599,14 @@ def _extract_patch(env_up, env_bottom, baseline, offset_baseline, end_points, di upper_seam = geom.LineString(upper_seam).simplify(5) bottom_seam = geom.LineString(bottom_seam).simplify(5) - # ugly workaround against GEOM parallel_offset bug creating a - # MultiLineString out of offset LineString - if upper_seam.parallel_offset(offset//2, side='right').geom_type == 'MultiLineString' or offset == 0: - upper_seam = np.array(upper_seam.coords, dtype=int) - else: - upper_seam = np.array(upper_seam.parallel_offset(offset//2, side='right').coords, dtype=int)[::-1] - if bottom_seam.parallel_offset(offset//2, side='left').geom_type == 'MultiLineString' or offset == 0: - bottom_seam = np.array(bottom_seam.coords, dtype=int) - else: - bottom_seam = np.array(bottom_seam.parallel_offset(offset//2, side='left').coords, dtype=int) + # XXX: hacky trick to make sure dotting is included in bounding polygon by + # expanding it to its maximum distance from baseline. + baseline = geom.LineString(baseline) + us_dist = upper_seam.hausdorff_distance(baseline) + bs_dist = bottom_seam.hausdorff_distance(baseline) + + upper_seam = np.array(baseline.parallel_offset(us_dist, side='right'), dtype=int)[::-1] + bottom_seam = np.array(baseline.parallel_offset(bs_dist, side='left'), dtype=int) # offsetting might produce bounds outside the image. Clip it to the image bounds. polygon = np.concatenate(([end_points[0]], upper_seam, [end_points[-1]], bottom_seam[::-1]))