Feat: add transform_to_rotated_box and box_xyxy_to_xyxyr#342
Feat: add transform_to_rotated_box and box_xyxy_to_xyxyr#342DerrickUnleashed wants to merge 19 commits into
transform_to_rotated_box and box_xyxy_to_xyxyr#342Conversation
Convert axis-aligned xyxy bounding boxes to xyxyr format (with 0 rotation) for rotated object detection pipelines. Includes an item-level transform that works on image_with_bounding_box objects and detection datasets.
…to_xyxyr - Fix broadcasting bug: cx/cy/hw/hh now reshaped to (n, 1) to match 2D angle - Fix dim arg: use dim=-1 (last axis) instead of dim=2 for min/max on corners - Fix dimension mismatch in torch_cat: reshape min/max results back to (n, 1) - Use torch_cat instead of torch_stack for corners to avoid 3D tensor - Handle empty boxes edge case (n=0) before any operations - Handle scalar angle broadcast to multiple boxes via expand - Accept per-box angle tensors
Draws rotated bounding boxes as polygons instead of axis-aligned rects. Computes the 4 corners of the rotated rectangle from xyxyr format and draws them with graphics::polygon.
|
@cregouby , I just wanted to know if I'm going in the right direction. |
|
Hello @DerrickUnleashed, This is the good direction, indeed, as a step toward implementing pytorch v2 transform. |
…egrees Align with pytorch/vision convention where angles are expressed in degrees for better human readability. Internally converts to radians for trigonometric computations.
…rendering The 5th column of xyxyr boxes is now in degrees after the preceding refactor. Convert to radians internally for trigonometric rendering.
…mage and boxes Rename item_transform_bbox_rotate() to item_transform_rotate() and move to a dedicated item_transform_geometry.R file (aligning with pytorch/vision file organization for geometric transforms). The function now rotates both the image (via transform_rotate) and the bounding boxes (via box_xyxy_to_xyr) together, matching the spirit of item_transform_* which should modify both and . Supports canvas expansion via the expand parameter. Angle parameter is in degrees (aligning with pytorch/vision convention).
The function converts bounding boxes to xyxyr format without modifying the image tensor, matching the original item_transform_bbox_rotate behavior.
cregouby
left a comment
There was a problem hiding this comment.
todo some small chrore
cregouby
left a comment
There was a problem hiding this comment.
todo Sorry I missed that : transform is intended for dataset augmentation, and shall rotate the image and the bbox, together, without bbox expansion.
This mean we have to use an additionnal box_rotate function and not the (to-be-kept) box_xyxy_to_xyxyr() function to rotate the box.
| expect_equal_to_r(result$y$boxes[1, 5], 0) | ||
| }) | ||
|
|
||
| test_that("item_transform_rotate does not modify the image", { |
There was a problem hiding this comment.
todo we DO expect the image to be modified !
| expect_equal(torch::as_array(result$y$boxes[, 5]), c(0, 0, 0)) | ||
| }) | ||
|
|
||
| test_that("item_transform_rotate does not mutate input", { |
There was a problem hiding this comment.
todo we DO expect the image to be modified !
| #' \eqn{r} is the rotation angle in degrees (counter-clockwise). The image | ||
| #' is left unchanged. For axis-aligned boxes, \eqn{r = 0}. |
There was a problem hiding this comment.
todo the image shall be rotated as well (see https://docs.pytorch.org/vision/main/auto_examples/transforms/plot_rotated_box_transforms.html)
|
|
||
| #' @export | ||
| item_transform_rotate.image_with_bounding_box <- function(x, angle = 0) { | ||
| x$y$boxes <- box_xyxy_to_xyxyr(x$y$boxes, angle = angle) |
There was a problem hiding this comment.
question I guess the required rotation of the box is much simpler here, as there is no need to take the axis-aligned bounding box of the rotated corners..
|
Apologies for the oversight I seem to have missed that the image is also modified that is rotated will implment this and update tests accordingly Hey, @cregouby , I have some questions to make sure the implementation is alright. https://developer.nvidia.com/blog/detecting-rotated-objects-using-the-odtk/ -> this just rotates the bbox The current task is just to rotate the image ? Do we need to rotate the bbox relative to the object. If we are gonna go by using PyTorch docs then do we utilise the |
|
I guess both features are valid:
The one you've made could be renamed `target_transform_rotate_box()` (as it does not require the image) and the one with image and box rotation `item_transform_rotate()`.
I guess you should focus more on the pytorch/vision/v2/transform for inspiration than from the Nvidia one (my bad)
Le 12 juillet 2026 17:03:26 GMT+02:00, Derrick Richard ***@***.***> a écrit :
…DerrickUnleashed left a comment (mlverse/torchvision#342)
Apologies for the oversight I seem to have missed that the image is also modified that is rotated will implment this and update tests accordingly
Hey, @cregouby , I have some questions to make sure the implementation is alright.
https://developer.nvidia.com/blog/detecting-rotated-objects-using-the-odtk/ -> this just rotates the bbox
The current task is just to rotate the image ? Do we need to rotate the bbox relative to the object.
--
Reply to this email directly or view it on GitHub:
#342 (comment)
You are receiving this because you were mentioned.
Message ID: ***@***.***>
|





Summary
item_transform_bbox_rotate()convertsimage_with_bounding_boxitems toimage_with_rotated_boxwith$boxesin xyxyr format.draw_bounding_boxes.image_with_rotated_box()renders them as rotated polygons.Changes
R/ops-box_convert.R—box_xyxy_to_xyxyr(boxes, angle)now rotates boxes around their center by the given angle (radians) and returns the enclosing axis-aligned box + angle column.R/target_transform_detection.R—item_transform_bbox_rotate(x, angle)S3 generic with methods forimage_with_bounding_boxanddataset.R/vision_utils.R— newdraw_bounding_boxes.image_with_rotated_boxS3 method that draws rotated rectangles as polygons viagraphics::polygon().tests/testthat/test-ops-box_convert.R— 5 new tests for rotation (pi/2, pi/4, per-box angles, edge cases).tests/testthat/test-target_transform_detection.R— 6 new tests for the item transform (basic, field preservation, empty, multiple, non-mutation, non-zero angle).Closes #338