Skip to content

[utils] Straighten grayscale pages in straighten_page - #2197

Merged
felixdittrich92 merged 2 commits into
mindee:mainfrom
RavSinghChandan:straighten-page-grayscale
Oct 30, 2026
Merged

felixdittrich92 merged 2 commits into
mindee:mainfrom
RavSinghChandan:straighten-page-grayscale

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

straighten_page padded the page with three pad widths, so a grayscale page of shape (H, W) raised ValueError: operands could not be broadcast together for any angle, although cv2.warpAffine and 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

@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.74%. Comparing base (6802eb8) to head (83a79f7).
⚠️ Report is 1 commits behind head on main.

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           
Flag Coverage Δ
unittests 97.74% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@felixdittrich92 felixdittrich92 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 to test_straighten_page_grayscale, e.g. geometry.straighten_page(page[..., :1], angle), and check that the shape keeps its last axis and the content matches straightened[..., :1];
  • update the docstring, which still says page: the original page image (H, W, C). It should say (H, W) or (H, W, C).

@felixdittrich92 felixdittrich92 self-assigned this Oct 30, 2026
@felixdittrich92 felixdittrich92 added type: enhancement Improvement module: utils Related to doctr.utils ext: tests Related to tests folder labels Oct 30, 2026
@felixdittrich92 felixdittrich92 added this to the 1.2.0 milestone Oct 30, 2026
@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

Thanks! Done:

  • The channel axis is put back right after the warp, so (H, W, 1) pages no longer crash and keep their last axis
  • test_straighten_page_grayscale now also checks the (H, W, 1) case against straightened[..., :1] and m_inv, and runs at angle 0 too (the path without padding)
  • The docstring now says (H, W) or (H, W, C)

@felixdittrich92
felixdittrich92 merged commit 3aa8ca8 into mindee:main Oct 30, 2026
57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ext: tests Related to tests folder module: utils Related to doctr.utils type: enhancement Improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants