8000 Keep detected corners on the sheet when it is small in the frame by jasonmadigan · Pull Request #256 · tracefinity/tracefinity · GitHub
[go: up one dir, main page]

Skip to content

Keep detected corners on the sheet when it is small in the frame - #256

Merged
jasonmadigan merged 2 commits into
mainfrom
fix/254-sheet-corners
Nov 14, 2026
Merged

jasonmadigan merged 2 commits into
mainfrom
fix/254-sheet-corners

Conversation

@jasonmadigan
Copy link
Copy Markdown
Collaborator

Corner detection could return corners well off the paper when the sheet is small in the frame. Two things went wrong on the same photo.

The mask was the sheet. A small sheet is still the salient object, but its U2-Net mask is under the 40% of the frame that marks a mask as the sheet. The sheet was blacked out and the corners came back on the outline of the mask: 21 to 45 px outside the paper where the mask spilled over. Corners enclosing an area the mask mostly fills are now treated as the mask's own outline. Detection reruns unmasked and keeps them only if that finds nothing.

The unmasked result was no better. Canny finds no closed contour on wood grain, and the brightness strategy returned a bounding rectangle widened by bright floor beside the sheet, up to 68 px out. The saturation contour was within 4 px but ran after brightness had been accepted. When brightness succeeds, closed contours from the saturation, adaptive-threshold and low-threshold Canny strategies that pass the paper-fill check are now compared with it, and the candidate with the largest share of its boundary on a step from brighter inside to darker outside is returned.

Behaviour change

Photos where brightness detection used to win can now return a contour from another strategy, when more of that contour sits on the sheet's edge. The four existing photo fixtures are unchanged, within the same 8 px tolerance.

Verification

  • cd backend && venv/bin/python -m pytest: 878 passed.
  • ruff check backend/: clean.
  • New fixture distant-sheet.jpg: A4 covering a tenth of the frame on a light wooden floor, with the object painted over. Maximum corner error before: 68.1 px unmasked, 45.8 px with a sheet-sized mask. After: 3.7 px for both.
  • On the two original photos with the real U2-Net mask, corners are within 4 px of lines fitted to the paper edges.
  • Frontend untouched, so pnpm test and make test-e2e-accuracy were left to CI.

Closes #254

A sheet that is small in the frame is still the salient object, but its
U2-Net mask is under the 40% of the frame that marks a mask as the sheet.
The sheet was blacked out and the corners came back on the outline of the
mask, 21 to 45 px outside the paper on the photo this is taken from.
Corners enclosing an area the mask mostly fills are now treated as the
mask's own outline: detection reruns unmasked and keeps them only if that
finds nothing.

Unmasked, the brightness strategy returned a bounding rectangle widened by
bright floor beside the sheet, up to 68 px out, while the saturation
contour was within 4 px but never compared with it. When brightness
succeeds, closed contours that pass the paper-fill check are now compared
with it by the share of their boundary on a step from brighter inside to
darker outside, and the best supported is returned.

Closes #254

Signed-off-by: Jason Madigan <jason@jasonmadigan.com>
@jasonmadigan
jasonmadigan marked this pull request as ready for review November 14, 2026 20:12
@jasonmadigan
jasonmadigan merged commit 359ca56 into main Nov 14, 2026
6 checks passed
@jasonmadigan
jasonmadigan deleted the fix/254-sheet-corners branch November 14, 2026 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Corner detection follows the tool mask instead of the sheet when the sheet is small in the frame

1 participant

0