Skip to content

Commit

Permalink
ugly bounding polygon hack for openiti
Browse files Browse the repository at this point in the history
expands bounding polygons to offset from baseline at maximum seam
distance. Should solve dots getting cut off in Arabic print.
  • Loading branch information
mittagessen committed Oct 25, 2024
1 parent 9540c18 commit 8cab7e0
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions kraken/lib/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down

0 comments on commit 8cab7e0

Please sign in to comment.