modest improvement to cropping (safer)

This commit is contained in:
2026-09-15 15:13:41 +02:00
parent 9b22a8a137
commit 5b8215e7f5
6 changed files with 56 additions and 9 deletions
+17 -4
View File
@@ -65,13 +65,26 @@ def _large_blank_ink(gray: np.ndarray, clean: np.ndarray, dpi: float):
n, labels, stats, centers = cv2.connectedComponentsWithStats(
(residual > 35).astype(np.uint8), 8)
keep = np.zeros(n, bool)
xx = stats[1:, 0]
ww, hh, area = stats[1:, 2], stats[1:, 3], stats[1:, 4]
keep[1:] = ((area >= .8*px*px) & (np.minimum(ww, hh) >= .6*px)
& (area / (ww*hh) > .3)
& (np.maximum(ww, hh) < 4*np.minimum(ww, hh)))
density = area / (ww*hh)
shortest, longest = np.minimum(ww, hh), np.maximum(ww, hh)
compact_ink = ((area >= .8*px*px) & (shortest >= .6*px)
& (density > .3) & (longest < 4*shortest))
# Ruling suppression can fragment faint pencil handwriting into sparse,
# elongated components. Admit those moderately more readily in the central
# 80% of the sheet. The outermost 9% deliberately uses a stricter filter:
# punched holes, torn binding edges, and page numbers usually occur there.
center_x = xx + ww/2
central = (center_x > width*.1) & (center_x < width*.9)
central_ink = (central & (area >= .55*px*px) & (shortest >= .45*px)
& (density > .18) & (longest < 6*shortest))
outer = (center_x < width*.09) | (center_x > width*.91)
outer_ink = (outer & (area >= 1.2*px*px) & (shortest >= .8*px)
& (density >= .4) & (longest < 3*shortest))
keep[1:] = np.where(outer, outer_ink, compact_ink | central_ink)
# Use side columns as a prior, then require repeated size and alignment. An
# isolated note in the same column remains eligible to protect the margin.
xx = stats[1:, 0]
candidates = np.flatnonzero(
((xx < 15*px) | (xx+ww > width-15*px))
& (ww > px) & (ww < 9*px) & (hh > px) & (hh < 12*px)) + 1