Skip to content

[utils] Expand grayscale images in rotate_image - #2195

Merged
felixdittrich92 merged 2 commits into
mindee:mainfrom
RavSinghChandan:rotate-image-grayscale-expand
Oct 30, 2026
Merged

felixdittrich92 merged 2 commits into
mindee:mainfrom
RavSinghChandan:rotate-image-grayscale-expand

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

rotate_image rotates a grayscale image of shape (H, W) without expand (as _utils.py does for the threshold map), but with expand=True it raised ValueError: operands could not be broadcast together because the padding always had three axes. preserve_origin_shape=True also took the target size from image.shape[:-1], which is only the height for such an image.

The padding now has one entry per axis of the image, and the target size is taken from its height and width, so a grayscale image is expanded like a color one

@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.72%. Comparing base (b14dd36) to head (bd1bc48).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2195      +/-   ##
==========================================
- Coverage   97.74%   97.72%   -0.02%     
==========================================
  Files         170      170              
  Lines       10961    10969       +8     
==========================================
+ Hits        10714    10720       +6     
- Misses        247      249       +2     
Flag Coverage Δ
unittests 97.72% <100.00%> (-0.02%) ⬇️

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) images now work with expand=True, including with preserve_origin_shape and at 90°, and match channel 0 of the RGB result. RGB images are unchanged. One thing left before merging:

(H, W, 1) images lose their channel axis

cv2.warpAffine (and cv2.resize) drop a singleton channel axis, so an (H, W, 1) input now comes back as (H', W'). Before this PR it crashed in the aspect-ratio pad. Now that _pad_width uses rot_img.ndim it no longer crashes, but a caller passing an (H, W, 1) mask silently gets a different number of dimensions back. The path without expand already did this, so it's best fixed for all modes at once. Restoring the axis just before returning does that:

            rot_img = cv2.resize(rot_img, image.shape[1::-1], interpolation=cv2.INTER_LINEAR)

    if rot_img.ndim < image.ndim:  # cv2 drops a singleton channel axis
        rot_img = rot_img[..., None]

    return rot_img

With this, img[..., :1] returns (32, 64, 1), (60, 120, 1), (32, 64, 1) and (64, 128, 1) for the no-expand, expand, expand + preserve_origin_shape and 90° cases, and matches channel 0 of the RGB result each time.

Could you also:

  • add an (H, W, 1) case to test_rotate_image that checks the trailing axis is kept;
  • use a random image for the new grayscale assertions, e.g. np.random.rand(32, 64, 3).astype(np.float32). With an all-ones img, np.array_equal(gray, color[..., 0]) would also pass if values were mixed across channels or the image were flipped;
  • mention the accepted shapes in the docstring: image: array of shape (H, W) or (H, W, C).

@felixdittrich92 felixdittrich92 self-assigned this Oct 30, 2026
@felixdittrich92 felixdittrich92 added this to the 1.2.0 milestone 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
@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review! Done:

  • The channel axis is restored before returning, so (H, W, 1) images keep it in every mode
  • test_rotate_image now uses a random image and checks (H, W) and (H, W, 1) against the RGB result for no expand, expand, expand + preserve_origin_shape and 90°
  • The docstring now says (H, W) or (H, W, C)

@felixdittrich92
felixdittrich92 merged commit 74e5759 into mindee:main Oct 30, 2026
56 of 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