Repository navigation
[utils] Expand grayscale images in rotate_image - #2195
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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
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) 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 totest_rotate_imagethat 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-onesimg,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).
|
Thanks for the careful review! Done:
|
rotate_imagerotates a grayscale image of shape (H, W) withoutexpand(as_utils.pydoes for the threshold map), but withexpand=Trueit raisedValueError: operands could not be broadcast togetherbecause the padding always had three axes.preserve_origin_shape=Truealso took the target size fromimage.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