Repository navigation
Keep detected corners on the sheet when it is small in the frame - #256
Merged
Merged
Conversation
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
marked this pull request as ready for review
November 14, 2026 20:12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.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.pnpm testandmake test-e2e-accuracywere left to CI.Closes #254