Repository navigation
[utils] Straighten grayscale pages in straighten_page - #2197
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2197 +/- ##
=======================================
Coverage 97.74% 97.74%
=======================================
Files 170 170
Lines 10964 10968 +4
=======================================
+ Hits 10717 10721 +4
Misses 247 247
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
felixdittrich92
left a comment
There was a problem hiding this comment.
Thanks for the fix! (H, W) pages now work for every angle I tried (10, −30, 0, 45, ±90). The output matches channel 0 of the RGB result, m_inv is the same, and RGB pages are unchanged. One thing left before merging:
(H, W, 1) pages still crash
channels is computed once from page.ndim, so for an (H, W, 1) page it keeps one channel entry. The first np.pad works, but cv2.warpAffine drops a singleton channel axis and returns (H, W). The aspect-ratio pad then passes a 3-axis pad width to a 2D array:
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (3,2) and requested shape (2,2)
At angle 0 there's no padding, so it doesn't crash, but the page comes back as (H, W) and loses its channel axis.
Putting the axis back right after the warp fixes both cases:
rotated = cv2.warpAffine(exp_img, rot_mat, (pw_pad, ph_pad))
if rotated.ndim < exp_img.ndim: # cv2 drops a singleton channel axis
rotated = rotated[..., None]
With this, page[..., :1] returns (H', W', 1) and matches channel 0 of the RGB result for all the angles above.
Could you also:
- add the
(H, W, 1)case totest_straighten_page_grayscale, e.g.geometry.straighten_page(page[..., :1], angle), and check that the shape keeps its last axis and the content matchesstraightened[..., :1]; - update the docstring, which still says
page: the original page image (H, W, C). It should say(H, W)or(H, W, C).
|
Thanks! Done:
|
straighten_pagepadded the page with three pad widths, so a grayscale page of shape (H, W) raisedValueError: operands could not be broadcast togetherfor any angle, althoughcv2.warpAffineand the crop handle it.The padding now leaves a channel axis only when the page has one, so a grayscale page is straightened like each channel of a color page, with the same inverse matrix