Matching rescanned pages to a damaged PDF with SIFT, and admitting when it can't
Description and use
A damaged PDF was repaired by replacing ruined pages with fresh scans using Darnleaf, which employs SIFT and RANSAC for accurate matching while preserving bookmarks and links.
Problem
A scanned PDF had pages ruined by a coffee ring and a crease, and fresh scans of those pages were available in no particular order.
Outcome
On 90 synthetic rescans, 45 were Clear and none were wrong; the right page was listed for 89 of 90 rescans. The real wrong rate could still be about 6.7%.
Workflow
- Render each PDF page with PDFium and reduce size.
- Extract a subset of SIFT features from each rendered page.
- For each rescan, shortlist ten candidate pages using the SIFT features.
- Verify each candidate with RANSAC under a similarity transform, requiring scale 0.7–1.4 and skew ≤12°. Determine the required quarter turn from the transform. Assign rescans to pages one‑to‑one, preventing conflicts. Classify matches as Clear if they have sufficient consistent features, ink agreement, and a margin over the runner‑up; otherwise classify as Check. If OCR is available, adjust scores but do not create matches solely on text. Swap replaced pages in place using pikepdf, keeping untouched pages as original objects. Maintain bookmarks and links that point to replaced pages. Generate a JSON receipt recording SHA‑256 hashes of the original, output, and each rescan, plus evidence for each swap.
Evidence
On 90 synthetic rescans over ten held‑out seeds, 45 were Clear and none of those was wrong.
A match is Clear only if it has enough consistent features, enough ink agreement after alignment and a big margin over the runner‑up. Otherwise it's Check.
Replaced pages are swapped in place with pikepdf. Untouched pages stay the original objects, and bookmarks and links that point at a replaced page still work.
- Record date: 2026-10-21T03:41:20.059Z